src/Point/EventSubscriber/CollectionByAccountReadSubscriber.php line 29

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Point\EventSubscriber;
  4. use ApiPlatform\Core\EventListener\EventPriorities;
  5. use App\Point\Doctrine\Entity\Transaction;
  6. use App\Point\Doctrine\Entity\Wallet;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  9. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  10. use Symfony\Component\HttpKernel\KernelEvents;
  11. final class CollectionByAccountReadSubscriber implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @return array<string, array<int, array<int, int|string>>>
  15.      */
  16.     public static function getSubscribedEvents(): array
  17.     {
  18.         return [
  19.             KernelEvents::CONTROLLER => [
  20.                 ['hasFilter'EventPriorities::PRE_READ],
  21.             ],
  22.         ];
  23.     }
  24.     public function hasFilter(ControllerEvent $event): void
  25.     {
  26.         $resource $event->getRequest()->attributes->get('_api_resource_class');
  27.         if (!in_array($resource, [Wallet::class, Transaction::class], true)) {
  28.             return;
  29.         }
  30.         $controller $event->getRequest()->attributes->get('_controller');
  31.         if ('api_platform.action.get_collection' !== $controller) {
  32.             return;
  33.         }
  34.         if (
  35.             !$event->getRequest()->query->has('account')
  36.             || '' === $event->getRequest()->query->get('account')
  37.         ) {
  38.             throw new BadRequestHttpException('Filter `account` is required');
  39.         }
  40.     }
  41. }