86 lines
2.6 KiB
PHP
86 lines
2.6 KiB
PHP
![]() |
<?php
|
||
|
namespace Incoviba\Service\Proyecto\Broker;
|
||
|
|
||
|
use Incoviba\Exception\ServiceAction\Update;
|
||
|
use PDOException;
|
||
|
use Psr\Log\LoggerInterface;
|
||
|
use Incoviba\Common\Ideal;
|
||
|
use Incoviba\Common\Implement;
|
||
|
use Incoviba\Exception\ServiceAction;
|
||
|
use Incoviba\Model;
|
||
|
use Incoviba\Repository;
|
||
|
class Contract extends Ideal\Service
|
||
|
{
|
||
|
public function __construct(LoggerInterface $logger,
|
||
|
protected Repository\Proyecto\Broker\Contract $contractRepository)
|
||
|
{
|
||
|
parent::__construct($logger);
|
||
|
}
|
||
|
|
||
|
public function getAll(): array
|
||
|
{
|
||
|
try {
|
||
|
return $this->contractRepository->fetchAll();
|
||
|
} catch (Implement\Exception\EmptyResult) {
|
||
|
return [];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @throws ServiceAction\Read
|
||
|
*/
|
||
|
public function getById(int $id): Model\Proyecto\Broker\Contract
|
||
|
{
|
||
|
try {
|
||
|
return $this->contractRepository->fetchById($id);
|
||
|
} catch (Implement\Exception\EmptyResult $exception) {
|
||
|
throw new ServiceAction\Read(__CLASS__, $exception);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @throws ServiceAction\Create
|
||
|
*/
|
||
|
public function add(array $data): Model\Proyecto\Broker\Contract
|
||
|
{
|
||
|
try {
|
||
|
return $this->contractRepository->fetchActiveByProjectAndBroker($data['proyecto_id'], $data['broker_rut']);
|
||
|
} catch (Implement\Exception\EmptyResult) {}
|
||
|
|
||
|
try {
|
||
|
$filteredData = $this->contractRepository->filterData($data);
|
||
|
$contract = $this->contractRepository->create($filteredData);
|
||
|
return $this->contractRepository->save($contract);
|
||
|
} catch (Implement\Exception\EmptyResult $exception) {
|
||
|
throw new ServiceAction\Create(__CLASS__, $exception);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @throws Update
|
||
|
*/
|
||
|
public function edit(Model\Proyecto\Broker\Contract $contract, array $data): Model\Proyecto\Broker\Contract
|
||
|
{
|
||
|
try {
|
||
|
$filteredData = $this->contractRepository->filterData($data);
|
||
|
return $this->contractRepository->edit($contract, $filteredData);
|
||
|
} catch (PDOException | Implement\Exception\EmptyResult) {
|
||
|
throw new ServiceAction\Update(__CLASS__);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @throws ServiceAction\Delete
|
||
|
*/
|
||
|
public function delete(int $id): Model\Proyecto\Broker\Contract
|
||
|
{
|
||
|
try {
|
||
|
$contract = $this->contractRepository->fetchById($id);
|
||
|
$this->contractRepository->remove($contract->id);
|
||
|
return $contract;
|
||
|
} catch (PDOException | Implement\Exception\EmptyResult $exception) {
|
||
|
throw new ServiceAction\Delete(__CLASS__, $exception);
|
||
|
}
|
||
|
}
|
||
|
}
|