src/Form/Type/SubresourceChoicesTrait.php line 70

Open in your IDE?
  1. <?php
  2. namespace App\Form\Type;
  3. use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
  4. use App\Model\IriNormalizableInterface;
  5. use App\Service\ApiWebServiceFilterBuilder\PaginationFilterBuilder;
  6. use App\Service\Cache\CacheManager;
  7. use App\V4\Logger\SentryLogger;
  8. use Psr\Cache\CacheException;
  9. use Psr\Cache\InvalidArgumentException;
  10. use Throwable;
  11. trait SubresourceChoicesTrait
  12. {
  13.     /**
  14.      * @throws CacheException
  15.      * @throws InvalidArgumentException
  16.      */
  17.     private function getChoices(
  18.         CollectionDataProviderInterface $collectionDataProvider,
  19.         CacheManager $cacheManager,
  20.         SentryLogger $sentryLogger,
  21.         string $entityFQCN,
  22.         array $filters,
  23.         string $getterLabel,
  24.         string $getterValue
  25.     ): array {
  26.         $entityChoices $this->getEntityChoices($collectionDataProvider$cacheManager$sentryLogger$entityFQCN$filters);
  27.         $choices = [];
  28.         foreach ($entityChoices as $entity) {
  29.             $choices[$entity->$getterLabel()] = $entity->$getterValue();
  30.         }
  31.         return $choices;
  32.     }
  33.     /**
  34.      * @throws CacheException
  35.      * @throws InvalidArgumentException
  36.      * @throws \Exception
  37.      */
  38.     private function getEntityChoices(
  39.         CollectionDataProviderInterface $collectionDataProvider,
  40.         CacheManager $cacheManager,
  41.         SentryLogger $sentryLogger,
  42.         string $entityFQCN,
  43.         array $filters = []
  44.     ): iterable {
  45.         $key $entityFQCN;
  46.         $choices $cacheManager->get($key__METHOD__.'_'.serialize($filters));
  47.         if (null !== $choices) {
  48.             unset($choices['isCache'], $choices['cacheDate']);
  49.             return $choices;
  50.         }
  51.         try {
  52.             $choices $collectionDataProvider->getCollection($entityFQCNnull, [
  53.                 'filters' => array_merge($filters, [PaginationFilterBuilder::ACTIVATE_PAGNIATION_KEY => false]),
  54.             ]);
  55.             foreach ($choices as $entity) {
  56.                 if ($entity instanceof IriNormalizableInterface) {
  57.                     $entity->setNormalizeAsIRI(true);
  58.                 }
  59.             }
  60.             $cacheManager->set($key__METHOD__.'_'.serialize($filters), $choices);
  61.             return $choices;
  62.         } catch (Throwable $exception) {
  63.             $sentryLogger->captureException(
  64.                 SentryLogger::CHANNEL_DATA_RETRIEVER,
  65.                 $exception,
  66.                 [
  67.                     'catchOnClass' => get_class($this),
  68.                     'entitySearched' => $entityFQCN,
  69.                     'filters' => serialize($filters),
  70.                 ]
  71.             );
  72.         }
  73.         return [];
  74.     }
  75. }