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

78 lines
2.5 KiB
PHP
Raw Normal View History

<?php
namespace Pterodactyl\Services\Servers;
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;
2020-12-16 23:55:44 +00:00
use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
use Pterodactyl\Exceptions\Http\Server\ServerTransferringException;
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\Repositories\Wings\DaemonServerRepository
*/
private $daemonServerRepository;
/**
* SuspensionService constructor.
*
* @param \Illuminate\Database\ConnectionInterface $connection
* @param \Pterodactyl\Repositories\Wings\DaemonServerRepository $daemonServerRepository
*/
public function __construct(
ConnectionInterface $connection,
DaemonServerRepository $daemonServerRepository
) {
$this->connection = $connection;
$this->daemonServerRepository = $daemonServerRepository;
}
/**
* Suspends a server on the system.
*
* @param \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]);
$isSuspending = $action === self::ACTION_SUSPEND;
// Nothing needs to happen if we're suspending the server and it is already
// suspended in the database. Additionally, nothing needs to happen if the server
// is not suspended and we try to un-suspend the instance.
if ($isSuspending === $server->suspended) {
return;
}
2020-12-16 23:55:44 +00:00
// Check if the server is currently being transferred.
if (! is_null($server->transfer)) {
2020-12-24 18:14:10 +00:00
throw new ServerTransferringException;
2020-12-16 23:55:44 +00:00
}
$this->connection->transaction(function () use ($action, $server) {
$server->update([
'suspended' => $action === self::ACTION_SUSPEND,
]);
// Only send the suspension request to wings if the server is not currently being transferred.
if (is_null($server->transfer)) {
$this->daemonServerRepository->setServer($server)->suspend($action === self::ACTION_UNSUSPEND);
}
});
}
}