1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace slavkluev\JobProgress; |
6
|
|
|
|
7
|
|
|
use slavkluev\JobProgress\models\JobProgress; |
8
|
|
|
use yii\base\Behavior; |
9
|
|
|
use yii\queue\ExecEvent; |
10
|
|
|
use yii\queue\Queue; |
11
|
|
|
|
12
|
|
|
class ProgressBehavior extends Behavior |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* The default value of max number of progress. |
16
|
|
|
* |
17
|
|
|
* @var int |
18
|
|
|
*/ |
19
|
|
|
public $progressMax = 10; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The default value of current number of progress. |
23
|
|
|
* |
24
|
|
|
* @var int |
25
|
|
|
*/ |
26
|
|
|
public $progressNow = 0; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The parameter is responsible for removing the progress after job execution. |
30
|
|
|
* |
31
|
|
|
* @var bool |
32
|
|
|
*/ |
33
|
|
|
public $deleteAfterExecution = false; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var JobProgress |
37
|
|
|
*/ |
38
|
|
|
protected $jobProgress; |
39
|
|
|
|
40
|
3 |
|
public function events() |
41
|
|
|
{ |
42
|
|
|
return [ |
43
|
3 |
|
Queue::EVENT_BEFORE_EXEC => 'beforeExec', |
44
|
|
|
Queue::EVENT_AFTER_EXEC => 'afterExec', |
45
|
|
|
]; |
46
|
|
|
} |
47
|
|
|
|
48
|
9 |
|
public function beforeExec(ExecEvent $event) |
49
|
|
|
{ |
50
|
9 |
|
$this->jobProgress = new JobProgress([ |
51
|
9 |
|
'job_id' => $event->id, |
52
|
9 |
|
'progress_max' => $this->progressMax, |
53
|
9 |
|
'progress_now' => $this->progressNow, |
54
|
|
|
]); |
55
|
9 |
|
$this->jobProgress->save(); |
56
|
9 |
|
} |
57
|
|
|
|
58
|
9 |
|
public function afterExec(ExecEvent $event) |
|
|
|
|
59
|
|
|
{ |
60
|
9 |
|
if ($this->deleteAfterExecution) { |
61
|
3 |
|
$this->jobProgress->delete(); |
62
|
|
|
} |
63
|
9 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return int |
67
|
|
|
*/ |
68
|
|
|
public function getProgressMax(): int |
69
|
|
|
{ |
70
|
|
|
return $this->jobProgress->getProgressMax(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return int |
75
|
|
|
*/ |
76
|
|
|
public function getProgressNow(): int |
77
|
|
|
{ |
78
|
|
|
return $this->jobProgress->getProgressNow(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return float |
83
|
|
|
*/ |
84
|
|
|
public function getPercent(): float |
85
|
|
|
{ |
86
|
|
|
return $this->jobProgress->getPercent(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Update the max number of progress. |
91
|
|
|
* |
92
|
|
|
* @param int $value |
93
|
|
|
*/ |
94
|
3 |
|
public function setProgressMax(int $value) |
95
|
|
|
{ |
96
|
3 |
|
$this->update(['progress_max' => $value]); |
97
|
3 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Update the current number of progress. |
101
|
|
|
* |
102
|
|
|
* @param int $value |
103
|
|
|
*/ |
104
|
3 |
|
public function setProgressNow(int $value) |
105
|
|
|
{ |
106
|
3 |
|
$this->update(['progress_now' => $value]); |
107
|
3 |
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Increase current number of progress by $offset. |
111
|
|
|
* |
112
|
|
|
* @param int $offset |
113
|
|
|
*/ |
114
|
|
|
public function incrementProgress(int $offset = 1) |
115
|
|
|
{ |
116
|
|
|
$value = $this->getProgressNow() + $offset; |
117
|
|
|
$this->setProgressNow($value); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param array $attributes |
122
|
|
|
*/ |
123
|
3 |
|
protected function update(array $attributes) |
124
|
|
|
{ |
125
|
3 |
|
$this->jobProgress->setAttributes($attributes); |
126
|
3 |
|
$this->jobProgress->update(); |
127
|
3 |
|
} |
128
|
|
|
} |
129
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.