Replace node repository

This commit is contained in:
Lance Pioch 2022-10-23 22:17:24 -04:00
parent 7266c66ebf
commit aeb7590a6d
14 changed files with 101 additions and 250 deletions

View file

@ -2,39 +2,26 @@
namespace Pterodactyl\Services\Locations;
use Webmozart\Assert\Assert;
use Pterodactyl\Models\Location;
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
use Pterodactyl\Exceptions\Service\Location\HasActiveNodesException;
class LocationDeletionService
{
/**
* LocationDeletionService constructor.
*/
public function __construct(
protected LocationRepositoryInterface $repository,
protected NodeRepositoryInterface $nodeRepository
) {
}
/**
* Delete an existing location.
*
* @throws \Pterodactyl\Exceptions\Service\Location\HasActiveNodesException
* @throws HasActiveNodesException
*/
public function handle(Location|int $location): ?int
{
$location = ($location instanceof Location) ? $location->id : $location;
/** @var Location $location */
$location = ($location instanceof Location) ? $location : Location::query()->findOrFail($location);
Assert::integerish($location, 'First argument passed to handle must be numeric or an instance of ' . Location::class . ', received %s.');
$count = $this->nodeRepository->findCountWhere([['location_id', '=', $location]]);
$count = $location->nodes()->count();
if ($count > 0) {
throw new HasActiveNodesException(trans('exceptions.locations.has_nodes'));
}
return $this->repository->delete($location);
return $location->delete();
}
}