|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Devsrv\ScheduledAction; |
| 4 | +use Carbon\Carbon; |
| 5 | +use Illuminate\Database\Eloquent\Builder; |
| 6 | +use Illuminate\Database\Eloquent\Collection; |
| 7 | +use Devsrv\ScheduledAction\Models\ModelAction; |
| 8 | + |
| 9 | +class Action |
| 10 | +{ |
| 11 | + public function emit() |
| 12 | + { |
| 13 | + |
| 14 | + } |
| 15 | + |
| 16 | + protected function getPendingSchedules($date = null, $limit = 10, $orderBy = 'asc') : Collection { |
| 17 | + if($orderBy !== 'asc') $orderBy = 'desc'; |
| 18 | + |
| 19 | + $date = $date ?? Carbon::now(); |
| 20 | + $day = strtoupper($date->englishDayOfWeek); |
| 21 | + |
| 22 | + return ModelAction::pending() |
| 23 | + ->where(function($query) use($date) { |
| 24 | + $query |
| 25 | + ->whereDate('act_on', $date); |
| 26 | + }) |
| 27 | + ->when($day, function ($query, $day) { |
| 28 | + return $query |
| 29 | + ->orWhere(function($query) use ($day) { |
| 30 | + $query->whereHas('recurringDays', function (Builder $query) use ($day) { |
| 31 | + $query->where('day', $day); |
| 32 | + }); |
| 33 | + }); |
| 34 | + }) |
| 35 | + ->orderByRaw('TIME(`act_at`) '. $orderBy) |
| 36 | + ->take($limit) |
| 37 | + ->get(); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * the list of scheduled pending actions to run today |
| 42 | + * @param int $take optional how many results to show |
| 43 | + * @param string $orderBy optional order by time when to run the process 'asc' | 'desc' |
| 44 | + * @return Illuminate\Database\Eloquent\Collection |
| 45 | + */ |
| 46 | + public function needsToRunToday($take = 10, $orderBy = 'asc') : Collection { |
| 47 | + return $this->getPendingSchedules(Carbon::now(), $take, $orderBy); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * the list of scheduled pending actions to run on a given date |
| 52 | + * @param Carbon $date target date |
| 53 | + * @param int $take optional how many results to show |
| 54 | + * @param string $orderBy optional order by time when to run the process 'asc' | 'desc' |
| 55 | + * @return Illuminate\Database\Eloquent\Collection |
| 56 | + */ |
| 57 | + public function needsToRunOn(Carbon $date, $take = 10, $orderBy = 'asc') : Collection { |
| 58 | + return $this->getPendingSchedules($date, $take, $orderBy); |
| 59 | + } |
| 60 | +} |
0 commit comments