This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Basebuilder\Scheduling\Event; |
||
4 | |||
5 | use Basebuilder\Scheduling\Event; |
||
6 | use Carbon\Carbon; |
||
7 | use Cron\CronExpression; |
||
8 | use Webmozart\Assert\Assert; |
||
9 | |||
10 | abstract class BaseEvent implements Event |
||
11 | { |
||
12 | /** |
||
13 | * @var string|null |
||
14 | */ |
||
15 | protected $name; |
||
16 | |||
17 | /** |
||
18 | * The cron expression representing the event's frequency. |
||
19 | * @var string |
||
20 | */ |
||
21 | protected $expression = '* * * * * *'; |
||
22 | |||
23 | /** |
||
24 | * The timezone the date should be evaluated on. |
||
25 | * @var \DateTimeZone|string |
||
26 | */ |
||
27 | protected $timezone; |
||
28 | |||
29 | /** |
||
30 | * The array of filter callbacks. These must return true |
||
31 | * @var callable[] |
||
32 | */ |
||
33 | protected $filters = []; |
||
34 | |||
35 | /** |
||
36 | * The array of callbacks to be run before the event is started. |
||
37 | * @var callable[] |
||
38 | */ |
||
39 | protected $beforeCallbacks = []; |
||
40 | |||
41 | /** |
||
42 | * The array of callbacks to be run after the event is finished. |
||
43 | * @var callable[] |
||
44 | */ |
||
45 | protected $afterCallbacks = []; |
||
46 | |||
47 | /** |
||
48 | * The array of reject callbacks. |
||
49 | * @var callable[] |
||
50 | */ |
||
51 | protected $rejects = []; |
||
52 | |||
53 | /** |
||
54 | * @var string |
||
55 | */ |
||
56 | protected $description; |
||
57 | |||
58 | /** |
||
59 | * If you want to run a single event, please give it a name |
||
60 | * |
||
61 | * @return null|string |
||
62 | */ |
||
63 | public function getName() |
||
64 | { |
||
65 | return $this->name; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @param string $name |
||
70 | * @return $this |
||
71 | */ |
||
72 | public function name($name) |
||
73 | { |
||
74 | $this->name = $name; |
||
75 | return $this; |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * @param string $description |
||
80 | * @return $this |
||
81 | */ |
||
82 | public function describe($description) |
||
83 | { |
||
84 | $this->description = $description; |
||
85 | return $this; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Schedule the event to run between start and end time. |
||
90 | * |
||
91 | * @param string $startTime |
||
92 | * @param string $endTime |
||
93 | * @return $this |
||
94 | */ |
||
95 | public function between($startTime, $endTime) |
||
96 | { |
||
97 | return $this->when($this->inTimeInterval($startTime, $endTime)); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * Schedule the event to not run between start and end time. |
||
102 | * |
||
103 | * @param string $startTime |
||
104 | * @param string $endTime |
||
105 | * @return $this |
||
106 | */ |
||
107 | public function notBetween($startTime, $endTime) |
||
108 | { |
||
109 | return $this->skip($this->inTimeInterval($startTime, $endTime)); |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Schedule the event to run between start and end time. |
||
114 | * |
||
115 | * @param string $startTime |
||
116 | * @param string $endTime |
||
117 | * @return \Closure |
||
118 | */ |
||
119 | private function inTimeInterval($startTime, $endTime) |
||
120 | { |
||
121 | return function () use ($startTime, $endTime) { |
||
122 | $now = Carbon::now()->getTimestamp(); |
||
123 | return $now >= strtotime($startTime) && $now <= strtotime($endTime); |
||
124 | }; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Set the timezone the date should be evaluated on. |
||
129 | * |
||
130 | * @return $this |
||
131 | */ |
||
132 | public function timezone(\DateTimeZone $timezone) |
||
133 | { |
||
134 | $this->timezone = $timezone; |
||
135 | |||
136 | return $this; |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * Determine if the given event should run based on the Cron expression. |
||
141 | * |
||
142 | * @return bool |
||
143 | */ |
||
144 | 2 | public function isDue() |
|
145 | { |
||
146 | 2 | return $this->expressionPasses() && $this->filtersPass(); |
|
147 | } |
||
148 | |||
149 | /** |
||
150 | * Determine if the Cron expression passes. |
||
151 | * |
||
152 | * @return boolean |
||
153 | */ |
||
154 | 2 | protected function expressionPasses() |
|
155 | { |
||
156 | 2 | $date = Carbon::now(); |
|
157 | |||
158 | 2 | if ($this->timezone) { |
|
159 | $date->setTimezone($this->timezone); |
||
160 | } |
||
161 | |||
162 | 2 | return $this->getCronExpression()->isDue($date->toDateTimeString()); |
|
163 | } |
||
164 | |||
165 | /** |
||
166 | * Determine if the filters pass for the event. |
||
167 | * |
||
168 | * @return boolean |
||
169 | */ |
||
170 | 2 | protected function filtersPass() |
|
171 | { |
||
172 | 2 | foreach ($this->filters as $callback) { |
|
173 | 1 | if (!call_user_func($callback)) { |
|
174 | 1 | return false; |
|
175 | } |
||
176 | 2 | } |
|
177 | |||
178 | 2 | foreach ($this->rejects as $callback) { |
|
179 | 1 | if (call_user_func($callback)) { |
|
180 | 1 | return false; |
|
181 | } |
||
182 | 2 | } |
|
183 | |||
184 | 2 | return true; |
|
185 | } |
||
186 | |||
187 | /** |
||
188 | * The Cron expression representing the event's frequency. |
||
189 | * |
||
190 | * @param string $expression |
||
191 | * @return $this |
||
192 | */ |
||
193 | 14 | public function cron(/* string */ $expression) |
|
194 | { |
||
195 | 14 | Assert::stringNotEmpty($expression); |
|
196 | |||
197 | 14 | $this->expression = $expression; |
|
198 | |||
199 | 14 | return $this; |
|
200 | } |
||
201 | |||
202 | /** |
||
203 | * @return CronExpression |
||
204 | */ |
||
205 | 14 | public function getCronExpression() |
|
206 | { |
||
207 | 14 | return CronExpression::factory($this->expression); |
|
208 | } |
||
209 | |||
210 | /** |
||
211 | * Change the minute when the job should run (0-59, *, *\/2 etc) |
||
212 | * |
||
213 | * @param string|int $minute |
||
214 | * @return $this |
||
215 | */ |
||
216 | 4 | public function minute($minute) |
|
217 | { |
||
218 | 4 | return $this->spliceIntoPosition(1, $minute); |
|
219 | } |
||
220 | |||
221 | /** |
||
222 | * Schedule the event to run every minute. |
||
223 | * |
||
224 | * @return $this |
||
225 | */ |
||
226 | 3 | public function everyMinute() |
|
227 | { |
||
228 | 3 | return $this->minute('*'); |
|
229 | } |
||
230 | |||
231 | /** |
||
232 | * Schedule this event to run every 5 minutes |
||
233 | * |
||
234 | * @return $this |
||
235 | */ |
||
236 | public function everyFiveMinutes() |
||
237 | { |
||
238 | return $this->everyNMinutes(5); |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * Schedule the event to run every N minutes |
||
243 | * |
||
244 | * @param int $n |
||
245 | * @return $this |
||
246 | */ |
||
247 | 1 | public function everyNMinutes(/* int */ $n) |
|
0 ignored issues
–
show
|
|||
248 | { |
||
249 | 1 | Assert::integer($n); |
|
250 | |||
251 | 1 | return $this->minute("*/{$n}"); |
|
0 ignored issues
–
show
As per coding-style, please use concatenation or
sprintf for the variable $n instead of interpolation.
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings. // Instead of
$x = "foo $bar $baz";
// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
![]() |
|||
252 | } |
||
253 | |||
254 | /** |
||
255 | * Set the hour when the job should run (0-23, *, *\/2, etc) |
||
256 | * |
||
257 | * @param string|int $hour |
||
258 | * @return $this |
||
259 | */ |
||
260 | 1 | public function hour($hour) |
|
261 | { |
||
262 | 1 | return $this->spliceIntoPosition(2, $hour); |
|
263 | } |
||
264 | |||
265 | /** |
||
266 | * Schedule the event to run hourly. |
||
267 | * |
||
268 | * @return $this |
||
269 | */ |
||
270 | 1 | public function hourly() |
|
271 | { |
||
272 | 1 | return $this |
|
273 | 1 | ->spliceIntoPosition(1, 0) |
|
274 | 1 | ->spliceIntoPosition(2, '*'); |
|
275 | } |
||
276 | |||
277 | /** |
||
278 | * Schedule the event to run daily. |
||
279 | * |
||
280 | * @return $this |
||
281 | */ |
||
282 | 1 | public function daily() |
|
283 | { |
||
284 | 1 | return $this |
|
285 | 1 | ->spliceIntoPosition(1, 0) |
|
286 | 1 | ->spliceIntoPosition(2, 0); |
|
287 | } |
||
288 | |||
289 | /** |
||
290 | * Schedule the event to run daily at a given time (10:00, 19:30, etc). |
||
291 | * |
||
292 | * @param string $time |
||
293 | * @return $this |
||
294 | */ |
||
295 | 1 | public function dailyAt(/* string */ $time) |
|
296 | { |
||
297 | 1 | Assert::stringNotEmpty($time); |
|
298 | |||
299 | 1 | $segments = explode(':', $time); |
|
300 | |||
301 | 1 | return $this->spliceIntoPosition(2, (int) $segments[0]) |
|
302 | 1 | ->spliceIntoPosition(1, count($segments) == 2 ? (int) $segments[1] : '0'); |
|
303 | } |
||
304 | |||
305 | /** |
||
306 | * Set the days of the week the command should run on. |
||
307 | * |
||
308 | * @param array|mixed $days |
||
309 | * @return $this |
||
310 | */ |
||
311 | 1 | public function days($days) |
|
312 | { |
||
313 | 1 | $days = is_array($days) ? $days : func_get_args(); |
|
0 ignored issues
–
show
|
|||
314 | |||
315 | 1 | return $this->spliceIntoPosition(5, implode(',', $days)); |
|
316 | } |
||
317 | |||
318 | /** |
||
319 | * Schedule the event to run only on weekdays. |
||
320 | * |
||
321 | * @return $this |
||
322 | */ |
||
323 | 1 | public function weekdays() |
|
324 | { |
||
325 | 1 | return $this->spliceIntoPosition(5, '1-5'); |
|
326 | } |
||
327 | |||
328 | /** |
||
329 | * Schedule the event to run only on Mondays. |
||
330 | * |
||
331 | * @return $this |
||
332 | */ |
||
333 | public function mondays() |
||
334 | { |
||
335 | return $this->days(1); |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * Schedule the event to run only on Tuesdays. |
||
340 | * |
||
341 | * @return $this |
||
342 | */ |
||
343 | public function tuesdays() |
||
344 | { |
||
345 | return $this->days(2); |
||
346 | } |
||
347 | |||
348 | /** |
||
349 | * Schedule the event to run only on Wednesdays. |
||
350 | * |
||
351 | * @return $this |
||
352 | */ |
||
353 | public function wednesdays() |
||
354 | { |
||
355 | return $this->days(3); |
||
356 | } |
||
357 | |||
358 | /** |
||
359 | * Schedule the event to run only on Thursdays. |
||
360 | * |
||
361 | * @return $this |
||
362 | */ |
||
363 | public function thursdays() |
||
364 | { |
||
365 | return $this->days(4); |
||
366 | } |
||
367 | |||
368 | /** |
||
369 | * Schedule the event to run only on Fridays. |
||
370 | * |
||
371 | * @return $this |
||
372 | */ |
||
373 | public function fridays() |
||
374 | { |
||
375 | return $this->days(5); |
||
376 | } |
||
377 | |||
378 | /** |
||
379 | * Schedule the event to run only on Saturdays. |
||
380 | * |
||
381 | * @return $this |
||
382 | */ |
||
383 | public function saturdays() |
||
384 | { |
||
385 | return $this->days(6); |
||
386 | } |
||
387 | |||
388 | /** |
||
389 | * Schedule the event to run only on Sundays. |
||
390 | * |
||
391 | * @return $this |
||
392 | */ |
||
393 | public function sundays() |
||
394 | { |
||
395 | return $this->days(0); |
||
396 | } |
||
397 | |||
398 | /** |
||
399 | * Schedule the event to run weekly. |
||
400 | * |
||
401 | * @return $this |
||
402 | */ |
||
403 | 1 | public function weekly() |
|
404 | { |
||
405 | 1 | return $this->spliceIntoPosition(1, 0) |
|
406 | 1 | ->spliceIntoPosition(2, 0) |
|
407 | 1 | ->spliceIntoPosition(5, 0); |
|
408 | } |
||
409 | |||
410 | /** |
||
411 | * Schedule the event to run weekly on a given day and time. |
||
412 | * |
||
413 | * @param int $day |
||
414 | * @param string $time |
||
415 | * @return $this |
||
416 | */ |
||
417 | public function weeklyOn($day, $time = '0:0') |
||
418 | { |
||
419 | $this->dailyAt($time); |
||
420 | return $this->spliceIntoPosition(5, $day); |
||
421 | } |
||
422 | |||
423 | /** |
||
424 | * Schedule the event to run monthly. |
||
425 | * |
||
426 | * @return $this |
||
427 | */ |
||
428 | 1 | public function monthly() |
|
429 | { |
||
430 | 1 | return $this->spliceIntoPosition(1, 0) |
|
431 | 1 | ->spliceIntoPosition(2, 0) |
|
432 | 1 | ->spliceIntoPosition(3, 1); |
|
433 | } |
||
434 | |||
435 | /** |
||
436 | * Schedule the event to run monthly on a given day and time. |
||
437 | * |
||
438 | * @param int $day |
||
439 | * @param string $time |
||
440 | * @return $this |
||
441 | */ |
||
442 | public function monthlyOn($day = 1, $time = '0:0') |
||
443 | { |
||
444 | $this->dailyAt($time); |
||
445 | return $this->spliceIntoPosition(3, $day); |
||
446 | } |
||
447 | |||
448 | /** |
||
449 | * Schedule the event to run quarterly. |
||
450 | * |
||
451 | * @return $this |
||
452 | */ |
||
453 | 1 | public function quarterly() |
|
454 | { |
||
455 | 1 | return $this->spliceIntoPosition(1, 0) |
|
456 | 1 | ->spliceIntoPosition(2, 0) |
|
457 | 1 | ->spliceIntoPosition(3, 1) |
|
458 | 1 | ->spliceIntoPosition(4, '*/3'); |
|
459 | } |
||
460 | |||
461 | /** |
||
462 | * Schedule the event to run yearly. |
||
463 | * |
||
464 | * @return $this |
||
465 | */ |
||
466 | 1 | public function yearly() |
|
467 | { |
||
468 | 1 | return $this->spliceIntoPosition(1, 0) |
|
469 | 1 | ->spliceIntoPosition(2, 0) |
|
470 | 1 | ->spliceIntoPosition(3, 1) |
|
471 | 1 | ->spliceIntoPosition(4, 1); |
|
472 | } |
||
473 | |||
474 | /** |
||
475 | * Splice the given value into the given position of the expression. |
||
476 | * |
||
477 | * @param int $position |
||
478 | * @param string $value |
||
479 | * @return $this |
||
480 | */ |
||
481 | 14 | protected function spliceIntoPosition($position, $value) |
|
482 | { |
||
483 | 14 | $segments = explode(' ', $this->expression); |
|
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 16 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues. ![]() |
|||
484 | 14 | $segments[$position - 1] = $value; |
|
485 | 14 | return $this->cron(implode(' ', $segments)); |
|
486 | } |
||
487 | |||
488 | |||
489 | /** |
||
490 | * Register a callback to further filter the schedule. |
||
491 | * |
||
492 | * @param callable $callback |
||
493 | * @return $this |
||
494 | */ |
||
495 | 1 | public function when(callable $callback) |
|
496 | { |
||
497 | 1 | $this->filters[] = $callback; |
|
498 | |||
499 | 1 | return $this; |
|
500 | } |
||
501 | |||
502 | /** |
||
503 | * Register a callback to further filter the schedule. |
||
504 | * |
||
505 | * @param callable $callback |
||
506 | * @return $this |
||
507 | */ |
||
508 | 1 | public function skip(callable $callback) |
|
509 | { |
||
510 | 1 | $this->rejects[] = $callback; |
|
511 | |||
512 | 1 | return $this; |
|
513 | } |
||
514 | |||
515 | /** |
||
516 | * Register a callback to be called before the operation. |
||
517 | * |
||
518 | * @param callable $callback |
||
519 | * @return $this |
||
520 | */ |
||
521 | public function before(callable $callback) |
||
522 | { |
||
523 | $this->beforeCallbacks[] = $callback; |
||
524 | |||
525 | return $this; |
||
526 | } |
||
527 | |||
528 | /** |
||
529 | * Register a callback to be called after the operation. |
||
530 | * |
||
531 | * @param callable $callback |
||
532 | * @return $this |
||
533 | */ |
||
534 | public function after(callable $callback) |
||
535 | { |
||
536 | $this->afterCallbacks[] = $callback; |
||
537 | |||
538 | return $this; |
||
539 | } |
||
540 | } |
||
541 |
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.