Files
oficial/app/src/Service/Inmobiliaria/Cuenta.php
Juan Pablo Vial 8f16f33a1e Cleanup
2025-03-03 14:57:22 -03:00

76 lines
2.5 KiB
PHP

<?php
namespace Incoviba\Service\Inmobiliaria;
use DateTimeImmutable;
use Incoviba\Exception\ServiceAction\Read;
use Psr\Log\LoggerInterface;
use Incoviba\Common\Ideal\Service;
use Incoviba\Common\Implement;
use Incoviba\Model;
use Incoviba\Repository;
class Cuenta extends Service
{
public function __construct(LoggerInterface $logger, protected Repository\Inmobiliaria\Cuenta $cuentaRepository,
protected Repository\Inmobiliaria\Cuenta\Estado $estadoRepository)
{
parent::__construct($logger);
}
/**
* @return array
* @throws Read
*/
public function getAllActive(): array
{
try {
$cuentas = array_map([$this, 'process'], $this->cuentaRepository->fetchAll());
return array_values(array_filter($cuentas, function(Model\Inmobiliaria\Cuenta $cuenta) {
return $cuenta->currentEstado()->active;
}));
} catch (Implement\Exception\EmptyResult $exception) {
throw new Read(__CLASS__, $exception);
}
}
/**
* @param int $inmobiliaria_rut
* @return array
* @throws Read
*/
public function getAllActiveByInmobiliaria(int $inmobiliaria_rut): array
{
try {
$cuentas = array_map([$this, 'process'], $this->cuentaRepository->fetchByInmobiliaria($inmobiliaria_rut));
return array_values(array_filter($cuentas, function(Model\Inmobiliaria\Cuenta $cuenta) {
return $cuenta->currentEstado()->active;
}));
} catch (Implement\Exception\EmptyResult $exception) {
throw new Read(__CLASS__, $exception);
}
}
public function add(array $data): Model\Inmobiliaria\Cuenta
{
$cuenta = $this->cuentaRepository->create($data);
$cuenta = $this->cuentaRepository->save($cuenta);
$dataEstado = [
'cuenta_id' => $cuenta->id,
'fecha' => new DateTimeImmutable(),
'active' => true
];
$estado = $this->estadoRepository->create($dataEstado);
$this->estadoRepository->save($estado);
return $this->process($cuenta);
}
protected function process(Model\Inmobiliaria\Cuenta $cuenta): Model\Inmobiliaria\Cuenta
{
$cuenta->addFactory('estados', (new Implement\Repository\Factory())
->setCallable(function($cuenta_id) {
return $this->estadoRepository->fetchByCuenta($cuenta_id);
})
->setArgs(['cuenta_id' => $cuenta->id]));
return $cuenta;
}
}