<?php
namespace App\Controller\Api;
use App\Enum\DocumentStatusEnum;
use App\Repository\PersonRepository;
use Evo\Infrastructure\MappingORM\Document;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class PersonController extends AbstractController
{
private PersonRepository $repository;
public function __construct(PersonRepository $repository)
{
$this->repository = $repository;
}
/**
* @Route(
* name="get_documents_people",
* path="/people/{id}/document",
* methods={"GET"},
* defaults={"_api_item_operation_name"="get_documents"}
* )
*/
public function getDocuments(Request $request, $id)
{
$person = $this->repository->find($id);
$statusQuery = $request->get('status');
$aStatus = explode(',', $statusQuery);
if (!isset($aStatus[0])) {
return $this->json([]);
}
if (null !== $person) {
$result = [];
$now = date('Y-m-d');
$documents = $person->getDocuments()->toArray();
/** @var Document $document */
foreach ($documents as $document) {
$isExpired = false;
if ($document->getExpirationDate() && DocumentStatusEnum::NOT_APPROVED !== $document->getStatus() && $document->getExpirationDate()->format('Y-m-d') < $now) {
$isExpired = true;
}
if ($isExpired && in_array('EXPIRED', $aStatus, true)) {
$result[] = $document;
} elseif (in_array($document->getStatus(), $aStatus, true)) {
$result[] = $document;
}
}
return $this->json($result);
}
return $this->json(['message' => 'Person not found'], Response::HTTP_NOT_FOUND);
}
}