misc_pterodactyl-panel/app/Services/Servers/SuspensionService.php

85 lines
2.4 KiB
PHP
Raw Normal View History

<?php
namespace Pterodactyl\Services\Servers;
use Psr\Log\LoggerInterface;
use Webmozart\Assert\Assert;
2017-08-05 22:26:30 +00:00
use Pterodactyl\Models\Server;
use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Repositories\Wings\DaemonServerRepository;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
class SuspensionService
{
2018-01-20 19:48:02 +00:00
const ACTION_SUSPEND = 'suspend';
const ACTION_UNSUSPEND = 'unsuspend';
/**
* @var \Illuminate\Database\ConnectionInterface
*/
private $connection;
/**
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
*/
private $repository;
/**
* @var \Psr\Log\LoggerInterface
*/
private $writer;
/**
* @var \Pterodactyl\Repositories\Wings\DaemonServerRepository
*/
private $daemonServerRepository;
/**
* SuspensionService constructor.
*
* @param \Illuminate\Database\ConnectionInterface $connection
* @param \Pterodactyl\Repositories\Wings\DaemonServerRepository $daemonServerRepository
2019-09-06 04:32:57 +00:00
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository
* @param \Psr\Log\LoggerInterface $writer
*/
public function __construct(
ConnectionInterface $connection,
DaemonServerRepository $daemonServerRepository,
ServerRepositoryInterface $repository,
LoggerInterface $writer
) {
$this->connection = $connection;
$this->repository = $repository;
$this->writer = $writer;
$this->daemonServerRepository = $daemonServerRepository;
}
/**
* Suspends a server on the system.
*
2017-08-22 03:10:48 +00:00
* @param int|\Pterodactyl\Models\Server $server
2019-09-06 04:32:57 +00:00
* @param string $action
*
* @throws \Throwable
*/
public function toggle(Server $server, $action = self::ACTION_SUSPEND)
{
Assert::oneOf($action, [self::ACTION_SUSPEND, self::ACTION_UNSUSPEND]);
if (
2018-01-20 19:48:02 +00:00
$action === self::ACTION_SUSPEND && $server->suspended ||
$action === self::ACTION_UNSUSPEND && ! $server->suspended
) {
return;
}
$this->connection->transaction(function () use ($action, $server) {
$this->repository->withoutFreshModel()->update($server->id, [
'suspended' => $action === self::ACTION_SUSPEND,
]);
$this->daemonServerRepository->setServer($server)->suspend($action === self::ACTION_UNSUSPEND);
});
}
}