src/Controller/Api/PersonController.php line 30

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Api;
  3. use App\Enum\DocumentStatusEnum;
  4. use App\Repository\PersonRepository;
  5. use Evo\Infrastructure\MappingORM\Document;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. class PersonController extends AbstractController
  11. {
  12.     private PersonRepository $repository;
  13.     public function __construct(PersonRepository $repository)
  14.     {
  15.         $this->repository $repository;
  16.     }
  17.     /**
  18.      * @Route(
  19.      *     name="get_documents_people",
  20.      *     path="/people/{id}/document",
  21.      *     methods={"GET"},
  22.      *     defaults={"_api_item_operation_name"="get_documents"}
  23.      * )
  24.      */
  25.     public function getDocuments(Request $request$id)
  26.     {
  27.         $person $this->repository->find($id);
  28.         $statusQuery $request->get('status');
  29.         $aStatus explode(','$statusQuery);
  30.         if (!isset($aStatus[0])) {
  31.             return $this->json([]);
  32.         }
  33.         if (null !== $person) {
  34.             $result = [];
  35.             $now date('Y-m-d');
  36.             $documents $person->getDocuments()->toArray();
  37.             /** @var Document $document */
  38.             foreach ($documents as $document) {
  39.                 $isExpired false;
  40.                 if ($document->getExpirationDate() && DocumentStatusEnum::NOT_APPROVED !== $document->getStatus() && $document->getExpirationDate()->format('Y-m-d') < $now) {
  41.                     $isExpired true;
  42.                 }
  43.                 if ($isExpired && in_array('EXPIRED'$aStatustrue)) {
  44.                     $result[] = $document;
  45.                 } elseif (in_array($document->getStatus(), $aStatustrue)) {
  46.                     $result[] = $document;
  47.                 }
  48.             }
  49.             return $this->json($result);
  50.         }
  51.         return $this->json(['message' => 'Person not found'], Response::HTTP_NOT_FOUND);
  52.     }
  53. }