src/EventSubscriber/PaginationSubscriber.php line 28

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by simpson <simpsonwork@gmail.com>
  4.  * Date: 2019-04-18
  5.  * Time: 20:53
  6.  */
  7. namespace App\EventSubscriber;
  8. use App\Entity\Profile\Profile;
  9. use App\Event\PaginatorPageTakenEvent;
  10. use App\Event\Profile\ProfilesShownEvent;
  11. use App\Repository\ReadModel\ProfileListingReadModel;
  12. use App\Service\DefaultCityProvider;
  13. use Carbon\CarbonImmutable;
  14. use Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination;
  15. use Knp\Component\Pager\Event\AfterEvent;
  16. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. class PaginationSubscriber implements EventSubscriberInterface
  19. {
  20.     public function __construct(
  21.         protected DefaultCityProvider $defaultCityProvider,
  22.         private EventDispatcherInterface $eventDispatcher
  23.     ) {}
  24.     public function changeRoute(AfterEvent $event): void
  25.     {
  26.         $paginationView $event->getPaginationView();
  27.         if (!$paginationView instanceof SlidingPagination) {
  28.             return;
  29.         }
  30.         $currentRoute $paginationView->getRoute();
  31.         if (false !== strpos($currentRoute'._pagination')) {
  32.             $paginationView->setUsedRoute(str_replace('._pagination'''$currentRoute));
  33.         }
  34.         if ('homepage' === $currentRoute) {
  35.             $paginationView->setUsedRoute('profile_list.list_by_city');
  36.             $paginationView->setParam('city'$this->defaultCityProvider->getDefaultCity()->getUriIdentity());
  37.         }
  38.     }
  39.     /**
  40.      * @inheritDoc
  41.      */
  42.     public static function getSubscribedEvents()
  43.     {
  44.         return [
  45.             PaginatorPageTakenEvent::NAME => 'onPaginatorPageTaken',
  46.             'knp_pager.after' => 'changeRoute',
  47.         ];
  48.     }
  49.     public function onPaginatorPageTaken(PaginatorPageTakenEvent $event): void
  50.     {
  51.         //вынесено d ProfileCtrController для ajax
  52.         return;
  53.         $items $event->getItems();
  54.         if(empty($items))
  55.             return;
  56.         if($items[0] instanceof Profile) {
  57.             $profileIds array_map(function(Profile $profile): int {
  58.                 return $profile->getId();
  59.             }, $items);
  60.             $this->eventDispatcher->dispatch(new ProfilesShownEvent($profileIds'pagination_profiles'), ProfilesShownEvent::NAME);
  61.         } elseif($items[0] instanceof ProfileListingReadModel) {
  62.             $profileIds array_map(function(ProfileListingReadModel $profileListingReadModel) {
  63.                 return $profileListingReadModel->id;
  64.             }, $items);
  65.             $this->eventDispatcher->dispatch(new ProfilesShownEvent($profileIds'pagination_profiles_read_model'), ProfilesShownEvent::NAME);
  66.         }
  67.     }
  68. }