1
|
|
|
<?php namespace Maqe\Qwatcher; |
2
|
|
|
|
3
|
|
|
use Illuminate\Database\Eloquent\Builder; |
4
|
|
|
use Illuminate\Database\Eloquent\Collection; |
5
|
|
|
use Illuminate\Pagination\LengthAwarePaginator; |
6
|
|
|
use Maqe\Qwatcher\Tracks\Enums\StatusType; |
7
|
|
|
use Maqe\Qwatcher\Tracks\TracksInterface; |
8
|
|
|
use Maqe\Qwatcher\Tracks\Tracks; |
9
|
|
|
use Maqe\Qwatcher\Tracks\Transformers\TrackTransformerInterface; |
10
|
|
|
|
11
|
|
|
class Qwatcher extends QwatchersAbstract |
12
|
|
|
{ |
13
|
|
|
private $trackTransformer; |
14
|
|
|
|
15
|
|
|
protected $statusable = []; |
16
|
|
|
|
17
|
|
|
protected $queryable = ['sort', 'limit']; |
18
|
|
|
|
19
|
|
|
protected $sortColumn = 'id'; |
20
|
|
|
|
21
|
|
|
protected $sortOrder = 'asc'; |
22
|
|
|
|
23
|
|
|
protected $sortable = ['id', 'driver', 'queue_at', 'job_name', 'process_at', 'success_at', 'failed_at']; |
24
|
|
|
|
25
|
|
|
protected $limit = null; |
26
|
|
|
|
27
|
|
|
public function __construct() |
28
|
|
|
{ |
29
|
|
|
$this->trackTransformer = app(TrackTransformerInterface::class); |
30
|
|
|
$this->statusable = StatusType::statsTypes(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Insert or update Track table depend on TracksInterface sub class (Adapter) |
35
|
|
|
* |
36
|
|
|
* @param TracksInterface $tracks Sub class that implements TracksInterface |
37
|
|
|
* @return mixed |
38
|
|
|
*/ |
39
|
|
|
public static function tracks(TracksInterface $tracks) |
40
|
|
|
{ |
41
|
|
|
return $tracks; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Added sort order to $this->sortOrder string, use with builder |
46
|
|
|
* |
47
|
|
|
* @param string $sortBy The sort string |
|
|
|
|
48
|
|
|
* @return $this |
49
|
|
|
*/ |
50
|
|
|
public function sortBy($sortColumn = 'id', $sortOrder = 'asc') |
51
|
|
|
{ |
52
|
|
|
|
53
|
|
|
$this->sortColumn = $this->filterSortColumn($sortColumn); |
54
|
|
|
$this->sortOrder = $this->filterSortOrder($sortOrder); |
55
|
|
|
|
56
|
|
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Set limit record to show |
61
|
|
|
* |
62
|
|
|
* @param integer $limit The number of record to retrieve |
63
|
|
|
* @return $this |
64
|
|
|
*/ |
65
|
|
|
public function limit($limit) |
66
|
|
|
{ |
67
|
|
|
$this->limit = $limit; |
68
|
|
|
|
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get the list of the queue track |
74
|
|
|
* |
75
|
|
|
* @return collection |
76
|
|
|
*/ |
77
|
|
View Code Duplication |
public function all() |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
$builder = new Tracks; |
80
|
|
|
|
81
|
|
|
$builder = $builder->newQuery(); |
82
|
|
|
|
83
|
|
|
$this->queryApplies($builder); |
84
|
|
|
|
85
|
|
|
return $this->trackTransformer->transforms($builder->get()); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get paginate list of the queue track |
90
|
|
|
* |
91
|
|
|
* @param $perPage The number of per page |
92
|
|
|
* @return collection |
93
|
|
|
*/ |
94
|
|
View Code Duplication |
public function paginate($perPage) |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
$builder = new Tracks; |
97
|
|
|
|
98
|
|
|
$builder = $builder->newQuery(); |
99
|
|
|
|
100
|
|
|
$this->queryApplies($builder); |
101
|
|
|
|
102
|
|
|
return $this->trackTransformer->transforms($builder->paginate($perPage)); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get the track record by id |
107
|
|
|
* |
108
|
|
|
* @param $id The track id |
109
|
|
|
* @return object |
110
|
|
|
*/ |
111
|
|
|
public function getById($id) |
112
|
|
|
{ |
113
|
|
|
return $this->trackTransformer->transform(Tracks::where('id', $id)->firstOrFail()); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Get the track record by current status |
118
|
|
|
* |
119
|
|
|
* @param $status The track status |
120
|
|
|
* @param $perPage The number of per page |
121
|
|
|
* @return collection |
122
|
|
|
*/ |
123
|
|
|
public function getByStatus($status, $per_page = null) |
124
|
|
|
{ |
125
|
|
|
if(!in_array($status, $this->statusable)) { |
126
|
|
|
throw new \InvalidArgumentException('"'.$status.'" is not allowed in status type'); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$builder = Tracks::whereNotNull('queue_at'); |
130
|
|
|
|
131
|
|
|
$methodName = 'filterBy'.ucfirst($status); |
132
|
|
|
|
133
|
|
|
if (method_exists($this, $methodName)) { |
134
|
|
|
$this->{$methodName}($builder); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$this->queryApplies($builder); |
138
|
|
|
|
139
|
|
View Code Duplication |
if (!is_null($per_page)) { |
|
|
|
|
140
|
|
|
return $this->trackTransformer->transformPaginator($builder->paginate($per_page)); |
141
|
|
|
} else { |
142
|
|
|
return $this->trackTransformer->transforms($builder->get()); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Get the track record by job name |
148
|
|
|
* |
149
|
|
|
* @param $name The job name |
150
|
|
|
* @param $perPage The number of per page |
151
|
|
|
* @return collection |
152
|
|
|
*/ |
153
|
|
|
public function getByJobName($name, $per_page = null) |
154
|
|
|
{ |
155
|
|
|
$collecName = strtolower(str_replace('_', '%',$name)); |
156
|
|
|
$condition = "`tracks`.`job_name` LIKE '%{$collecName}%'"; |
157
|
|
|
$builder = Tracks::whereRaw($condition); |
158
|
|
|
|
159
|
|
|
$this->queryApplies($builder); |
160
|
|
|
|
161
|
|
View Code Duplication |
if (!is_null($per_page)) { |
|
|
|
|
162
|
|
|
return $this->trackTransformer->transformPaginator($builder->paginate($per_page)); |
163
|
|
|
} else { |
164
|
|
|
return $this->trackTransformer->transforms($builder->get()); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Filter by queue date is not null (get queue that not run the job yet) |
170
|
|
|
* - process, succeed and failed must be null |
171
|
|
|
* |
172
|
|
|
* @param Builder $builder The tracks builder |
173
|
|
|
* @return Builder The query builder with filter applied. |
174
|
|
|
*/ |
175
|
|
|
protected function filterByQueue(Builder $builder) |
176
|
|
|
{ |
177
|
|
|
return $builder |
178
|
|
|
->whereNull('process_at') |
179
|
|
|
->whereNull('succeed_at') |
180
|
|
|
->whereNull('failed_at'); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Filter by process date is not null |
185
|
|
|
* - succeed and failed must be null |
186
|
|
|
* |
187
|
|
|
* @param Builder $builder The tracks builder |
188
|
|
|
* @return Builder The query builder with filter applied. |
189
|
|
|
*/ |
190
|
|
|
protected function filterByProcess(Builder $builder) |
191
|
|
|
{ |
192
|
|
|
return $builder |
193
|
|
|
->whereNotNull('process_at') |
194
|
|
|
->whereNull('succeed_at') |
195
|
|
|
->whereNull('failed_at'); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Filter by succeed date is not null |
200
|
|
|
* - process is not null |
201
|
|
|
* - failed is null |
202
|
|
|
* |
203
|
|
|
* @param Builder $builder The tracks builder |
204
|
|
|
* @return Builder The query builder with filter applied. |
205
|
|
|
*/ |
206
|
|
|
protected function filterBySucceed(Builder $builder) |
207
|
|
|
{ |
208
|
|
|
return $builder |
209
|
|
|
->whereNotNull('succeed_at') |
210
|
|
|
->whereNull('failed_at'); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Filter by failed date is not null |
215
|
|
|
* - succeed is null |
216
|
|
|
* |
217
|
|
|
* @param Builder $builder The tracks builder |
218
|
|
|
* @return Builder The query builder with filter applied. |
219
|
|
|
*/ |
220
|
|
|
protected function filterByFailed(Builder $builder) |
221
|
|
|
{ |
222
|
|
|
return $builder |
223
|
|
|
->whereNotNull('failed_at') |
224
|
|
|
->whereNull('succeed_at'); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Filter sort order column string |
229
|
|
|
* |
230
|
|
|
* @param string $sortColumn The sort order column string |
231
|
|
|
* @return stirng |
232
|
|
|
*/ |
233
|
|
|
protected function filterSortColumn($sortColumn) |
234
|
|
|
{ |
235
|
|
|
return in_array($sortColumn, $this->sortable) ? $sortColumn : 'id'; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Filter sort order string, allowed only asc, desc |
240
|
|
|
* |
241
|
|
|
* @param string $sortOrder The sort order string |
242
|
|
|
* @return stirng |
243
|
|
|
*/ |
244
|
|
|
protected function filterSortOrder($sortOrder) |
245
|
|
|
{ |
246
|
|
|
return in_array($sortOrder, ['asc', 'desc']) ? $sortOrder : 'asc'; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Apply limit if $this->limit is not null |
251
|
|
|
* |
252
|
|
|
* @param Builder $builder The tracks builder |
253
|
|
|
* @return Builder The query builder with take applied. |
254
|
|
|
*/ |
255
|
|
|
protected function applyLimit(Builder $builder) |
256
|
|
|
{ |
257
|
|
|
return (!is_null($this->limit)) ? $builder->take($this->limit) : $builder; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
protected function applySort(Builder $builder) |
261
|
|
|
{ |
262
|
|
|
return $builder->orderBy($this->sortColumn, $this->sortOrder); |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.