2017-09-13 04:45:19 +00:00
|
|
|
<?php
|
2017-09-26 02:43:01 +00:00
|
|
|
/**
|
2017-09-13 04:45:19 +00:00
|
|
|
* Pterodactyl - Panel
|
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
|
|
|
*
|
2017-09-26 02:43:01 +00:00
|
|
|
* This software is licensed under the terms of the MIT license.
|
|
|
|
* https://opensource.org/licenses/MIT
|
2017-09-13 04:45:19 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\Schedules\Tasks;
|
|
|
|
|
|
|
|
use Webmozart\Assert\Assert;
|
|
|
|
use Pterodactyl\Models\Schedule;
|
|
|
|
use Pterodactyl\Contracts\Repository\TaskRepositoryInterface;
|
|
|
|
use Pterodactyl\Exceptions\Service\Schedule\Task\TaskIntervalTooLongException;
|
|
|
|
|
|
|
|
class TaskCreationService
|
|
|
|
{
|
2017-11-12 23:16:54 +00:00
|
|
|
const MAX_INTERVAL_TIME_SECONDS = 900;
|
|
|
|
|
2017-09-13 04:45:19 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\TaskRepositoryInterface
|
|
|
|
*/
|
|
|
|
protected $repository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TaskCreationService constructor.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Contracts\Repository\TaskRepositoryInterface $repository
|
|
|
|
*/
|
|
|
|
public function __construct(TaskRepositoryInterface $repository)
|
|
|
|
{
|
|
|
|
$this->repository = $repository;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new task that is assigned to a schedule.
|
|
|
|
*
|
|
|
|
* @param int|\Pterodactyl\Models\Schedule $schedule
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param array $data
|
|
|
|
* @param bool $returnModel
|
2017-09-13 04:45:19 +00:00
|
|
|
* @return bool|\Pterodactyl\Models\Task
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Service\Schedule\Task\TaskIntervalTooLongException
|
|
|
|
*/
|
|
|
|
public function handle($schedule, array $data, $returnModel = true)
|
|
|
|
{
|
2017-09-15 04:02:31 +00:00
|
|
|
Assert::true(($schedule instanceof Schedule || is_digit($schedule)),
|
2017-09-13 04:45:19 +00:00
|
|
|
'First argument passed to handle must be numeric or instance of \Pterodactyl\Models\Schedule, received %s.'
|
|
|
|
);
|
|
|
|
|
|
|
|
$schedule = ($schedule instanceof Schedule) ? $schedule->id : $schedule;
|
2017-09-15 04:02:31 +00:00
|
|
|
$delay = $data['time_interval'] === 'm' ? $data['time_value'] * 60 : $data['time_value'];
|
2017-11-12 23:16:54 +00:00
|
|
|
if ($delay > self::MAX_INTERVAL_TIME_SECONDS) {
|
2017-09-13 04:45:19 +00:00
|
|
|
throw new TaskIntervalTooLongException(trans('exceptions.tasks.chain_interval_too_long'));
|
|
|
|
}
|
|
|
|
|
2018-01-05 22:33:50 +00:00
|
|
|
$repository = ($returnModel) ? $this->repository : $this->repository->withoutFreshModel();
|
2017-09-13 04:45:19 +00:00
|
|
|
$task = $repository->create([
|
|
|
|
'schedule_id' => $schedule,
|
|
|
|
'sequence_id' => $data['sequence_id'],
|
|
|
|
'action' => $data['action'],
|
|
|
|
'payload' => $data['payload'],
|
|
|
|
'time_offset' => $delay,
|
2018-02-04 19:51:24 +00:00
|
|
|
], false);
|
2017-09-13 04:45:19 +00:00
|
|
|
|
|
|
|
return $task;
|
|
|
|
}
|
|
|
|
}
|