2017-08-05 22:20:07 +00:00
|
|
|
<?php
|
2022-01-15 16:10:37 +00:00
|
|
|
|
2017-08-05 22:20:07 +00:00
|
|
|
namespace Pterodactyl\Services\Nodes;
|
|
|
|
|
2017-08-05 22:26:30 +00:00
|
|
|
use Pterodactyl\Models\Node;
|
2017-08-05 22:20:07 +00:00
|
|
|
use Illuminate\Contracts\Translation\Translator;
|
|
|
|
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
|
2017-08-27 19:55:25 +00:00
|
|
|
use Pterodactyl\Exceptions\Service\HasActiveServersException;
|
2017-08-05 22:26:30 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
2017-08-05 22:20:07 +00:00
|
|
|
|
2017-08-27 20:10:51 +00:00
|
|
|
class NodeDeletionService
|
2017-08-05 22:20:07 +00:00
|
|
|
{
|
|
|
|
/**
|
2022-10-14 16:59:20 +00:00
|
|
|
* NodeDeletionService constructor.
|
2017-08-05 22:20:07 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
2022-10-14 16:59:20 +00:00
|
|
|
protected NodeRepositoryInterface $repository,
|
|
|
|
protected ServerRepositoryInterface $serverRepository,
|
|
|
|
protected Translator $translator
|
2017-08-05 22:20:07 +00:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a node from the panel if no servers are attached to it.
|
|
|
|
*
|
2017-08-21 00:23:50 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Service\HasActiveServersException
|
2017-08-05 22:20:07 +00:00
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function handle(int|Node $node): int
|
2017-08-05 22:20:07 +00:00
|
|
|
{
|
|
|
|
if ($node instanceof Node) {
|
|
|
|
$node = $node->id;
|
|
|
|
}
|
|
|
|
|
2018-01-05 04:49:50 +00:00
|
|
|
$servers = $this->serverRepository->setColumns('id')->findCountWhere([['node_id', '=', $node]]);
|
2017-08-05 22:20:07 +00:00
|
|
|
if ($servers > 0) {
|
2022-01-15 16:10:37 +00:00
|
|
|
throw new HasActiveServersException($this->translator->get('exceptions.node.servers_attached'));
|
2017-08-05 22:20:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->repository->delete($node);
|
|
|
|
}
|
|
|
|
}
|