2019-09-11 01:33:40 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Console\Command;
|
2019-10-01 05:30:11 +00:00
|
|
|
use Webkul\Product\Helpers\GenerateProduct;
|
2019-09-11 01:33:40 +00:00
|
|
|
|
2020-02-25 13:23:00 +00:00
|
|
|
/**
|
|
|
|
|
* Class GenerateProducts
|
|
|
|
|
*
|
|
|
|
|
* @package App\Console\Commands
|
|
|
|
|
*/
|
2019-09-11 01:33:40 +00:00
|
|
|
class GenerateProducts extends Command
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* The name and signature of the console command.
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2019-10-01 05:30:11 +00:00
|
|
|
protected $signature = 'bagisto:generate {value} {quantity}';
|
2019-09-11 01:33:40 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The console command description.
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
protected $description = 'Generates product with random attribute values.';
|
|
|
|
|
|
2019-10-01 05:30:11 +00:00
|
|
|
/**
|
|
|
|
|
* GenerateProduct instance
|
|
|
|
|
*/
|
|
|
|
|
protected $generateProduct;
|
|
|
|
|
|
2019-09-11 01:33:40 +00:00
|
|
|
/**
|
|
|
|
|
* ProductRepository instance
|
|
|
|
|
*/
|
|
|
|
|
protected $product;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a new command instance.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2019-10-01 05:30:11 +00:00
|
|
|
public function __construct(GenerateProduct $generateProduct)
|
2019-09-11 01:33:40 +00:00
|
|
|
{
|
|
|
|
|
parent::__construct();
|
|
|
|
|
|
2019-10-01 05:30:11 +00:00
|
|
|
$this->generateProduct = $generateProduct;
|
2019-09-11 01:33:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Execute the console command.
|
|
|
|
|
*
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
public function handle()
|
2019-10-03 05:27:07 +00:00
|
|
|
{
|
2019-10-01 05:30:11 +00:00
|
|
|
if (! is_string($this->argument('value')) || ! is_numeric($this->argument('quantity'))) {
|
|
|
|
|
$this->info('Illegal parameters or value of parameters are passed');
|
2019-09-11 01:33:40 +00:00
|
|
|
} else {
|
2020-02-25 13:23:00 +00:00
|
|
|
if (strtolower($this->argument('value')) == 'product' || strtolower($this->argument('value')) == 'products') {
|
|
|
|
|
$quantity = (int)$this->argument('quantity');
|
2019-10-01 05:30:11 +00:00
|
|
|
|
2020-02-25 13:23:00 +00:00
|
|
|
// @see https://laravel.com/docs/6.x/artisan#writing-output
|
|
|
|
|
// @see https://symfony.com/doc/current/components/console/helpers/progressbar.html
|
|
|
|
|
$bar = $this->output->createProgressBar($quantity);
|
|
|
|
|
|
|
|
|
|
$this->line("Generating $quantity {$this->argument('value')}.");
|
|
|
|
|
|
|
|
|
|
$bar->start();
|
|
|
|
|
|
|
|
|
|
$generatedProducts = 0;
|
|
|
|
|
$this->generateProduct->generateDemoBrand();
|
2019-10-01 05:30:11 +00:00
|
|
|
while ($quantity > 0) {
|
|
|
|
|
try {
|
|
|
|
|
$result = $this->generateProduct->create();
|
2020-02-25 13:23:00 +00:00
|
|
|
$generatedProducts++;
|
|
|
|
|
$bar->advance();
|
2019-10-01 05:30:11 +00:00
|
|
|
} catch (\Exception $e) {
|
2020-01-27 16:06:03 +00:00
|
|
|
report($e);
|
2019-10-01 05:30:11 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
2019-10-03 05:27:07 +00:00
|
|
|
|
2019-10-01 05:30:11 +00:00
|
|
|
$quantity--;
|
2019-10-03 05:27:07 +00:00
|
|
|
}
|
|
|
|
|
|
2020-02-25 13:23:00 +00:00
|
|
|
|
|
|
|
|
if ($result) {
|
|
|
|
|
$bar->finish();
|
|
|
|
|
$this->info("\n$generatedProducts Product(s) created successfully.");
|
|
|
|
|
} else {
|
2019-10-01 05:30:11 +00:00
|
|
|
$this->info('Product(s) cannot be created successfully.');
|
2020-02-25 13:23:00 +00:00
|
|
|
}
|
2019-10-01 05:30:11 +00:00
|
|
|
} else {
|
|
|
|
|
$this->line('Sorry, this generate option is invalid.');
|
|
|
|
|
}
|
2019-09-11 01:33:40 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|