1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaraBlockList\Console\Commands; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Illuminate\Console\Command; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use Illuminate\Support\Collection; |
9
|
|
|
use LaraBlockList\Contracts\CanBeInBlocklist; |
10
|
|
|
use LaraBlockList\Jobs\CheckForBlocklistJob; |
11
|
|
|
|
12
|
|
|
class CheckForBlocklistCommand extends Command |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The name and signature of the console command. |
17
|
|
|
* |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $signature = 'blocklist:check |
21
|
|
|
{model : Model class} |
22
|
|
|
{id? : Entity id} |
23
|
|
|
{--F|from= : created started from; format Y-m-d} |
24
|
|
|
{--Q|queue=now : use queue or not} |
25
|
|
|
{--chunk=100 : Chunk count} |
26
|
|
|
{--delay=5 : Dispatch delay in seconds} |
27
|
|
|
{--model_id_column=id : Model identification column name} |
28
|
|
|
{--created_at_column=created_at : Created at column name} |
29
|
|
|
'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The console command description. |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $description = ''; |
37
|
|
|
|
38
|
5 |
|
public function handle() |
39
|
|
|
{ |
40
|
5 |
|
$job = CheckForBlocklistJob::class; |
41
|
|
|
|
42
|
5 |
|
$model = $this->argument('model'); |
43
|
|
|
|
44
|
5 |
|
if (!is_subclass_of($model, Model::class) || !is_a($model, CanBeInBlocklist::class, true)) { |
45
|
1 |
|
throw new \Exception("Class [{$model}] should be valid model."); |
46
|
|
|
} |
47
|
|
|
|
48
|
4 |
|
if ($id = $this->argument('id')) { |
49
|
1 |
|
$this->dispatchJob($job, $model, $id); |
50
|
|
|
} else { |
51
|
3 |
|
$query = $model::query(); |
52
|
|
|
|
53
|
3 |
|
if ($from = $this->option('from')) { |
54
|
1 |
|
$query->where( |
55
|
1 |
|
$this->createdAtColumnName(), |
56
|
1 |
|
'>=', |
57
|
1 |
|
Carbon::createFromFormat('Y-m-d', $from)->format('Y-m-d H:i:s') |
58
|
1 |
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
3 |
|
$query->select($this->modelIdColumnName())->chunk( |
62
|
3 |
|
$this->chunkCount(), |
63
|
3 |
|
fn (Collection $contacts) => $this->dispatchJob($job, $model, $contacts->pluck($this->modelIdColumnName())->toArray()) |
64
|
3 |
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
4 |
|
return 0; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
3 |
|
public function modelIdColumnName(): string |
74
|
|
|
{ |
75
|
3 |
|
return (string) $this->option('model_id_column'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
1 |
|
public function createdAtColumnName(): string |
82
|
|
|
{ |
83
|
1 |
|
return (string) $this->option('created_at_column'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return int |
88
|
|
|
*/ |
89
|
1 |
|
public function getDispatchDelay(): int |
90
|
|
|
{ |
91
|
1 |
|
return (int) $this->option('delay'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return int |
96
|
|
|
*/ |
97
|
3 |
|
public function chunkCount(): int |
98
|
|
|
{ |
99
|
3 |
|
return (int) $this->option('chunk'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $job |
104
|
|
|
* @param ...$options |
105
|
|
|
*/ |
106
|
4 |
|
protected function dispatchJob(string $job, ...$options) |
107
|
|
|
{ |
108
|
4 |
|
$queueName = $this->option('queue'); |
109
|
|
|
|
110
|
|
|
switch ($queueName) { |
111
|
4 |
|
case 'now': |
112
|
2 |
|
case 'sync': |
113
|
3 |
|
$job::dispatchSync(...$options); |
114
|
|
|
|
115
|
3 |
|
break; |
116
|
|
|
default: |
117
|
1 |
|
$job::dispatch(...$options)->onQueue($queueName ?? 'default') |
118
|
1 |
|
->delay(now()->addMinutes($this->getDispatchDelay())); |
119
|
|
|
|
120
|
1 |
|
break; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|