Files
oficial/app/src/Service/Persona.php

117 lines
4.4 KiB
PHP
Raw Normal View History

2024-06-18 22:41:03 -04:00
<?php
namespace Incoviba\Service;
2025-05-07 19:24:33 -04:00
use PDOException;
use Psr\Log\LoggerInterface;
2024-06-18 22:41:03 -04:00
use Incoviba\Common\Ideal;
use Incoviba\Common\Implement;
2025-05-07 19:24:33 -04:00
use Incoviba\Exception\ServiceAction\Create;
2025-03-03 14:57:22 -03:00
use Incoviba\Exception\ServiceAction\Read;
2024-06-18 22:41:03 -04:00
use Incoviba\Model;
use Incoviba\Repository;
class Persona extends Ideal\Service
{
2025-01-17 16:56:27 -03:00
public function __construct(LoggerInterface $logger,
protected Repository\Persona $personaRepository,
2025-02-03 22:17:57 -03:00
protected Repository\Persona\Datos $datosPersonaRepository,
protected Repository\Venta\Propietario $propietarioRepository)
2024-06-18 22:41:03 -04:00
{
parent::__construct($logger);
}
2025-03-03 14:57:22 -03:00
/**
* @param int $rut
* @return Model\Persona
2025-05-07 19:24:33 -04:00
* @throws Read|Create
2025-03-03 14:57:22 -03:00
*/
2024-12-03 16:45:20 -03:00
public function getById(int $rut): Model\Persona
2024-06-18 22:41:03 -04:00
{
2025-03-03 14:57:22 -03:00
try {
return $this->process($this->personaRepository->fetchById($rut));
} catch (Implement\Exception\EmptyResult $exception) {
2025-05-07 19:24:33 -04:00
try {
$this->propietarioRepository->fetchById($rut);
return $this->add(compact('rut'));
} catch (Implement\Exception\EmptyResult $exception) {
throw new Read(__CLASS__, $exception);
}
2025-03-03 14:57:22 -03:00
}
2024-06-18 22:41:03 -04:00
}
2025-05-07 19:24:33 -04:00
/**
* @param array $data
* @return Model\Persona
* @throws Create
*/
2024-06-18 22:41:03 -04:00
public function add(array $data): Model\Persona
{
try {
2024-12-03 16:45:20 -03:00
$persona = $this->personaRepository->fetchById($data['rut']);
2024-06-18 22:41:03 -04:00
} catch (Implement\Exception\EmptyResult) {
2025-02-03 22:17:57 -03:00
try {
$propietario = $this->propietarioRepository->fetchById($data['rut']);
$data['nombres'] = $propietario->nombres;
$data['apellido_paterno'] = $propietario->apellidos['paterno'];
$data['apellido_materno'] = $propietario->apellidos['materno'];
$data['direccion_id'] = $propietario->datos->direccion->id;
2025-05-07 19:24:33 -04:00
if (isset($propietario->datos?->email)) {
$data['email'] = $propietario->datos->email;
}
if (isset($propietario->datos?->telefono)) {
$data['telefono'] = $propietario->datos->telefono;
}
2025-02-03 22:17:57 -03:00
} catch (Implement\Exception\EmptyResult) {}
2024-06-18 22:41:03 -04:00
$persona = $this->personaRepository->create($data);
2025-05-07 19:24:33 -04:00
try {
$persona = $this->personaRepository->save($persona);
} catch (PDOException $exception) {
throw new Create(__CLASS__, $exception);
}
2024-06-18 22:41:03 -04:00
}
2025-02-03 22:17:57 -03:00
if (isset($data['direccion_id']) or isset($data['email']) or isset($data['telefono'])) {
2024-06-18 22:41:03 -04:00
$datosData = ['persona_rut' => $persona->rut];
2025-02-03 22:17:57 -03:00
if (isset($data['direccion_id'])) {
$datosData['direccion_id'] = $data['direccion_id'];
}
2024-06-18 22:41:03 -04:00
if (isset($data['email'])) {
$datosData['email'] = $data['email'];
}
if (isset($data['telefono'])) {
$datosData['telefono'] = $data['telefono'];
}
try {
$datos = $this->datosPersonaRepository->fetchByPersona($persona->rut);
$this->datosPersonaRepository->edit($datos, $data);
} catch (Implement\Exception\EmptyResult) {
$datos = $this->datosPersonaRepository->create($datosData);
2025-05-07 19:24:33 -04:00
try {
$this->datosPersonaRepository->save($datos);
} catch (PDOException $exception) {
throw new Create(__CLASS__, $exception);
}
2024-06-18 22:41:03 -04:00
}
}
return $this->process($persona);
}
2024-12-03 17:35:29 -03:00
public function edit(Model\Persona $persona, array $data): Model\Persona
{
$filteredData = $this->personaRepository->filterData($data);
try {
$datosData = $this->datosPersonaRepository->filterData($data);
$this->datosPersonaRepository->edit($persona->datos(), $datosData);
return $this->personaRepository->edit($persona, $filteredData);
} catch (Implement\Exception\EmptyResult) {
return $persona;
}
}
2024-06-18 22:41:03 -04:00
protected function process(Model\Persona $persona): Model\Persona
{
$persona->addFactory('datos', (new Implement\Repository\Factory())
->setCallable([$this->datosPersonaRepository, 'fetchByPersona'])
->setArgs(['persona_rut' => $persona->rut]));
return $persona;
}
}