/**
  * Generate products.
  *
  * ## Options
  *
  * [--file=<file>]
  * : Specify which files to used. By default pulls random zips from DB.
  *
  * [--count=<count>]
  * : How many products to generate. Default: 15
  *
  * @param $args
  * @param $assoc_args
  */
 public function generate($args, $assoc_args)
 {
     $count = \WP_CLI\Utils\get_flag_value($assoc_args, 'count', 15);
     if (!\WP_CLI\Utils\get_flag_value($assoc_args, 'file')) {
         /**
          * @var \wpdb $wpdb
          */
         global $wpdb;
         $results = $wpdb->get_results($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_type = 'attachment'\n\t\t\t\t AND post_mime_type = 'application/zip' LIMIT %d", $count));
         $files = array();
         foreach ($results as $result) {
             $files[] = $result->ID;
         }
     } else {
         if (get_post_type($assoc_args['file']) != 'attachment') {
             WP_CLI::error("Invalid file post type.");
         }
         $files = array($assoc_args['file']);
     }
     if (empty($files)) {
         WP_CLI::error('No files exist.');
     }
     $notify = \WP_CLI\Utils\make_progress_bar('Generating products.', $count);
     $limits = array('-', '2', '2', '5', '5', '10');
     for ($i = 0; $i < $count; $i++) {
         $title = $this->faker->catchPhrase . ' software';
         $price = itelic_purebell(44, 199, 45);
         $price = floatval(intval($price));
         $index = array_rand($files);
         $file = get_post($files[$index]);
         try {
             $file = itelic_rename_file($file, $title . '-1.0.zip');
         } catch (InvalidArgumentException $e) {
             WP_CLI::error($e->getMessage());
         }
         $params = array('description' => $this->faker->realText(), 'limit' => $limits[array_rand($limits)], 'file' => $file->ID);
         $recurring = array(array('interval' => 'month', 'count' => 1, 'frequency' => 10), array('interval' => 'month', 'count' => 6, 'frequency' => 20), array('interval' => 'none', 'frequency' => 30), array('interval' => 'year', 'count' => 1, 'frequency' => 100));
         $rand = rand(0, 100);
         foreach ($recurring as $option) {
             if ($rand <= $option['frequency']) {
                 if ($option['interval'] != 'none') {
                     $params['interval'] = $option['interval'];
                     $params['interval-count'] = $option['count'];
                 }
                 break;
             }
         }
         $result = $this->create_product($title, $price, $params);
         if (is_wp_error($result)) {
             WP_CLI::error($result);
         }
         $notify->tick();
     }
     $notify->finish();
 }
 /**
  * Generate a minor release.
  *
  * @param \ITELIC\Release $latest
  *
  * @return \ITELIC\Release|null
  */
 protected function generate_minor_release(\ITELIC\Release $latest)
 {
     $product = $latest->get_product();
     $type = \ITELIC\Release::TYPE_MINOR;
     $version = \WP_CLI\Utils\increment_version($latest->get_version(), 'minor');
     $download = $latest->get_download();
     $name = $product->post_title . "-{$version}.zip";
     $file = itelic_rename_file($download, $name);
     $fixes = rand(3, 15);
     $tweaks = rand(1, 10);
     $adds = rand(0, 2);
     $changelog = $this->generate_changelog($adds, $tweaks, $fixes);
     return itelic_create_release(array('product' => $product, 'file' => $file, 'version' => $version, 'type' => $type, 'changelog' => $changelog));
 }