src/Controller/Api/SearchController.php line 37

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Api;
  3. use App\Enum\OrganizationStatusEnum;
  4. use App\Service\ElasticSearchService;
  5. use Evo\Infrastructure\MappingORM\Organization;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Symfony\Component\Serializer\SerializerInterface;
  11. use Symfony\Contracts\Translation\TranslatorInterface;
  12. /**
  13.  * @Route("/search")
  14.  */
  15. final class SearchController extends AbstractController
  16. {
  17.     private ElasticSearchService $elasticSearchService;
  18.     private SerializerInterface $serializer;
  19.     private TranslatorInterface $translator;
  20.     public function __construct(
  21.         ElasticSearchService $elasticSearchService,
  22.         SerializerInterface $serializer,
  23.         TranslatorInterface $translator
  24.     ) {
  25.         $this->elasticSearchService $elasticSearchService;
  26.         $this->serializer $serializer;
  27.         $this->translator $translator;
  28.     }
  29.     /**
  30.      * @Route("/mailbox", name="app_search_mailbox", methods={"POST"})
  31.      */
  32.     public function searchFromMailbox(Request $request)
  33.     {
  34.         $content json_decode($request->getContent(), true512JSON_THROW_ON_ERROR);
  35.         if (!isset($content['search_phrase']) || empty($content['search_phrase'])) {
  36.             throw new \InvalidArgumentException('Not valid search input!');
  37.         }
  38.         $results $this->elasticSearchService->search(
  39.             ElasticSearchService::CONTEXT_MAILBOX,
  40.             $content['search_phrase'],
  41.             $content['postal_code'] ?? null
  42.         );
  43.         $normalized $this->serializer->normalize($results$this->extractFormat($request), [
  44.             'groups' => ['read_search''read_search_signature'],
  45.             'callbacks' => $this->getSerializerCallbacks(),
  46.         ]);
  47.         return $this->json($normalized);
  48.     }
  49.     /**
  50.      * @Route("/admin", name="app_search_admin", methods={"POST"})
  51.      */
  52.     public function searchFromAdmin(Request $request)
  53.     {
  54.         $content json_decode($request->getContent(), true512JSON_THROW_ON_ERROR);
  55.         if (!isset($content['search_phrase']) || empty($content['search_phrase'])) {
  56.             throw new \InvalidArgumentException('Not valid search input!');
  57.         }
  58.         $results $this->elasticSearchService->search(
  59.             ElasticSearchService::CONTEXT_ADMIN,
  60.             $content['search_phrase'],
  61.             null
  62.         );
  63.         $normalized $this->serializer->normalize($results$this->extractFormat($request), [
  64.             'groups' => ['read_search'],
  65.             'callbacks' => $this->getSerializerCallbacks(),
  66.         ]);
  67.         return $this->json($normalized);
  68.     }
  69.     private function extractFormat(Request $request): string
  70.     {
  71.         $acceptable = ['application/ld+json''application/json'];
  72.         $header $request->getAcceptableContentTypes()[0] ?? '*/*';
  73.         // Use json by default if */* is accepted
  74.         $accept = ('*/*' !== $header) ? $header $acceptable[0];
  75.         if (!in_array($accept$acceptabletrue)) {
  76.             throw new NotAcceptableHttpException();
  77.         }
  78.         return $request->getFormat($accept);
  79.     }
  80.     private function getSerializerCallbacks(): array
  81.     {
  82.         $translator $this->translator;
  83.         return [
  84.             // For organizations only, translate status
  85.             'statusTranslated' => function ($innerObject$outerObjectstring $attributeNamestring $format null, array $context = []) use ($translator) {
  86.                 if (!$outerObject instanceof Organization) {
  87.                     return '';
  88.                 }
  89.                 if (null === $innerObject) {
  90.                     return '';
  91.                 }
  92.                 if (!OrganizationStatusEnum::isValueExist($innerObject)) {
  93.                     return '';
  94.                 }
  95.                 return $translator->trans(OrganizationStatusEnum::getReadableValue($innerObject));
  96.             },
  97.             'numberOfLetterParcel' => function ($innerObject$outerObjectstring $attributeNamestring $format null, array $context = []) {
  98.                 if ($outerObject instanceof Organization) {
  99.                     $numberOfLetterParcel $this->getOrganizationNumberOfLetterParcel($outerObject'LETTER,LETTER_WITH_AR,NOTICE,BAILIFF_NOTICE,BAILIFF_DEED');
  100.                     return $numberOfLetterParcel $this->getOrganizationNumberOfLetterParcel($outerObject'PARCEL');
  101.                 }
  102.                 return 0;
  103.             },
  104.         ];
  105.     }
  106.     private function getOrganizationNumberOfLetterParcel(Organization $organizationstring $type): int
  107.     {
  108.         $letterParcel $this->forward(LetterController::class.'::letterParcel', [
  109.             'type' => $type,
  110.             'orgaId' => $organization->getId(),
  111.         ]);
  112.         return json_decode($letterParcel->getContent(), null512JSON_THROW_ON_ERROR)->result;
  113.     }
  114. }