<?php
namespace App\Controller\API;
use App\Controller\BaseController;
use App\Entity\App\AreaGobierno;
use App\Entity\App\Tramite;
use App\Entity\Log\LogInteraction;
use App\Services\API\ImagenService;
use App\Services\API\TramiteService;
use App\Utils\APIResponse;
use App\Utils\CommonFunctions;
use Exception;
use OpenApi\Annotations as OA;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\Routing\Annotation\Route;
class APIController extends BaseController
{
/**
* @Route("/imagen/{dir}/{filename}", name="get_imagen_by_filename", methods={"GET"})
* @param Request $request
* @param ImagenService $imagenService
* @param $dir
* @param $filename
* @return Response
*
* @OA\Get(
* path="/api/v1/imagen/{dir}/{filename}",
* summary="Obtener una imagen por filename",
* @OA\Response(
* response=200,
* description="Response",
* @OA\MediaType(
* mediaType="image/jpg"
* ),
* @OA\MediaType(
* mediaType="image/png"
* ),
* @OA\MediaType(
* mediaType="image/svg+xml"
* )
* )
* )
*/
public function imagenAction(Request $request, ImagenService $imagenService, $dir, $filename)
{
try {
return $this->file($imagenService->getAbsolutePathImagen($dir, $filename), $filename, ResponseHeaderBag::DISPOSITION_INLINE);
} catch (Exception $e) {
$result = null;
$statusCode = APIResponse::$INTERNAL_ERROR;
$status = APIResponse::$INTERNAL_ERROR;
$errors = array($e->getMessage());
$errorInfo = CommonFunctions::getErrorException($e);
$this->logInteractionService->addErrorLog(
LogInteraction::$LIST,
CommonFunctions::getClassMethod(__METHOD__),
$e->getMessage(),
$errorInfo
);
return $this->generateJsonResponse($result, $statusCode, $status, $errors);
}
}
/**
* @Route("/archivo/{dir}/{filename}", name="get_archivo_by_filename", methods={"GET"})
* @param Request $request
* @param TramiteService $tramiteService
* @param $dir
* @param $filename
* @return Response
*
* @OA\Get(
* path="/api/v1/archivo/{dir}/{filename}",
* summary="Obtener una imagen por filename",
* @OA\Response(
* response=200,
* description="Response",
* @OA\MediaType(
* mediaType="image/jpg"
* ),
* @OA\MediaType(
* mediaType="image/png"
* ),
* @OA\MediaType(
* mediaType="image/svg+xml"
* )
* )
* )
*/
public function archivoAction(Request $request, TramiteService $tramiteService, $dir, $filename)
{
try {
return $this->file($tramiteService->getAbsolutePathArchivo($dir, $filename), $filename, ResponseHeaderBag::DISPOSITION_INLINE);
} catch (Exception $e) {
$result = null;
$statusCode = APIResponse::$INTERNAL_ERROR;
$status = APIResponse::$INTERNAL_ERROR;
$errors = array($e->getMessage());
$errorInfo = CommonFunctions::getErrorException($e);
$this->logInteractionService->addErrorLog(
LogInteraction::$LIST,
CommonFunctions::getClassMethod(__METHOD__),
$e->getMessage(),
$errorInfo
);
return $this->generateJsonResponse($result, $statusCode, $status, $errors);
}
}
/**
* @Route("/rutas", name="get_rutas", methods={"GET"})
* @param Request $request
* @return Response
*
* @OA\Get(
* path="/api/v1/rutas",
* summary="Obtener las rutas",
* @OA\Response(
* response=200,
* description="Response",
* @OA\MediaType(
* mediaType="application/json",
* @OA\Schema(
* @OA\Property(property="status", type="integer"),
* @OA\Property(property="errors", type="array", @OA\Items(type="string")),
* @OA\Property(property="content", type="object")
* )
* )
* )
* )
*/
public function rutasAction(Request $request)
{
$statusCode = APIResponse::$SUCCESS;
$status = APIResponse::$SUCCESS;
$errors = array();
$result = array('tramites-y-servicios' => array(), 'areas-gobierno' => array());
try {
$frontUrl = CommonFunctions::getParameter('FRONT_URL');
$frontUrl = CommonFunctions::getPathOrURLWithoutSlash($frontUrl);
$result['tramites-y-servicios'] = $this->getTramitesRutas($frontUrl);
$result['areas-gobierno'] = $this->getAreasRutas($frontUrl);
} catch (Exception $e) {
$statusCode = APIResponse::$INTERNAL_ERROR;
$status = APIResponse::$INTERNAL_ERROR;
$errors = array($e->getMessage());
$errorInfo = CommonFunctions::getErrorException($e);
$result = null;
$this->logInteractionService->addErrorLog(
LogInteraction::$LIST,
CommonFunctions::getClassMethod(__METHOD__),
$e->getMessage(),
$errorInfo
);
}
return $this->generateJsonResponse($result, $statusCode, $status, $errors);
}
private function getTramitesRutas($frontUrl)
{
$rutas = array();
$tramites = $this->managerRegistry->getManager()->getRepository(Tramite::class)->findAll();
foreach ($tramites as $tramite) {
$item = array(
'nombre' => $tramite->getNombre(),
'url' => $tramite->getLink()
);
$cats = array();
foreach ($tramite->getGrupo()->getBreadCrumb() as $categoria) {
$cats[] = array(
'nombre' => $categoria->getNombre(),
'url' => $frontUrl . '/tramites-y-servicios/' . $categoria->getId()
);
}
$item['breadcrumb'] = $cats;
$rutas[] = $item;
}
return $rutas;
}
private function getAreasRutas($frontUrl)
{
$rutas = array();
$areas = $this->managerRegistry->getManager()->getRepository(AreaGobierno::class)->findAll();
foreach ($areas as $area) {
$item = array(
'nombre' => $area->getNombre(),
'url' => $area->getLink(),
'breadcrumb' => array(
'nombre' => 'Inicio',
'url' => $frontUrl
)
);
$rutas[] = $item;
}
return $rutas;
}
}