src/Controller/RestController.php line 161

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Admin;
  4. use App\Entity\Headquarter;
  5. use App\Entity\UserNotification;
  6. use App\Entity\Role;
  7. use App\Entity\Survey;
  8. use App\Entity\User;
  9. use App\Entity\UserDevice;
  10. use App\Entity\UserTimesheet;
  11. use App\Serializer\API\UserTimesheetSerializer;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use PhpOffice\PhpSpreadsheet\IOFactory;
  14. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  15. use Symfony\Component\DependencyInjection\ContainerInterface;
  16. use Symfony\Component\HttpFoundation\HeaderBag;
  17. use Symfony\Component\HttpFoundation\JsonResponse;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\HttpFoundation\RequestStack;
  20. use Symfony\Component\HttpFoundation\Response;
  21. use Symfony\Component\HttpFoundation\StreamedResponse;
  22. use Symfony\Component\Uid\Uuid;
  23. use Symfony\Component\Validator\Constraints\Collection;
  24. use Symfony\Component\Validator\Validation;
  25. use Symfony\Contracts\Translation\TranslatorInterface;
  26. class RestController
  27. {
  28.     /** @var ContainerInterface */
  29.     protected $container;
  30.     protected EntityManagerInterface $em;
  31.     /** @var Request */
  32.     protected $request;
  33.     protected TranslatorInterface $translator;
  34.     protected string $default_locale;
  35.     protected ?array $decode_token;
  36.     public function __construct(
  37.         EntityManagerInterface $em,
  38.         RequestStack $requestStack,
  39.         TranslatorInterface $translator,
  40.         string $default_locale 'es'
  41.     ) {
  42.         $this->em $em;
  43.         $this->request $requestStack->getCurrentRequest();
  44.         $this->translator $translator;
  45.         $this->default_locale $default_locale;
  46.     }
  47.     /**
  48.      * @internal
  49.      * @required
  50.      */
  51.     public function setContainer(ContainerInterface $container): ?ContainerInterface
  52.     {
  53.         $previous $this->container;
  54.         $this->container $container;
  55.         return $previous;
  56.     }
  57.     protected function getRequestHeaders(): HeaderBag
  58.     {
  59.         return $this->request->headers;
  60.     }
  61.     protected function getRequestBody(): array
  62.     {
  63.         $json = (string)$this->request->getContent();
  64.         if ($json) {
  65.             return json_decode($jsontrue512JSON_THROW_ON_ERROR);
  66.         }
  67.         return [];
  68.     }
  69.     protected function getDefaultLocale(): string
  70.     {
  71.         /** @phpstan-ignore-next-line */
  72.         return $this->request?->getDefaultLocale();
  73.     }
  74.     protected function getLocale(): string
  75.     {
  76.         /** @phpstan-ignore-next-line */
  77.         return $this->request?->getLocale();
  78.     }
  79.     public function getAdmin(): ?Admin
  80.     {
  81.         /** @phpstan-ignore-next-line */
  82.         return $this->request?->attributes?->get('admin');
  83.     }
  84.     public function getHeadquarter(): ?Headquarter
  85.     {
  86.         /** @phpstan-ignore-next-line */
  87.         return $this->request?->attributes?->get('headquarter');
  88.     }
  89.     public function getUser(): ?User
  90.     {
  91.         /** @phpstan-ignore-next-line */
  92.         return $this->request?->attributes?->get('user');
  93.     }
  94.     public function getDevice(): ?UserDevice
  95.     {
  96.         /** @phpstan-ignore-next-line */
  97.         return $this->request?->attributes?->get('device');
  98.     }
  99.     public function getAudiencesOrUsers(): ?array
  100.     {
  101.         /** @phpstan-ignore-next-line */
  102.         return $this->request?->attributes?->get('audiences_or_users');
  103.     }
  104.     public function getPermissionsToArray(): array
  105.     {
  106.         if ($admin $this->getAdmin()) {
  107.             if ($admin->getIsSuper()) {
  108.                 return $admin->getHeadquarter()->getPermissionsToArray();
  109.             }
  110.             return $admin->getPermissionsToArray();
  111.         }
  112.         if ($user $this->getUser()) {
  113.             return $user->getRole()->getPermissionsToArray();
  114.         }
  115.         return [];
  116.     }
  117.     protected function validate(array $dataCollection $constraint): array
  118.     {
  119.         $validator Validation::createValidator();
  120.         $errors $validator->validate($data$constraint);
  121.         $messages = [];
  122.         if (count($errors) > 0) {
  123.             foreach ($errors as $violation) {
  124.                 $key str_replace(array('['']'), ''$violation->getPropertyPath());
  125.                 $messages[$key][] = $this->translator->trans(
  126.                     $violation->getMessageTemplate(),
  127.                     $violation->getParameters(),
  128.                     'validators'
  129.                 );
  130.             }
  131.         }
  132.         return $messages;
  133.     }
  134.     protected function json(
  135.         array|bool|null $data,
  136.         int             $status Response::HTTP_OK,
  137.         array           $headers = [],
  138.         array           $context = []
  139.     ): JsonResponse {
  140.         $commons null;
  141.         $user $this->getUser();
  142.         if ($user instanceof User) {
  143.             $unread $this->em->getRepository(UserNotification::class)->findBy([
  144.                 'user' => $user,
  145.                 'status' => UserNotification::STATUS_UNREAD
  146.             ]);
  147.             /** @phpstan-ignore-next-line */
  148.             $surveys $this->em->getRepository(Survey::class)->findPaginateBy(
  149.                 [
  150.                     'status' => Survey::STATUS_PUBLISHED,
  151.                     'headquarter' => $this->getHeadquarter(),
  152.                 ],
  153.                 [
  154.                     'audiences_or_users' => $this->getAudiencesOrUsers(),
  155.                     'between_start-expiration' => true
  156.                 ],
  157.                 [
  158.                     //
  159.                 ],
  160.                 1,
  161.                 0
  162.             );
  163.             $timesheet $this->em->getRepository(UserTimesheet::class)->findOneBy([
  164.                 'user' => $user,
  165.                 'is_started' => true
  166.             ]);
  167.             if ($timesheet instanceof UserTimesheet) {
  168.                 $timesheet UserTimesheetSerializer::item($timesheet);
  169.             }
  170.             $commons = [
  171.                 'notifications_unread' => count($unread),
  172.                 'surveys_pending' => count($surveys),
  173.                 'timesheets_active' => $timesheet
  174.             ];
  175.         }
  176.         $response = [
  177.             'data' => $data,
  178.             'commons' => $commons,
  179.             'requestId' => Uuid::v4()
  180.         ];
  181.         /** @phpstan-ignore-next-line */
  182.         $json $this->container->get('serializer')->serialize($response'json'array_merge([
  183.             'json_encode_options' => JsonResponse::DEFAULT_ENCODING_OPTIONS,
  184.         ], $context));
  185.         return new JsonResponse($json$status$headerstrue);
  186.     }
  187.     protected function render(
  188.         string $view,
  189.         array $parameters = [],
  190.         Response $response null
  191.     ): Response {
  192.         /** @phpstan-ignore-next-line */
  193.         $content $this->container->get('twig')->render($view$parameters);
  194.         if (null === $response) {
  195.             $response = new Response();
  196.         }
  197.         $response->setContent($content);
  198.         return $response;
  199.     }
  200.     protected function export(
  201.         ?array $data,
  202.     ): Response {
  203.         $spreadsheet = new Spreadsheet();
  204.         $spreadsheet
  205.             ->getActiveSheet()
  206.             ->fromArray($datanull'A1');
  207.         $content IOFactory::createWriter($spreadsheet'Xlsx');
  208.         return new StreamedResponse(function () use ($content) {
  209.             $content->save('php://output');
  210.         });
  211.     }
  212. }