misc_pterodactyl-panel/app/Services/Schedules/Tasks/TaskCreationService.php

71 lines
2.3 KiB
PHP
Raw Normal View History

<?php
2017-09-26 02:43:01 +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
*/
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
{
const MAX_INTERVAL_TIME_SECONDS = 900;
/**
* @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
* @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)),
'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'];
if ($delay > self::MAX_INTERVAL_TIME_SECONDS) {
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();
$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);
return $task;
}
}