|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Pterodactyl\Services\Schedules; |
| 4 | + |
| 5 | +use Cron\CronExpression; |
| 6 | +use Pterodactyl\Models\Schedule; |
| 7 | +use Illuminate\Database\ConnectionInterface; |
| 8 | +use Pterodactyl\Contracts\Repository\TaskRepositoryInterface; |
| 9 | +use Pterodactyl\Services\Schedules\Tasks\TaskCreationService; |
| 10 | +use Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface; |
| 11 | + |
| 12 | +class ScheduleUpdateService |
| 13 | +{ |
| 14 | + /** |
| 15 | + * @var \Illuminate\Database\ConnectionInterface |
| 16 | + */ |
| 17 | + private $connection; |
| 18 | + /** |
| 19 | + * @var \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface |
| 20 | + */ |
| 21 | + private $repository; |
| 22 | + /** |
| 23 | + * @var \Pterodactyl\Contracts\Repository\TaskRepositoryInterface |
| 24 | + */ |
| 25 | + private $taskRepository; |
| 26 | + /** |
| 27 | + * @var \Pterodactyl\Services\Schedules\Tasks\TaskCreationService |
| 28 | + */ |
| 29 | + private $taskCreationService; |
| 30 | + |
| 31 | + /** |
| 32 | + * ScheduleUpdateService constructor. |
| 33 | + * |
| 34 | + * @param \Illuminate\Database\ConnectionInterface $connection |
| 35 | + * @param \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface $repository |
| 36 | + * @param \Pterodactyl\Services\Schedules\Tasks\TaskCreationService $taskCreationService |
| 37 | + * @param \Pterodactyl\Contracts\Repository\TaskRepositoryInterface $taskRepository |
| 38 | + */ |
| 39 | + public function __construct( |
| 40 | + ConnectionInterface $connection, |
| 41 | + ScheduleRepositoryInterface $repository, |
| 42 | + TaskCreationService $taskCreationService, |
| 43 | + TaskRepositoryInterface $taskRepository |
| 44 | + ) { |
| 45 | + $this->connection = $connection; |
| 46 | + $this->repository = $repository; |
| 47 | + $this->taskCreationService = $taskCreationService; |
| 48 | + $this->taskRepository = $taskRepository; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Update an existing schedule by deleting all current tasks and re-inserting the |
| 53 | + * new values. |
| 54 | + * |
| 55 | + * @param \Pterodactyl\Models\Schedule $schedule |
| 56 | + * @param array $data |
| 57 | + * @param array $tasks |
| 58 | + * @return \Pterodactyl\Models\Schedule |
| 59 | + * |
| 60 | + * @throws \Pterodactyl\Exceptions\Model\DataValidationException |
| 61 | + * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException |
| 62 | + * @throws \Pterodactyl\Exceptions\Service\Schedule\Task\TaskIntervalTooLongException |
| 63 | + */ |
| 64 | + public function handle(Schedule $schedule, array $data, array $tasks): Schedule |
| 65 | + { |
| 66 | + $data = array_merge($data, [ |
| 67 | + 'next_run_at' => $this->getCronTimestamp($data), |
| 68 | + ]); |
| 69 | + |
| 70 | + $this->connection->beginTransaction(); |
| 71 | + |
| 72 | + $schedule = $this->repository->update($schedule->id, $data); |
| 73 | + $this->taskRepository->deleteWhere([['schedule_id', '=', $schedule->id]]); |
| 74 | + |
| 75 | + foreach ($tasks as $index => $task) { |
| 76 | + $this->taskCreationService->handle($schedule, [ |
| 77 | + 'time_interval' => array_get($task, 'time_interval'), |
| 78 | + 'time_value' => array_get($task, 'time_value'), |
| 79 | + 'sequence_id' => $index + 1, |
| 80 | + 'action' => array_get($task, 'action'), |
| 81 | + 'payload' => array_get($task, 'payload'), |
| 82 | + ], false); |
| 83 | + } |
| 84 | + |
| 85 | + $this->connection->commit(); |
| 86 | + |
| 87 | + return $schedule; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Return a DateTime object after parsing the cron data provided. |
| 92 | + * |
| 93 | + * @param array $data |
| 94 | + * @return \DateTime |
| 95 | + */ |
| 96 | + private function getCronTimestamp(array $data) |
| 97 | + { |
| 98 | + $formattedCron = sprintf('%s %s %s * %s *', |
| 99 | + array_get($data, 'cron_minute', '*'), |
| 100 | + array_get($data, 'cron_hour', '*'), |
| 101 | + array_get($data, 'cron_day_of_month', '*'), |
| 102 | + array_get($data, 'cron_day_of_week', '*') |
| 103 | + ); |
| 104 | + |
| 105 | + return CronExpression::factory($formattedCron)->getNextRunDate(); |
| 106 | + } |
| 107 | +} |
0 commit comments