PteroTheme/app/Services/Locations/LocationDeletionService.php

37 lines
891 B
PHP

<?php
namespace Pterodactyl\Services\Locations;
use Webmozart\Assert\Assert;
use Pterodactyl\Models\Location;
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
use Pterodactyl\Exceptions\Service\Location\HasActiveNodesException;
class LocationDeletionService
{
/**
* LocationDeletionService constructor.
*/
public function __construct(protected NodeRepositoryInterface $nodeRepository)
{
}
/**
* Delete an existing location.
*
* @throws HasActiveNodesException
*/
public function handle(Location|int $location): ?int
{
$location = ($location instanceof Location) ? $location : $location->id;
$count = $location->nodes()->count();
if ($count > 0) {
throw new HasActiveNodesException(trans('exceptions.locations.has_nodes'));
}
return $location->delete();
}
}