2017-06-14 23:53:24 -05:00
|
|
|
<?php
|
|
|
|
|
|
2017-09-15 00:16:03 -05:00
|
|
|
namespace Pterodactyl\Services\Locations;
|
2017-06-14 23:53:24 -05:00
|
|
|
|
2018-01-03 21:14:53 -06:00
|
|
|
use Pterodactyl\Models\Location;
|
2017-09-15 00:16:03 -05:00
|
|
|
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
|
2017-07-02 21:29:58 -05:00
|
|
|
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
|
2017-09-15 00:16:03 -05:00
|
|
|
use Pterodactyl\Exceptions\Service\Location\HasActiveNodesException;
|
2017-06-14 23:53:24 -05:00
|
|
|
|
2017-09-15 00:16:03 -05:00
|
|
|
class LocationDeletionService
|
2017-06-14 23:53:24 -05:00
|
|
|
{
|
|
|
|
|
/**
|
2017-09-15 00:16:03 -05:00
|
|
|
* LocationDeletionService constructor.
|
2017-06-14 23:53:24 -05:00
|
|
|
*/
|
2017-09-15 00:16:03 -05:00
|
|
|
public function __construct(
|
2022-10-14 10:59:20 -06:00
|
|
|
protected LocationRepositoryInterface $repository,
|
2024-10-21 19:18:20 -06:00
|
|
|
protected NodeRepositoryInterface $nodeRepository,
|
2017-09-15 00:16:03 -05:00
|
|
|
) {
|
2017-06-14 23:53:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-09-15 00:16:03 -05:00
|
|
|
* Delete an existing location.
|
2017-06-14 23:53:24 -05:00
|
|
|
*
|
2024-10-21 19:18:20 -06:00
|
|
|
* @throws HasActiveNodesException
|
2017-06-14 23:53:24 -05:00
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
public function handle(Location|int $location): ?int
|
2017-06-14 23:53:24 -05:00
|
|
|
{
|
2025-12-20 15:55:13 -08:00
|
|
|
$location = $location instanceof Location ? $location->id : $location;
|
2017-06-14 23:53:24 -05:00
|
|
|
|
2017-09-15 00:16:03 -05:00
|
|
|
$count = $this->nodeRepository->findCountWhere([['location_id', '=', $location]]);
|
|
|
|
|
if ($count > 0) {
|
|
|
|
|
throw new HasActiveNodesException(trans('exceptions.locations.has_nodes'));
|
|
|
|
|
}
|
2017-06-14 23:53:24 -05:00
|
|
|
|
2017-09-15 00:16:03 -05:00
|
|
|
return $this->repository->delete($location);
|
2017-06-14 23:53:24 -05:00
|
|
|
}
|
|
|
|
|
}
|