feature/cierres (#30)
Reservas agregar y aprobar Co-authored-by: Juan Pablo Vial <jpvialb@incoviba.cl> Reviewed-on: #30
This commit is contained in:
49
app/src/Service/Direccion.php
Normal file
49
app/src/Service/Direccion.php
Normal file
@ -0,0 +1,49 @@
|
||||
<?php
|
||||
namespace Incoviba\Service;
|
||||
|
||||
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 Direccion extends Ideal\Service
|
||||
{
|
||||
public function __construct(LoggerInterface $logger, protected Repository\Direccion $direccionRepository)
|
||||
{
|
||||
parent::__construct($logger);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @return Model\Direccion
|
||||
* @throws ServiceAction\Create
|
||||
*/
|
||||
public function add(array $data): Model\Direccion
|
||||
{
|
||||
$dataMap = [
|
||||
'street' => 'calle',
|
||||
'number' => 'numero',
|
||||
'comuna_id' => 'comuna',
|
||||
];
|
||||
foreach ($data as $key => $value) {
|
||||
if (array_key_exists($key, $dataMap)) {
|
||||
$data[$dataMap[$key]] = $value;
|
||||
unset($data[$key]);
|
||||
}
|
||||
}
|
||||
try {
|
||||
return $this->direccionRepository->fetchByCalleAndNumeroAndExtraAndComuna($data['calle'], $data['numero'], $data['extra'], $data['comuna']);
|
||||
} catch (Implement\Exception\EmptyResult) {
|
||||
$filteredData = $this->direccionRepository->filterData($data);
|
||||
$direccion = $this->direccionRepository->create($filteredData);
|
||||
try {
|
||||
return $this->direccionRepository->save($direccion);
|
||||
} catch (PDOException $exception) {
|
||||
throw new ServiceAction\Create(__CLASS__, $exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user