src/EventSubscriber/PaginateSubscriber.php line 30

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Doctrine\ORM\Query;
  4. use Knp\Component\Pager\Event\ItemsEvent;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  7. class PaginateSubscriber implements EventSubscriberInterface
  8. {
  9.     public const ALIASSES = ['applicant''vacancy'];
  10.     /**
  11.      * @var AuthorizationCheckerInterface
  12.      */
  13.     private $authorizationChecker;
  14.     /**
  15.      * PaginateApplicantSubscriber constructor.
  16.      */
  17.     public function __construct(AuthorizationCheckerInterface $authorizationChecker)
  18.     {
  19.         $this->authorizationChecker $authorizationChecker;
  20.     }
  21.     /**
  22.      * {@inheritdoc}
  23.      */
  24.     public function items(ItemsEvent $event)
  25.     {
  26.         if (!isset($event->options['alias']) || !\in_array($event->options['alias'], self::ALIASSEStrue)) {
  27.             return;
  28.         }
  29.         /** @var Query $query */
  30.         $query $event->target;
  31.         $collection $query->getResult();
  32.         $collection array_filter($collection, function ($entity) {
  33.             return $this->authorizationChecker->isGranted('view'$entity);
  34.         });
  35.         $event->count \count($collection);
  36.         $event->items \array_slice($collection$event->getOffset(), $event->getLimit());
  37.         $event->stopPropagation();
  38.     }
  39.     /**
  40.      * {@inheritdoc}
  41.      */
  42.     public static function getSubscribedEvents()
  43.     {
  44.         return ['knp_pager.items' => ['items'1]];
  45.     }
  46. }