Rutas API Contratos
This commit is contained in:
@ -1,7 +1,9 @@
|
||||
<?php
|
||||
namespace Incoviba\Service\Proyecto\Broker;
|
||||
|
||||
use Incoviba\Exception\ServiceAction\Update;
|
||||
use DateTimeInterface;
|
||||
use DateTimeImmutable;
|
||||
use DateMalformedStringException;
|
||||
use PDOException;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Incoviba\Common\Ideal;
|
||||
@ -9,10 +11,12 @@ 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)
|
||||
protected Repository\Proyecto\Broker\Contract $contractRepository,
|
||||
protected Repository\Proyecto\Broker\Contract\State $stateRepository)
|
||||
{
|
||||
parent::__construct($logger);
|
||||
}
|
||||
@ -20,11 +24,19 @@ class Contract extends Ideal\Service
|
||||
public function getAll(): array
|
||||
{
|
||||
try {
|
||||
return $this->contractRepository->fetchAll();
|
||||
return array_map([$this, 'process'], $this->contractRepository->fetchAll());
|
||||
} catch (Implement\Exception\EmptyResult) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
public function getByBroker(int $broker_rut): array
|
||||
{
|
||||
try {
|
||||
return array_map([$this, 'broker'], $this->contractRepository->fetchByBroker($broker_rut));
|
||||
} catch (Implement\Exception\EmptyResult $exception) {
|
||||
throw new ServiceAction\Read(__CLASS__, $exception);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ServiceAction\Read
|
||||
@ -32,7 +44,7 @@ class Contract extends Ideal\Service
|
||||
public function getById(int $id): Model\Proyecto\Broker\Contract
|
||||
{
|
||||
try {
|
||||
return $this->contractRepository->fetchById($id);
|
||||
return $this->process($this->contractRepository->fetchById($id));
|
||||
} catch (Implement\Exception\EmptyResult $exception) {
|
||||
throw new ServiceAction\Read(__CLASS__, $exception);
|
||||
}
|
||||
@ -44,26 +56,36 @@ class Contract extends Ideal\Service
|
||||
public function add(array $data): Model\Proyecto\Broker\Contract
|
||||
{
|
||||
try {
|
||||
return $this->contractRepository->fetchActiveByProjectAndBroker($data['proyecto_id'], $data['broker_rut']);
|
||||
return $this->process($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);
|
||||
$contract = $this->contractRepository->save($contract);
|
||||
$type = Model\Proyecto\Broker\Contract\State\Type::ACTIVE;
|
||||
$date = new DateTimeImmutable();
|
||||
if (isset($data['date'])) {
|
||||
try {
|
||||
$date = new DateTimeImmutable($data['date']);
|
||||
} catch (DateMalformedStringException) {}
|
||||
}
|
||||
$state = $this->stateRepository->create(['contract_id' => $contract->id, 'date' => $date, 'type' => $type]);
|
||||
$this->stateRepository->save($state);
|
||||
return $this->process($contract);
|
||||
} catch (Implement\Exception\EmptyResult $exception) {
|
||||
throw new ServiceAction\Create(__CLASS__, $exception);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Update
|
||||
* @throws ServiceAction\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);
|
||||
return $this->process($this->contractRepository->edit($contract, $filteredData));
|
||||
} catch (PDOException | Implement\Exception\EmptyResult) {
|
||||
throw new ServiceAction\Update(__CLASS__);
|
||||
}
|
||||
@ -82,4 +104,30 @@ class Contract extends Ideal\Service
|
||||
throw new ServiceAction\Delete(__CLASS__, $exception);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ServiceAction\Update
|
||||
*/
|
||||
public function inactive(Model\Proyecto\Broker\Contract $contract, DateTimeInterface $date = new DateTimeImmutable()): Model\Proyecto\Broker\Contract
|
||||
{
|
||||
try {
|
||||
$type = Model\Proyecto\Broker\Contract\State\Type::INACTIVE;
|
||||
$state = $this->stateRepository->create(['contract_id' => $contract->id, 'date' => $date, 'type' => $type]);
|
||||
$this->stateRepository->save($state);
|
||||
return $this->process($contract);
|
||||
} catch (PDOException | Implement\Exception\EmptyResult $exception) {
|
||||
throw new ServiceAction\Update(__CLASS__, $exception);
|
||||
}
|
||||
}
|
||||
|
||||
protected function process(Model\Proyecto\Broker\Contract $contract): Model\Proyecto\Broker\Contract
|
||||
{
|
||||
$contract->addFactory('states', (new Implement\Repository\Factory())
|
||||
->setCallable([$this->stateRepository, 'fetchByContract'])
|
||||
->setArgs(['contract_id' => $contract->id]));
|
||||
$contract->addFactory('currentState', (new Implement\Repository\Factory())
|
||||
->setCallable([$this->stateRepository, 'fetchActiveByContract'])
|
||||
->setArgs(['contract_id' => $contract->id]));
|
||||
return $contract;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user