diff --git a/modules/store/commerce_store.services.yml b/modules/store/commerce_store.services.yml index f454e17f..071acca9 100644 --- a/modules/store/commerce_store.services.yml +++ b/modules/store/commerce_store.services.yml @@ -1,4 +1,9 @@ services: + commerce_store.create_store_command: + class: Drupal\commerce_store\Command\CreateStoreCommand + tags: + - { name: drupal.command } + commerce_store.store_context: class: Drupal\commerce_store\StoreContext arguments: ['@request_stack', '@commerce_store.chain_store_resolver'] diff --git a/modules/store/src/Command/CreateStoreCommand.php b/modules/store/src/Command/CreateStoreCommand.php index 6dd5d46d..f5994ed4 100644 --- a/modules/store/src/Command/CreateStoreCommand.php +++ b/modules/store/src/Command/CreateStoreCommand.php @@ -4,12 +4,26 @@ namespace Drupal\commerce_store\Command; use CommerceGuys\Intl\Currency\CurrencyRepository; use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\Console\Question\Question; -use Drupal\Console\Command\ContainerAwareCommand; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputOption; +use Drupal\Console\Core\Command\Shared\ContainerAwareCommandTrait; +use Drupal\Console\Core\Style\DrupalStyle; +use Drupal\Console\Annotations\DrupalCommand; + +/** + * Class CreateStoreCommand. + * + * @package Drupal\commerce_store + * + * @DrupalCommand ( + * extension="commerce_store", + * extensionType="module" + * ) + */ +class CreateStoreCommand extends Command { -class CreateStoreCommand extends ContainerAwareCommand { + use ContainerAwareCommandTrait; /** * {@inheritdoc} @@ -24,19 +38,20 @@ class CreateStoreCommand extends ContainerAwareCommand { ->addOption('currency', '', InputOption::VALUE_OPTIONAL, 'The store currency (optional)'); } + /** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { - $container = $this->getContainer(); - $currency_importer = $container->get('commerce_price.currency_importer'); - $store_storage = $container->get('entity_type.manager')->getStorage('commerce_store'); - + $currency_importer = $this->get('commerce_price.currency_importer'); + /** @var \Drupal\commerce_store\StoreStorageInterface $store_storage */ + $store_storage = $this->get('entity_type.manager')->getStorage('commerce_store'); $country_code = $input->getOption('country'); $currency_code = $input->getOption('currency'); // Allow the caller to specify the country, but not the currency. if (empty($currency_code)) { - $country_repository = $container->get('address.country_repository'); + /** @var \CommerceGuys\Intl\Country\CountryRepositoryInterface $country_repository */ + $country_repository = $this->get('address.country_repository'); $country = $country_repository->get($country_code, 'en'); $currency_code = $country->getCurrencyCode(); if (empty($currency_code)) { @@ -63,7 +78,7 @@ class CreateStoreCommand extends ContainerAwareCommand { $store_storage->markAsDefault($store); } - $link = $container->get('url_generator')->generate('entity.commerce_store.edit_form', ['commerce_store' => $store->id()], TRUE); + $link = $this->get('url_generator')->generate('entity.commerce_store.edit_form', ['commerce_store' => $store->id()], TRUE); $output->writeln(sprintf('The store has been created. Go to %s to complete the store address and manage other settings.', $link)); } @@ -71,48 +86,33 @@ class CreateStoreCommand extends ContainerAwareCommand { * {@inheritdoc} */ protected function interact(InputInterface $input, OutputInterface $output) { - $container = $this->getContainer(); - $mail_validator = $container->get('email.validator'); - $country_repository = $container->get('address.country_repository'); + $io = new DrupalStyle($input, $output); + $mail_validator = $this->get('email.validator'); + $country_repository = $this->get('address.country_repository'); $currency_repository = new CurrencyRepository(); - $helper = $this->getHelper('question'); - // Symfony Console has no built-in way to ensure the value is not empty. - $required_validator = function ($value) { - if (empty($value)) { - throw new \RuntimeException("Value can't be empty."); - } - return $value; - }; // --name option. $name = $input->getOption('name'); if (!$name) { - $question = new Question('Enter the store name: ', ''); - $question->setValidator($required_validator); - $name = $helper->ask($input, $output, $question); + $name = $io->ask('Enter the store name', ''); } $input->setOption('name', $name); // --mail option. $mail = $input->getOption('mail'); if (!$mail) { - $question = new Question('Enter the store email: ', ''); - $question->setValidator(function ($mail) use ($mail_validator) { + $mail = $io->ask('Enter the store email', '', function ($mail) use ($mail_validator) { if (empty($mail) || !$mail_validator->isValid($mail)) { throw new \RuntimeException('The entered email is not valid.'); } return $mail; }); - $mail = $helper->ask($input, $output, $question); } $input->setOption('mail', $mail); // --country option. $country = $input->getOption('country'); if (!$country) { $country_names = array_flip($country_repository->getList('en')); - $question = new Question('Enter the store country: ', ''); - $question->setAutocompleterValues($country_names); - $question->setValidator($required_validator); - $country = $helper->ask($input, $output, $question); + $country = $io->choiceNoList('Enter the store country', $country_names, ''); $country = $country_names[$country]; } $input->setOption('country', $country); @@ -121,17 +121,9 @@ class CreateStoreCommand extends ContainerAwareCommand { if (!$currency) { $country = $country_repository->get($country, 'en'); $currency_code = $country->getCurrencyCode(); - if ($currency_code) { - $question = new Question("Enter the store currency [$currency_code]: ", $currency_code); - } - else { - $question = new Question('Enter the store currency: '); - } - $question->setAutocompleterValues(array_keys($currency_repository->getList('en'))); - $question->setValidator($required_validator); - $currency = $helper->ask($input, $output, $question); + $currency_names = array_keys($currency_repository->getList('en')); + $currency = $io->choiceNoList('Enter the store currency', $currency_names, $currency_code); } $input->setOption('currency', $currency); } - }