Servicio operadores

This commit is contained in:
Juan Pablo Vial
2025-02-24 12:41:13 -03:00
parent a44bd610ad
commit 7fb28cd44c

View File

@ -0,0 +1,157 @@
<?php
namespace Incoviba\Service\Proyecto;
use PDOException;
use Psr\Log\LoggerInterface;
use Incoviba\Common\Ideal;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Common\Implement\Repository\Factory;
use Incoviba\Exception\ServiceAction;
use Incoviba\Model;
use Incoviba\Repository;
use Incoviba\Service;
class Broker extends Ideal\Service
{
public function __construct(LoggerInterface $logger,
protected Repository\Proyecto\Broker $brokerRepository,
protected Repository\Proyecto\Broker\Data $dataRepository, protected Service\Persona $personaService)
{
parent::__construct($logger);
}
/**
* @param array $data
* @return Model\Proyecto\Broker
* @throws ServiceAction\Create
*/
public function add(array $data): Model\Proyecto\Broker
{
try {
$broker = $this->brokerRepository->fetchById($data['rut']);
} catch (EmptyResult) {
$filteredData = $this->brokerRepository->filterData($data);
try {
$broker = $this->brokerRepository->create($filteredData);
$broker = $this->brokerRepository->save($broker);
} catch (PDOException | EmptyResult $exception) {
throw new ServiceAction\Create(__CLASS__, $exception);
}
}
$this->addData($broker, $data);
return $this->process($broker);
}
/**
* @param int $id
* @return Model\Proyecto\Broker
* @throws ServiceAction\Read
*/
public function get(int $id): Model\Proyecto\Broker
{
try {
return $this->process($this->brokerRepository->fetchById($id));
} catch (EmptyResult $exception) {
throw new ServiceAction\Read(__CLASS__, $exception);
}
}
/**
* @return array
*/
public function getAll(): array
{
try {
return array_map([$this, 'process'], $this->brokerRepository->fetchAll());
} catch (EmptyResult) {
return [];
}
}
/**
* @param array $data
* @return Model\Proyecto\Broker
* @throws ServiceAction\Update
*/
public function edit(array $data): Model\Proyecto\Broker
{
try {
$broker = $this->brokerRepository->fetchById($data['id']);
} catch (EmptyResult $exception) {
throw new ServiceAction\Update(__CLASS__, $exception);
}
$filteredData = $this->brokerRepository->filterData($data);
try {
$broker = $this->brokerRepository->edit($broker, $filteredData);
$this->editData($broker, $data);
} catch (PDOException | EmptyResult) {
} finally {
return $this->process($broker);
}
}
/**
* @param int $broker_rut
* @return Model\Proyecto\Broker
* @throws ServiceAction\Delete
*/
public function delete(int $broker_rut): Model\Proyecto\Broker
{
try {
$broker = $this->brokerRepository->fetchById($broker_rut);
} catch (EmptyResult $exception) {
throw new ServiceAction\Delete(__CLASS__, $exception);
}
try {
$this->brokerRepository->remove($broker);
} catch (PDOException | EmptyResult $exception) {
throw new ServiceAction\Delete(__CLASS__, $exception);
} finally {
return $broker;
}
}
protected function process(Model\Proyecto\Broker $broker): Model\Proyecto\Broker
{
$broker->addFactory('data', (new Factory())
->setArgs(['broker_rut' => $broker->rut])
->setCallable([$this->dataRepository, 'fetchByBroker'])
);
return $broker;
}
protected function addData(Model\Proyecto\Broker $broker, array $data): Model\Proyecto\Broker\Data
{
$data['broker_rut'] = $broker->rut;
$filteredData = $this->dataRepository->filterData($data);
if (isset($filteredData['representative_rut'])) {
try {
$this->personaService->getById($filteredData['representative_rut']);
} catch (EmptyResult) {
unset($filteredData['representative_rut']);
}
}
try {
$brokerData = $this->dataRepository->fetchByBroker($broker->rut);
return $this->dataRepository->edit($brokerData, $filteredData);
} catch (EmptyResult) {}
try {
$brokerData = $this->dataRepository->create($filteredData);
return $this->dataRepository->save($brokerData);
} catch (PDOException | EmptyResult $exception) {
throw new ServiceAction\Create(__CLASS__, $exception);
}
}
protected function editData(Model\Proyecto\Broker $broker, array $data): Model\Proyecto\Broker\Data
{
try {
$brokerData = $this->dataRepository->fetchByBroker($broker->rut);
} catch (EmptyResult $exception) {
throw new ServiceAction\Update(__CLASS__, $exception);
}
try {
$data['broker_rut'] = $broker->rut;
$filteredData = $this->dataRepository->filterData($data);
$brokerData = $this->dataRepository->edit($brokerData, $filteredData);
return $this->dataRepository->save($brokerData);
} catch (PDOException | EmptyResult) {
return $brokerData;
}
}
}