You are here

public function GenerateProducts::validateDrushParams in Commerce Bulk 8

File

modules/commerce_generate/src/Plugin/DevelGenerate/GenerateProducts.php, line 689

Class

GenerateProducts
Provides a GenerateProducts plugin.

Namespace

Drupal\commerce_generate\Plugin\DevelGenerate

Code

public function validateDrushParams($args, $options = []) {
  $switcher = \Drupal::service('account_switcher');
  $switcher
    ->switchTo(new UserSession([
    'uid' => 1,
    'roles' => [
      'authenticated',
      'administrator',
    ],
  ]));

  // Without $switcher the store storage may be inaccessible in some set up.
  $stores = $product_types = [];
  foreach ($this->storeStorage
    ->loadMultiple() as $store) {
    $stores[] = $store
      ->id();
  }
  foreach ($this->productTypeStorage
    ->loadMultiple() as $type) {
    $product_types[] = $type
      ->id();
  }
  $switcher
    ->switchBack();
  if (empty($stores)) {
    throw new \Exception(dt('You do not have any stores to which generated products could be assigned. '));
  }
  if (empty($product_types)) {
    throw new \Exception(dt('You do not have any product types which could be created. '));
  }
  $values = [];
  $default_settings = $this
    ->getDefaultSettings();
  $values['num'] = array_shift($args);
  if (method_exists($this, 'isDrush8') && $this
    ->isDrush8()) {
    $keys = [
      'stores',
      'product-types',
      'kill',
      'batch',
      'title-prefix',
      'title-length',
      'price-min',
      'price-max',
      'price-per-variation',
      'owner',
      'languages',
    ];
    foreach ($keys as $index => $key) {
      if (!drush_get_option($key)) {
        unset($keys[$index]);
      }
      else {
        $keys[$index] = str_replace('-', '_', $key);
        if ($keys[$index] == 'kill') {
          $values['kill'] = TRUE;
          unset($keys[$index]);
        }
        elseif ($keys[$index] == 'price_per_variation') {
          $values['price_per_variation'] = TRUE;
          unset($keys[$index]);
        }
      }
    }
    $values += array_combine($keys, $args);
    if (!empty($values['stores'])) {
      $values['stores'] = StringUtils::csvToArray($values['stores']);
    }
    if (!empty($values['product_types'])) {
      $values['product_types'] = StringUtils::csvToArray($values['product_types']);
    }
    if (!empty($values['languages'])) {
      $values['languages'] = StringUtils::csvToArray($values['languages']);
    }
    $values += $default_settings;
  }
  else {
    if (!empty($options['stores'])) {
      $values['stores'] = StringUtils::csvToArray($options['stores']);
    }
    if (!empty($options['product_types'])) {
      $values['product_types'] = StringUtils::csvToArray($options['product_types']);
    }
    if (!empty($options['languages'])) {
      $values['languages'] = StringUtils::csvToArray($options['languages']);
    }
    $values += $options;
    $values += $default_settings;
  }
  if (!empty($values['languages'])) {
    $values['languages']['add_language'] = array_intersect($values['languages'], array_keys($this->languageManager
      ->getLanguages(LanguageInterface::STATE_ALL)));
  }
  if (!empty($values['stores'])) {
    $selected_stores = array_values(array_intersect($values['stores'], $stores));
    if ($selected_stores != $values['stores']) {
      throw new \Exception(dt('One or more stores have been entered that don\'t exist on this site'));
    }
  }
  elseif (!$values['kill'] && $values['num'] > 0) {
    $values['stores'] = $stores;
  }
  if (!empty($values['product_types'])) {
    $selected_product_types = array_values(array_intersect($values['product_types'], $product_types));
    if ($selected_product_types != $values['product_types']) {
      throw new \Exception(dt('One or more product types have been entered that don\'t exist on this site'));
    }
  }
  elseif ($values['num'] > 0) {
    $values['product_types'] = $product_types;
  }
  if (!empty($values['stores'])) {
    $values['stores'] = array_combine($values['stores'], $values['stores']);
  }
  if (!empty($values['product_types'])) {
    $values['product_types'] = array_combine($values['product_types'], $values['product_types']);
  }
  $values['price_number'] = '1.23';
  if ($values['num'] > 0 && $values['batch'] > 1 && $values['num'] >= $values['batch']) {
    $this->drushBatch = TRUE;
    $this
      ->prepareGenerateProduct($values);
  }
  return $values;
}