More server management via the API

This commit is contained in:
Dane Everitt 2018-01-20 13:48:02 -06:00
parent 3724559468
commit 17544481b5
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
6 changed files with 193 additions and 26 deletions

View file

@ -19,6 +19,9 @@ use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonS
class SuspensionService
{
const ACTION_SUSPEND = 'suspend';
const ACTION_UNSUSPEND = 'unsuspend';
/**
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface
*/
@ -70,29 +73,29 @@ class SuspensionService
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function toggle($server, $action = 'suspend')
public function toggle($server, $action = self::ACTION_SUSPEND)
{
if (! $server instanceof Server) {
$server = $this->repository->find($server);
}
if (! in_array($action, ['suspend', 'unsuspend'])) {
if (! in_array($action, [self::ACTION_SUSPEND, self::ACTION_UNSUSPEND])) {
throw new \InvalidArgumentException(sprintf(
'Action must be either suspend or unsuspend, %s passed.',
'Action must be either ' . self::ACTION_SUSPEND . ' or ' . self::ACTION_UNSUSPEND . ', %s passed.',
$action
));
}
if (
$action === 'suspend' && $server->suspended ||
$action === 'unsuspend' && ! $server->suspended
$action === self::ACTION_SUSPEND && $server->suspended ||
$action === self::ACTION_UNSUSPEND && ! $server->suspended
) {
return true;
}
$this->database->beginTransaction();
$this->repository->withoutFreshModel()->update($server->id, [
'suspended' => $action === 'suspend',
'suspended' => $action === self::ACTION_SUSPEND,
]);
try {