1
1
mirror of https://github.com/pterodactyl/panel.git synced 2024-11-22 09:02:28 +01:00

Remove unused code

This commit is contained in:
Dane Everitt 2020-03-22 14:16:12 -07:00
parent 734bdc7b78
commit fd2ceacfe2
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
6 changed files with 0 additions and 691 deletions

View File

@ -1,98 +0,0 @@
<?php
namespace Pterodactyl\Services\Schedules;
use Cron\CronExpression;
use Pterodactyl\Models\Server;
use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Services\Schedules\Tasks\TaskCreationService;
use Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface;
class ScheduleCreationService
{
/**
* @var \Illuminate\Database\ConnectionInterface
*/
protected $connection;
/**
* @var \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface
*/
protected $repository;
/**
* @var \Pterodactyl\Services\Schedules\Tasks\TaskCreationService
*/
protected $taskCreationService;
/**
* ScheduleCreationService constructor.
*
* @param \Illuminate\Database\ConnectionInterface $connection
* @param \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface $repository
* @param \Pterodactyl\Services\Schedules\Tasks\TaskCreationService $taskCreationService
*/
public function __construct(
ConnectionInterface $connection,
ScheduleRepositoryInterface $repository,
TaskCreationService $taskCreationService
) {
$this->connection = $connection;
$this->repository = $repository;
$this->taskCreationService = $taskCreationService;
}
/**
* Create a new schedule for a specific server.
*
* @param \Pterodactyl\Models\Server $server
* @param array $data
* @param array $tasks
* @return \Pterodactyl\Models\Schedule
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Service\Schedule\Task\TaskIntervalTooLongException
*/
public function handle(Server $server, array $data, array $tasks = [])
{
$data = array_merge($data, [
'server_id' => $server->id,
'next_run_at' => $this->getCronTimestamp($data),
]);
$this->connection->beginTransaction();
$schedule = $this->repository->create($data);
foreach ($tasks as $index => $task) {
$this->taskCreationService->handle($schedule, [
'time_interval' => array_get($task, 'time_interval'),
'time_value' => array_get($task, 'time_value'),
'sequence_id' => $index + 1,
'action' => array_get($task, 'action'),
'payload' => array_get($task, 'payload'),
], false);
}
$this->connection->commit();
return $schedule;
}
/**
* Return a DateTime object after parsing the cron data provided.
*
* @param array $data
* @return \DateTime
*/
private function getCronTimestamp(array $data)
{
$formattedCron = sprintf('%s %s %s * %s',
array_get($data, 'cron_minute', '*'),
array_get($data, 'cron_hour', '*'),
array_get($data, 'cron_day_of_month', '*'),
array_get($data, 'cron_day_of_week', '*')
);
return CronExpression::factory($formattedCron)->getNextRunDate();
}
}

View File

@ -1,110 +0,0 @@
<?php
namespace Pterodactyl\Services\Schedules;
use Cron\CronExpression;
use Pterodactyl\Models\Schedule;
use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Contracts\Repository\TaskRepositoryInterface;
use Pterodactyl\Services\Schedules\Tasks\TaskCreationService;
use Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface;
class ScheduleUpdateService
{
/**
* @var \Illuminate\Database\ConnectionInterface
*/
private $connection;
/**
* @var \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface
*/
private $repository;
/**
* @var \Pterodactyl\Services\Schedules\Tasks\TaskCreationService
*/
private $taskCreationService;
/**
* @var \Pterodactyl\Contracts\Repository\TaskRepositoryInterface
*/
private $taskRepository;
/**
* ScheduleUpdateService constructor.
*
* @param \Illuminate\Database\ConnectionInterface $connection
* @param \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface $repository
* @param \Pterodactyl\Services\Schedules\Tasks\TaskCreationService $taskCreationService
* @param \Pterodactyl\Contracts\Repository\TaskRepositoryInterface $taskRepository
*/
public function __construct(
ConnectionInterface $connection,
ScheduleRepositoryInterface $repository,
TaskCreationService $taskCreationService,
TaskRepositoryInterface $taskRepository
) {
$this->connection = $connection;
$this->repository = $repository;
$this->taskCreationService = $taskCreationService;
$this->taskRepository = $taskRepository;
}
/**
* Update an existing schedule by deleting all current tasks and re-inserting the
* new values.
*
* @param \Pterodactyl\Models\Schedule $schedule
* @param array $data
* @param array $tasks
* @return \Pterodactyl\Models\Schedule
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
* @throws \Pterodactyl\Exceptions\Service\Schedule\Task\TaskIntervalTooLongException
*/
public function handle(Schedule $schedule, array $data, array $tasks): Schedule
{
$data = array_merge($data, [
'next_run_at' => $this->getCronTimestamp($data),
]);
$this->connection->beginTransaction();
$schedule = $this->repository->update($schedule->id, $data);
$this->taskRepository->deleteWhere([['schedule_id', '=', $schedule->id]]);
foreach ($tasks as $index => $task) {
$this->taskCreationService->handle($schedule, [
'time_interval' => array_get($task, 'time_interval'),
'time_value' => array_get($task, 'time_value'),
'sequence_id' => $index + 1,
'action' => array_get($task, 'action'),
'payload' => array_get($task, 'payload'),
], false);
}
$this->connection->commit();
return $schedule;
}
/**
* Return a DateTime object after parsing the cron data provided.
*
* @param array $data
* @return \DateTime
*/
private function getCronTimestamp(array $data)
{
$formattedCron = sprintf('%s %s %s * %s',
array_get($data, 'cron_minute', '*'),
array_get($data, 'cron_hour', '*'),
array_get($data, 'cron_day_of_month', '*'),
array_get($data, 'cron_day_of_week', '*')
);
return CronExpression::factory($formattedCron)->getNextRunDate();
}
}

View File

@ -1,70 +0,0 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* 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
* @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)
{
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;
$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'));
}
$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,
], false);
return $task;
}
}

View File

@ -1,108 +0,0 @@
<?php
namespace Tests\Unit\Services\Schedules;
use Mockery as m;
use Tests\TestCase;
use Cron\CronExpression;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\Schedule;
use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Services\Schedules\ScheduleCreationService;
use Pterodactyl\Services\Schedules\Tasks\TaskCreationService;
use Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface;
class ScheduleCreationServiceTest extends TestCase
{
/**
* @var \Illuminate\Database\ConnectionInterface
*/
protected $connection;
/**
* @var \Cron\CronExpression
*/
protected $cron;
/**
* @var \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface
*/
protected $repository;
/**
* @var \Pterodactyl\Services\Schedules\ScheduleCreationService
*/
protected $service;
/**
* @var \Pterodactyl\Services\Schedules\Tasks\TaskCreationService
*/
protected $taskCreationService;
/**
* Setup tests.
*/
public function setUp()
{
parent::setUp();
$this->connection = m::mock(ConnectionInterface::class);
$this->repository = m::mock(ScheduleRepositoryInterface::class);
$this->taskCreationService = m::mock(TaskCreationService::class);
$this->service = new ScheduleCreationService($this->connection, $this->repository, $this->taskCreationService);
}
/**
* Test that a schedule with no tasks can be created.
*/
public function testScheduleWithNoTasksIsCreated()
{
$schedule = factory(Schedule::class)->make();
$server = factory(Server::class)->make();
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('create')->with([
'server_id' => $server->id,
'next_run_at' => CronExpression::factory('* * * * *')->getNextRunDate(),
'test_key' => 'value',
])->once()->andReturn($schedule);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$response = $this->service->handle($server, ['test_key' => 'value', 'server_id' => '123abc']);
$this->assertInstanceOf(Schedule::class, $response);
$this->assertEquals($schedule, $response);
}
/**
* Test that a schedule with at least one task can be created.
*/
public function testScheduleWithTasksIsCreated()
{
$schedule = factory(Schedule::class)->make();
$server = factory(Server::class)->make();
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('create')->with([
'server_id' => $server->id,
'next_run_at' => CronExpression::factory('* * * * *')->getNextRunDate(),
'test_key' => 'value',
])->once()->andReturn($schedule);
$this->taskCreationService->shouldReceive('handle')->with($schedule, [
'time_interval' => 'm',
'time_value' => 10,
'sequence_id' => 1,
'action' => 'test',
'payload' => 'testpayload',
], false)->once()->andReturnNull();
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$response = $this->service->handle($server, ['test_key' => 'value'], [
['time_interval' => 'm', 'time_value' => 10, 'action' => 'test', 'payload' => 'testpayload'],
]);
$this->assertInstanceOf(Schedule::class, $response);
$this->assertEquals($schedule, $response);
}
}

View File

@ -1,97 +0,0 @@
<?php
namespace Tests\Unit\Services\Schedules;
use Mockery as m;
use Tests\TestCase;
use Cron\CronExpression;
use Pterodactyl\Models\Schedule;
use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Services\Schedules\ScheduleUpdateService;
use Pterodactyl\Contracts\Repository\TaskRepositoryInterface;
use Pterodactyl\Services\Schedules\Tasks\TaskCreationService;
use Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface;
class ScheduleUpdateServiceTest extends TestCase
{
/**
* @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock
*/
private $connection;
/**
* @var \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface|\Mockery\Mock
*/
private $repository;
/**
* @var \Pterodactyl\Services\Schedules\Tasks\TaskCreationService|\Mockery\Mock
*/
private $taskCreationService;
/**
* @var \Pterodactyl\Contracts\Repository\TaskRepositoryInterface|\Mockery\Mock
*/
private $taskRepository;
/**
* Setup tests.
*/
public function setUp()
{
parent::setUp();
$this->connection = m::mock(ConnectionInterface::class);
$this->repository = m::mock(ScheduleRepositoryInterface::class);
$this->taskCreationService = m::mock(TaskCreationService::class);
$this->taskRepository = m::mock(TaskRepositoryInterface::class);
}
/**
* Test that a schedule can be updated.
*/
public function testUpdate()
{
$schedule = factory(Schedule::class)->make();
$tasks = [['action' => 'test-action']];
$data = [
'cron_minute' => 1,
'cron_hour' => 2,
'cron_day_of_month' => 3,
'cron_day_of_week' => 4,
'next_run_at' => '_INVALID_VALUE',
];
$this->connection->shouldReceive('beginTransaction')->once()->withNoArgs();
$this->repository->shouldReceive('update')->once()->with($schedule->id, array_merge($data, [
'next_run_at' => CronExpression::factory('1 2 3 * 4')->getNextRunDate(),
]))->andReturn($schedule);
$this->taskRepository->shouldReceive('deleteWhere')->once()->with([['schedule_id', '=', $schedule->id]]);
$this->taskCreationService->shouldReceive('handle')->once()->with($schedule, m::subset([
'sequence_id' => 1,
'action' => 'test-action',
]), false);
$this->connection->shouldReceive('commit')->once()->withNoArgs();
$response = $this->getService()->handle($schedule, $data, $tasks);
$this->assertInstanceOf(Schedule::class, $response);
$this->assertSame($schedule, $response);
}
/**
* Return an instance of the service with mocked dependencies.
*
* @return \Pterodactyl\Services\Schedules\ScheduleUpdateService
*/
private function getService(): ScheduleUpdateService
{
return new ScheduleUpdateService(
$this->connection,
$this->repository,
$this->taskCreationService,
$this->taskRepository
);
}
}

View File

@ -1,208 +0,0 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Tests\Unit\Services\Schedules\Tasks;
use Mockery as m;
use Tests\TestCase;
use Pterodactyl\Models\Task;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\Schedule;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Contracts\Repository\TaskRepositoryInterface;
use Pterodactyl\Services\Schedules\Tasks\TaskCreationService;
use Pterodactyl\Exceptions\Service\Schedule\Task\TaskIntervalTooLongException;
class TaskCreationServiceTest extends TestCase
{
/**
* @var \Pterodactyl\Contracts\Repository\TaskRepositoryInterface
*/
protected $repository;
/**
* @var \Pterodactyl\Services\Schedules\Tasks\TaskCreationService
*/
protected $service;
/**
* Setup tests.
*/
public function setUp()
{
parent::setUp();
$this->repository = m::mock(TaskRepositoryInterface::class);
$this->service = new TaskCreationService($this->repository);
}
/**
* Test that a task is created and a model is returned for the task.
*
* @dataProvider validIntervalProvider
*/
public function testTaskIsCreatedAndModelReturned($interval, $value, $final)
{
$schedule = factory(Schedule::class)->make();
$task = factory(Task::class)->make();
$this->repository->shouldReceive('create')->with([
'schedule_id' => $schedule->id,
'sequence_id' => 1,
'action' => $task->action,
'payload' => $task->payload,
'time_offset' => $final,
], false)->once()->andReturn($task);
$response = $this->service->handle($schedule, [
'time_interval' => $interval,
'time_value' => $value,
'sequence_id' => 1,
'action' => $task->action,
'payload' => $task->payload,
]);
$this->assertNotEmpty($response);
$this->assertInstanceOf(Task::class, $response);
$this->assertEquals($task, $response);
}
/**
* Test that no new model is returned when a task is created.
*/
public function testTaskIsCreatedAndModelIsNotReturned()
{
$schedule = factory(Schedule::class)->make();
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('create')->with([
'schedule_id' => $schedule->id,
'sequence_id' => 1,
'action' => 'test',
'payload' => 'testpayload',
'time_offset' => 300,
], false)->once()->andReturn(true);
$response = $this->service->handle($schedule, [
'time_interval' => 'm',
'time_value' => 5,
'sequence_id' => 1,
'action' => 'test',
'payload' => 'testpayload',
], false);
$this->assertNotEmpty($response);
$this->assertNotInstanceOf(Task::class, $response);
$this->assertTrue($response);
}
/**
* Test that an ID can be passed in place of the schedule model itself.
*/
public function testIdCanBePassedInPlaceOfScheduleModel()
{
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('create')->with([
'schedule_id' => 1234,
'sequence_id' => 1,
'action' => 'test',
'payload' => 'testpayload',
'time_offset' => 300,
], false)->once()->andReturn(true);
$response = $this->service->handle(1234, [
'time_interval' => 'm',
'time_value' => 5,
'sequence_id' => 1,
'action' => 'test',
'payload' => 'testpayload',
], false);
$this->assertNotEmpty($response);
$this->assertNotInstanceOf(Task::class, $response);
$this->assertTrue($response);
}
/**
* Test exception is thrown if the interval is greater than 15 minutes.
*
* @dataProvider invalidIntervalProvider
*/
public function testExceptionIsThrownIfIntervalIsMoreThan15Minutes($interval, $value)
{
try {
$this->service->handle(1234, [
'time_interval' => $interval,
'time_value' => $value,
]);
} catch (DisplayException $exception) {
$this->assertInstanceOf(TaskIntervalTooLongException::class, $exception);
$this->assertEquals(trans('exceptions.tasks.chain_interval_too_long'), $exception->getMessage());
}
}
/**
* Test that exceptions are thrown if the Schedule module or ID is invalid.
*
* @dataProvider invalidScheduleArgumentProvider
* @expectedException \InvalidArgumentException
*/
public function testExceptionIsThrownIfInvalidArgumentIsPassed($argument)
{
$this->service->handle($argument, []);
}
/**
* Provides valid time intervals to be used in tests.
*
* @return array
*/
public function validIntervalProvider()
{
return [
['s', 30, 30],
['s', 60, 60],
['s', 90, 90],
['m', 1, 60],
['m', 5, 300],
];
}
/**
* Return invalid time formats.
*
* @return array
*/
public function invalidIntervalProvider()
{
return [
['m', 15.1],
['m', 16],
['s', 901],
];
}
/**
* Return an array of invalid schedule data to test against.
*
* @return array
*/
public function invalidScheduleArgumentProvider()
{
return [
[123.456],
['string'],
['abc123'],
['123_test'],
[new Server()],
[Schedule::class],
];
}
}