<?php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use FOS\RestBundle\Controller\Annotations as Rest;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use App\Entity\Interfaces\EntityInterface;
use App\Entity\Solicitacao;
use App\Form\SolicitacaoType;
use App\Repository\SolicitacaoRepository;
use App\Service\SolicitacaoService;
use App\Service\NotificationService;
use Psr\Log\LoggerInterface;
/**
* @Route("/api", name="api_")
*/
class SolicitacaoController extends AbstractAppController
{
private $notificationService;
public function __construct(LoggerInterface $logger, SolicitacaoRepository $repository, SolicitacaoService $service, NotificationService $notificationService)
{
$this->entity = new Solicitacao();
$this->repository = $repository;
$this->formType = SolicitacaoType::class;
$this->service = $service;
$this->logger = $logger;
$this->notificationService = $notificationService;
}
/**
* @Rest\Get("/solicitacao")
*/
public function list(): Response
{
return parent::list();
}
/**
* @Rest\Get("/solicitacao/{selector}")
* @ParamConverter("entity", class="App\Entity\Solicitacao")
*/
public function show(EntityInterface $entity): Response
{
return parent::show($entity);
}
/**
* @Rest\Post("/solicitacao")
*/
public function new(Request $request): Response
{
$entity = $this->entity;
$entity = $this->preHydrateEntity($entity);
$form = $this->createForm($this->formType, $entity);
$data = json_decode($request->getContent(), true);
$form->submit($data);
if ($form->isSubmitted() && $form->isValid()) {
try {
$this->service->criar($entity);
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
return $this->handleView($this->view($e->getMessage(), Response::HTTP_BAD_REQUEST));
}
return $this->handleView($this->view([], Response::HTTP_CREATED));
}
return $this->handleView($this->view($form->getErrors(), Response::HTTP_BAD_REQUEST));
}
/**
* @Rest\Put("/solicitacao/{selector}/aprovar")
* @ParamConverter("entity", class="App\Entity\Solicitacao")
*/
public function aprovar(Solicitacao $entity): Response
{
try {
$this->service->aprovar($entity, $this->getUser());
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
return $this->handleView($this->view($e->getMessage(), Response::HTTP_BAD_REQUEST));
}
$this->notificationService->sendDownloadStatus($entity->getDispositivo());
return $this->handleView($this->view($entity, Response::HTTP_OK));
}
/**
* @Rest\Put("/solicitacao/{selector}/recusar")
* @ParamConverter("entity", class="App\Entity\Solicitacao")
*/
public function recusar(Solicitacao $entity): Response
{
try {
$this->service->recusar($entity, $this->getUser());
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
return $this->handleView($this->view($e->getMessage(), Response::HTTP_BAD_REQUEST));
}
$this->notificationService->sendDownloadStatus($entity->getDispositivo());
return $this->handleView($this->view($entity, Response::HTTP_OK));
}
/**
* @Rest\Get("/solicitacao_pendente")
*/
public function listPendentes(): Response
{
$criteria = ['status' => Solicitacao::PENDENTE];
$orderBy = ['dataHoraSolicitacao' => 'DESC'];
return $this->handleView($this->view($this->repository->findByUser($this->getUser(), $criteria, $orderBy)));
}
/**
* @Rest\Get("/solicitacao_aprovada_ultima")
*/
public function listAprovadasUltimas(): Response
{
$criteria = ['status' => [Solicitacao::APROVADA, Solicitacao::PRE_APROVADA]];
$orderBy = ['dataHoraStatus' => 'DESC'];
$limit = 10;
return $this->handleView($this->view($this->repository->findByUser($this->getUser(), $criteria, $orderBy, $limit)));
}
}