You are here

protected function CreateStoreCommand::execute in Commerce Core 8.2

File

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

Class

CreateStoreCommand
Provides a command for creating stores.

Namespace

Drupal\commerce_store\Command

Code

protected function execute(InputInterface $input, OutputInterface $output) {
  $io = new DrupalStyle($input, $output);
  $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 = $this->countryRepository
      ->get($country_code, 'en');
    $currency_code = $country
      ->getCurrencyCode();
    if (empty($currency_code)) {
      $message = sprintf('Default currency not known for country %s, please specify one via --currency.', $country_code);
      throw new \RuntimeException($message);
    }
  }
  $store_storage = $this->entityTypeManager
    ->getStorage('commerce_store');
  $this->currencyImporter
    ->import($currency_code);
  $values = [
    'type' => 'online',
    'uid' => 1,
    'name' => $input
      ->getOption('name'),
    'mail' => $input
      ->getOption('mail'),
    'default_currency' => $currency_code,
    'timezone' => 'UTC',
    'address' => [
      'country_code' => $country_code,
    ],
  ];
  $store = $store_storage
    ->create($values);

  // Make this the default store, since there's no other.
  if (!$store_storage
    ->loadDefault()) {
    $store
      ->setDefault(TRUE);
  }
  $store
    ->save();
  $link = $this->urlGenerator
    ->generate('entity.commerce_store.edit_form', [
    'commerce_store' => $store
      ->id(),
  ], TRUE);
  $io
    ->writeln(sprintf('The store has been created. Go to %s to complete the store address and manage other settings.', $link));
}