Servicio Contratos
This commit is contained in:
85
app/src/Service/Proyecto/Broker/Contract.php
Normal file
85
app/src/Service/Proyecto/Broker/Contract.php
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
<?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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user