2017-08-09 06:24:55 +02:00
|
|
|
<?php
|
|
|
|
|
2017-10-07 23:16:51 +02:00
|
|
|
namespace Pterodactyl\Services\Eggs;
|
2017-08-09 06:24:55 +02:00
|
|
|
|
2017-10-07 06:57:53 +02:00
|
|
|
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
|
2017-10-07 23:16:51 +02:00
|
|
|
use Pterodactyl\Exceptions\Service\Egg\HasChildrenException;
|
2017-08-19 05:19:06 +02:00
|
|
|
use Pterodactyl\Exceptions\Service\HasActiveServersException;
|
2017-08-12 22:29:01 +02:00
|
|
|
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
2017-08-09 06:24:55 +02:00
|
|
|
|
2017-10-07 23:16:51 +02:00
|
|
|
class EggDeletionService
|
2017-08-09 06:24:55 +02:00
|
|
|
{
|
2017-08-12 22:29:01 +02:00
|
|
|
/**
|
2017-10-07 23:16:51 +02:00
|
|
|
* EggDeletionService constructor.
|
2017-08-09 06:24:55 +02:00
|
|
|
*/
|
2017-08-12 22:29:01 +02:00
|
|
|
public function __construct(
|
2022-10-14 18:59:20 +02:00
|
|
|
protected ServerRepositoryInterface $serverRepository,
|
|
|
|
protected EggRepositoryInterface $repository
|
2017-08-12 22:29:01 +02:00
|
|
|
) {
|
2017-08-09 06:24:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-07 23:16:51 +02:00
|
|
|
* Delete an Egg from the database if it has no active servers attached to it.
|
2017-08-09 06:24:55 +02:00
|
|
|
*
|
2017-08-19 05:19:06 +02:00
|
|
|
* @throws \Pterodactyl\Exceptions\Service\HasActiveServersException
|
2017-10-07 23:16:51 +02:00
|
|
|
* @throws \Pterodactyl\Exceptions\Service\Egg\HasChildrenException
|
2017-08-09 06:24:55 +02:00
|
|
|
*/
|
2017-10-07 23:16:51 +02:00
|
|
|
public function handle(int $egg): int
|
2017-08-09 06:24:55 +02:00
|
|
|
{
|
2017-10-07 23:16:51 +02:00
|
|
|
$servers = $this->serverRepository->findCountWhere([['egg_id', '=', $egg]]);
|
2017-08-12 22:29:01 +02:00
|
|
|
if ($servers > 0) {
|
2017-10-07 23:16:51 +02:00
|
|
|
throw new HasActiveServersException(trans('exceptions.nest.egg.delete_has_servers'));
|
2017-08-09 06:24:55 +02:00
|
|
|
}
|
|
|
|
|
2017-10-07 23:16:51 +02:00
|
|
|
$children = $this->repository->findCountWhere([['config_from', '=', $egg]]);
|
2017-09-30 19:54:09 +02:00
|
|
|
if ($children > 0) {
|
2017-10-07 23:16:51 +02:00
|
|
|
throw new HasChildrenException(trans('exceptions.nest.egg.has_children'));
|
2017-09-30 19:54:09 +02:00
|
|
|
}
|
|
|
|
|
2017-10-07 23:16:51 +02:00
|
|
|
return $this->repository->delete($egg);
|
2017-08-09 06:24:55 +02:00
|
|
|
}
|
|
|
|
}
|