Manejo de excepciones
This commit is contained in:
@ -1,7 +1,9 @@
|
||||
<?php
|
||||
namespace Incoviba\Service\Venta;
|
||||
|
||||
use Incoviba\Exception\ServiceAction\Read;
|
||||
use PDOException;
|
||||
use Incoviba\Common\Implement\Exception\EmptyResult;
|
||||
use Incoviba\Exception\ServiceAction\{Create, Read, Update};
|
||||
use Incoviba\Repository;
|
||||
use Incoviba\Model;
|
||||
|
||||
@ -11,21 +13,49 @@ class PropiedadUnidad
|
||||
protected Repository\Venta\Unidad $unidadRepository,
|
||||
protected Precio $precioService) {}
|
||||
|
||||
/**
|
||||
* @param int $unidad_id
|
||||
* @return Model\Venta\PropiedadUnidad
|
||||
* @throws Read
|
||||
*/
|
||||
public function getById(int $unidad_id): Model\Venta\PropiedadUnidad
|
||||
{
|
||||
return $this->process($this->propiedadUnidadRepository->fetchById($unidad_id));
|
||||
try {
|
||||
return $this->process($this->propiedadUnidadRepository->fetchById($unidad_id));
|
||||
} catch (EmptyResult $exception) {
|
||||
throw new Read(__CLASS__, $exception);
|
||||
}
|
||||
}
|
||||
public function getByVenta(int $venta_id): array
|
||||
{
|
||||
return array_map([$this, 'process'], $this->propiedadUnidadRepository->fetchByVenta($venta_id));
|
||||
try {
|
||||
return array_map([$this, 'process'], $this->propiedadUnidadRepository->fetchByVenta($venta_id));
|
||||
} catch (EmptyResult) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
public function getByPropiedad(int $propiedad_id): array
|
||||
{
|
||||
return array_map([$this, 'process'], $this->propiedadUnidadRepository->fetchByPropiedad($propiedad_id));
|
||||
try {
|
||||
return array_map([$this, 'process'], $this->propiedadUnidadRepository->fetchByPropiedad($propiedad_id));
|
||||
} catch (EmptyResult) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @return Model\Venta\PropiedadUnidad
|
||||
* @throws Create
|
||||
* @throws Read
|
||||
*/
|
||||
public function add(array $data): Model\Venta\PropiedadUnidad
|
||||
{
|
||||
$unidad = $this->unidadRepository->fetchById($data['unidad']);
|
||||
try {
|
||||
$unidad = $this->unidadRepository->fetchById($data['unidad']);
|
||||
} catch (EmptyResult $exception) {
|
||||
throw new Read(__CLASS__, $exception);
|
||||
}
|
||||
$temp = json_decode(json_encode($unidad), JSON_OBJECT_AS_ARRAY);
|
||||
$columnMap = [
|
||||
'proyecto_tipo_unidad' => 'pt'
|
||||
@ -38,11 +68,27 @@ class PropiedadUnidad
|
||||
$temp['propiedad'] = $data['propiedad'];
|
||||
$temp['valor'] = $data['valor'];
|
||||
$pu = $this->propiedadUnidadRepository->create($temp);
|
||||
return $this->process($this->propiedadUnidadRepository->save($pu));
|
||||
try {
|
||||
return $this->process($this->propiedadUnidadRepository->save($pu));
|
||||
} catch (PDOException $exception) {
|
||||
throw new Create(__CLASS__, $exception);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Model\Venta\PropiedadUnidad $propiedadUnidad
|
||||
* @param array $data
|
||||
* @return Model\Venta\PropiedadUnidad
|
||||
* @throws EmptyResult
|
||||
* @throws Update
|
||||
*/
|
||||
public function edit(Model\Venta\PropiedadUnidad $propiedadUnidad, array $data): Model\Venta\PropiedadUnidad
|
||||
{
|
||||
return $this->process($this->propiedadUnidadRepository->edit($propiedadUnidad, $data));
|
||||
try {
|
||||
return $this->process($this->propiedadUnidadRepository->edit($propiedadUnidad, $data));
|
||||
} catch (PDOException $exception) {
|
||||
throw new Update(__CLASS__, $exception);
|
||||
}
|
||||
}
|
||||
|
||||
protected function process(Model\Venta\PropiedadUnidad $unidad): Model\Venta\PropiedadUnidad
|
||||
|
Reference in New Issue
Block a user