/
SeedCommand.php
85 lines (69 loc) · 1.87 KB
/
SeedCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
namespace Amranidev\Laracombee\Console\Commands;
use Laracombee;
use Illuminate\Console\Command;
use Amranidev\Laracombee\Console\LaracombeeCommand;
class SeedCommand extends LaracombeeCommand
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'laracombee:seed
{type : Catalog type (user or item)}
{--chunk= : total chunk}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Seed records into recombee db';
/**
* The default user model.
*
* @var string
*/
protected static $userModel = '\\App\\User';
/**
* The default chunk value.
*
* @var int
*/
protected static $chunk = 100;
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
$chunk = (int) $this->option('chunk') ?: self::$chunk;
$class = config('laracombee.'.$this->argument('type'));
$records = $class::all();
$total = $records->count();
$bar = $this->output->createProgressBar($total / $chunk);
$records->chunk($chunk)->each(function ($users) use ($bar) {
$batch = $this->{'add'.ucfirst($this->argument('type')).'s'}($users->all());
Laracombee::batch($batch)->then(function ($response) use ($bar) {
})->otherwise(function ($error) {
$this->info('');
$this->error($error);
die();
})->wait();
$bar->advance();
});
$bar->finish();
$this->info('');
$this->info('Done!');
}
}