. */ namespace SP\Domain\Client\Services; use SP\Core\Application; use SP\Domain\Account\Ports\AccountFilterBuilder; use SP\Domain\Client\Models\Client as ClientModel; use SP\Domain\Client\Ports\ClientRepository; use SP\Domain\Client\Ports\ClientService; use SP\Domain\Common\Models\Simple; use SP\Domain\Common\Services\Service; use SP\Domain\Common\Services\ServiceException; use SP\Domain\Core\Dtos\ItemSearchDto; use SP\Domain\Core\Exceptions\ConstraintException; use SP\Domain\Core\Exceptions\QueryException; use SP\Domain\Core\Exceptions\SPException; use SP\Infrastructure\Common\Repositories\DuplicatedItemException; use SP\Infrastructure\Common\Repositories\NoSuchItemException; use SP\Infrastructure\Database\QueryResult; use function SP\__u; /** * Class Client * * @template T of ClientModel */ final class Client extends Service implements ClientService { public function __construct( Application $application, private readonly ClientRepository $clientRepository, private readonly AccountFilterBuilder $accountFilterUser ) { parent::__construct($application); } /** * @param ItemSearchDto $itemSearchData * * @return QueryResult */ public function search(ItemSearchDto $itemSearchData): QueryResult { return $this->clientRepository->search($itemSearchData); } /** * @param int $id * @return ClientModel * @throws NoSuchItemException * @throws SPException */ public function getById(int $id): ClientModel { $result = $this->clientRepository->getById($id); if ($result->getNumRows() === 0) { throw new NoSuchItemException(__u('Client not found'), SPException::INFO); } return $result->getData(ClientModel::class); } /** * Returns the item for given name * * @param string $name * @return ClientModel|null * @throws ConstraintException * @throws NoSuchItemException * @throws QueryException * @throws SPException */ public function getByName(string $name): ?ClientModel { $result = $this->clientRepository->getByName($name); if ($result->getNumRows() === 0) { throw new NoSuchItemException(__u('Client not found'), SPException::INFO); } return $result->getData(ClientModel::class); } /** * @param int $id * * @return ClientService * @throws ConstraintException * @throws NoSuchItemException * @throws QueryException */ public function delete(int $id): ClientService { if ($this->clientRepository->delete($id)->getAffectedNumRows() === 0) { throw new NoSuchItemException(__u('Client not found'), SPException::INFO); } return $this; } /** * @param int[] $ids * * @return void * @throws ConstraintException * @throws QueryException * @throws ServiceException */ public function deleteByIdBatch(array $ids): void { if ($this->clientRepository->deleteByIdBatch($ids)->getAffectedNumRows() === 0) { throw new ServiceException( __u('Error while deleting the clients'), SPException::WARNING ); } } /** * @param ClientModel $client * * @return int * @throws SPException * @throws DuplicatedItemException */ public function create(ClientModel $client): int { return $this->clientRepository->create($client)->getLastId(); } /** * @param ClientModel $client * * @return void * @throws ConstraintException * @throws DuplicatedItemException * @throws QueryException */ public function update(ClientModel $client): void { $this->clientRepository->update($client); } /** * Get all items from the service's repository * * @return array */ public function getAll(): array { return $this->clientRepository->getAll()->getDataAsArray(ClientModel::class); } /** * Returns all clients visible for a given user * * @return Simple[] * @throws QueryException * @throws ConstraintException * @throws SPException */ public function getAllForUser(): array { return $this->clientRepository->getAllForFilter($this->accountFilterUser)->getDataAsArray(); } }