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