pelican-panel-mirror/app/Services/Nests/NestDeletionService.php
2024-03-12 22:39:16 -04:00

35 lines
944 B
PHP

<?php
namespace App\Services\Nests;
use App\Contracts\Repository\NestRepositoryInterface;
use App\Exceptions\Service\HasActiveServersException;
use App\Contracts\Repository\ServerRepositoryInterface;
class NestDeletionService
{
/**
* NestDeletionService constructor.
*/
public function __construct(
protected ServerRepositoryInterface $serverRepository,
protected NestRepositoryInterface $repository
) {
}
/**
* Delete a nest from the system only if there are no servers attached to it.
*
* @throws \App\Exceptions\Service\HasActiveServersException
*/
public function handle(int $nest): int
{
$count = $this->serverRepository->findCountWhere([['nest_id', '=', $nest]]);
if ($count > 0) {
throw new HasActiveServersException(trans('exceptions.nest.delete_has_servers'));
}
return $this->repository->delete($nest);
}
}