100 lines
4.1 KiB
PHP
100 lines
4.1 KiB
PHP
<?php
|
|
namespace Incoviba\Controller\API\Proyectos;
|
|
|
|
use Incoviba\Common\Implement\Exception\EmptyRedis;
|
|
use Incoviba\Common\Implement\Exception\EmptyResult;
|
|
use Incoviba\Controller\API\emptyBody;
|
|
use Incoviba\Controller\API\withJson;
|
|
use Incoviba\Controller\withRedis;
|
|
use Incoviba\Repository;
|
|
use Incoviba\Service;
|
|
use Psr\Http\Message\ResponseInterface;
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
class EstadosProyectos
|
|
{
|
|
use withJson, emptyBody, withRedis;
|
|
public function byProyecto(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService,
|
|
Repository\Proyecto\EstadoProyecto $estadoProyectoRepository, int $proyecto_id): ResponseInterface
|
|
{
|
|
$output = [
|
|
'proyecto_id' => $proyecto_id,
|
|
'estados' => []
|
|
];
|
|
$redisKey = "estados:proyecto:{$proyecto_id}";
|
|
try {
|
|
$output['estados'] = $this->fetchRedis($redisService, $redisKey);
|
|
} catch (EmptyRedis) {
|
|
try {
|
|
$output['estados'] = $estadoProyectoRepository->fetchByProyecto($proyecto_id);
|
|
$this->saveRedis($redisService, $redisKey, $output['estados']);
|
|
} catch (EmptyResult) {
|
|
return $this->emptyBody($response);
|
|
}
|
|
}
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function currentByProyecto(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService,
|
|
Repository\Proyecto\EstadoProyecto $estadoProyectoRepository, int $proyecto_id): ResponseInterface
|
|
{
|
|
$output = [
|
|
'proyecto_id' => $proyecto_id,
|
|
'estado' => null
|
|
];
|
|
$redisKey = "estado:proyecto:{$proyecto_id}";
|
|
try {
|
|
$output['estado'] = $this->fetchRedis($redisService, $redisKey);
|
|
} catch (EmptyRedis) {
|
|
try {
|
|
$output['estado'] = $estadoProyectoRepository->fetchCurrentByProyecto($proyecto_id);
|
|
$this->saveRedis($redisService, $redisKey, $output['estado']);
|
|
} catch (EmptyResult) {
|
|
return $this->emptyBody($response);
|
|
}
|
|
}
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function firstByProyecto(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService,
|
|
Repository\Proyecto\EstadoProyecto $estadoProyectoRepository, int $proyecto_id): ResponseInterface
|
|
{
|
|
$output = [
|
|
'proyecto_id' => $proyecto_id,
|
|
'estado' => null
|
|
];
|
|
$redisKey = "estado:proyecto:{$proyecto_id}:first";
|
|
try {
|
|
$output['estado'] = $this->fetchRedis($redisService, $redisKey);
|
|
} catch (EmptyRedis) {
|
|
try {
|
|
$output['estado'] = $estadoProyectoRepository->fetchFirstByProyecto($proyecto_id);
|
|
$this->saveRedis($redisService, $redisKey, $output['estado']);
|
|
} catch (EmptyResult) {
|
|
return $this->emptyBody($response);
|
|
}
|
|
}
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function recepcionByProyecto(ServerRequestInterface $request, ResponseInterface $response,
|
|
Service\Redis $redisService,
|
|
Repository\Proyecto\EstadoProyecto $estadoProyectoRepository,
|
|
int $proyecto_id): ResponseInterface
|
|
{
|
|
$output = [
|
|
'proyecto_id' => $proyecto_id,
|
|
'estado' => null
|
|
];
|
|
$redisKey = "recepcion:proyecto:{$proyecto_id}";
|
|
try {
|
|
$output['estado'] = $this->fetchRedis($redisService, $redisKey);
|
|
} catch (EmptyRedis) {
|
|
try {
|
|
$output['estado'] = $estadoProyectoRepository->fetchRecepcionByProyecto($proyecto_id);
|
|
$this->saveRedis($redisService, $redisKey, $output['estado']);
|
|
} catch (EmptyResult) {
|
|
return $this->emptyBody($response);
|
|
}
|
|
}
|
|
return $this->withJson($response, $output);
|
|
}
|
|
}
|