misc_pterodactyl-panel/app/Services/Locations/LocationDeletionService.php

64 lines
1.8 KiB
PHP
Raw Normal View History

<?php
2017-09-26 02:43:01 +00:00
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
2017-09-26 02:43:01 +00:00
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Services\Locations;
use Webmozart\Assert\Assert;
2018-01-04 03:14:53 +00:00
use Pterodactyl\Models\Location;
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
use Pterodactyl\Exceptions\Service\Location\HasActiveNodesException;
class LocationDeletionService
{
/**
* @var \Pterodactyl\Contracts\Repository\NodeRepositoryInterface
*/
protected $nodeRepository;
/**
* @var \Pterodactyl\Contracts\Repository\LocationRepositoryInterface
*/
protected $repository;
/**
* LocationDeletionService constructor.
*/
public function __construct(
LocationRepositoryInterface $repository,
NodeRepositoryInterface $nodeRepository
) {
$this->nodeRepository = $nodeRepository;
$this->repository = $repository;
}
/**
* Delete an existing location.
*
2018-01-04 03:14:53 +00:00
* @param int|\Pterodactyl\Models\Location $location
2021-01-23 20:33:34 +00:00
*
* @return int|null
*
* @throws \Pterodactyl\Exceptions\Service\Location\HasActiveNodesException
*/
public function handle($location)
{
2018-01-04 03:14:53 +00:00
$location = ($location instanceof Location) ? $location->id : $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]]);
if ($count > 0) {
throw new HasActiveNodesException(trans('exceptions.locations.has_nodes'));
}
return $this->repository->delete($location);
}
}