You are here

protected function CreateStoreCommand::interact in Commerce Core 8.2

File

modules/store/src/Command/CreateStoreCommand.php, line 149

Class

CreateStoreCommand
Provides a command for creating stores.

Namespace

Drupal\commerce_store\Command

Code

protected function interact(InputInterface $input, OutputInterface $output) {
  $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);
  }
  $input
    ->setOption('name', $name);

  // --mail option.
  $mail = $input
    ->getOption('mail');
  if (!$mail) {
    $question = new Question('Enter the store email: ', '');
    $question
      ->setValidator(function ($mail) {
      if (empty($mail) || !$this->emailValidator
        ->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($this->countryRepository
      ->getList('en'));
    $question = new Question('Enter the store country: ', '');
    $question
      ->setAutocompleterValues($country_names);
    $question
      ->setValidator($required_validator);
    $country = $helper
      ->ask($input, $output, $question);
    $country = $country_names[$country];
  }
  $input
    ->setOption('country', $country);

  // --currency option.
  $currency = $input
    ->getOption('currency');
  if (!$currency) {
    $country = $this->countryRepository
      ->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);
  }
  $input
    ->setOption('currency', $currency);
}