src/EventSubscriber/PaginationSubscriber.php line 30

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 App\Service\SplitFirstPagePagination;
  14. use Carbon\CarbonImmutable;
  15. use Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination;
  16. use Knp\Component\Pager\Event\AfterEvent;
  17. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. class PaginationSubscriber implements EventSubscriberInterface
  20. {
  21.     public function __construct(
  22.         protected DefaultCityProvider $defaultCityProvider,
  23.         private EventDispatcherInterface $eventDispatcher,
  24.         private SplitFirstPagePagination $splitFirstPagePagination,
  25.     ) {}
  26.     public function changeRoute(AfterEvent $event): void
  27.     {
  28.         $paginationView $event->getPaginationView();
  29.         if (!$paginationView instanceof SlidingPagination) {
  30.             return;
  31.         }
  32.         $currentRoute $paginationView->getRoute();
  33.         if (false !== strpos($currentRoute'._pagination')) {
  34.             $paginationView->setUsedRoute(str_replace('._pagination'''$currentRoute));
  35.         }
  36.         if ('homepage' === $currentRoute) {
  37.             $paginationView->setUsedRoute('profile_list.list_by_city');
  38.             $paginationView->setParam('city'$this->defaultCityProvider->getDefaultCity()->getUriIdentity());
  39.         }
  40.         $this->fixSplitPageCount($paginationView);
  41.     }
  42.     private function fixSplitPageCount(SlidingPagination $paginationView): void
  43.     {
  44.         if (!$this->splitFirstPagePagination->isActive()) {
  45.             return;
  46.         }
  47.         $realTotal $paginationView->getTotalItemCount();
  48.         $currentPageLimit $paginationView->getItemNumberPerPage();
  49.         $paginationView->setTotalItemCount(
  50.             $this->splitFirstPagePagination->getPaginatorAdjustedTotalCount($realTotal$currentPageLimit)
  51.         );
  52.     }
  53.     /**
  54.      * @inheritDoc
  55.      */
  56.     public static function getSubscribedEvents()
  57.     {
  58.         return [
  59.             PaginatorPageTakenEvent::NAME => 'onPaginatorPageTaken',
  60.             'knp_pager.after' => 'changeRoute',
  61.         ];
  62.     }
  63.     public function onPaginatorPageTaken(PaginatorPageTakenEvent $event): void
  64.     {
  65.         //вынесено d ProfileCtrController для ajax
  66.         return;
  67.         $items $event->getItems();
  68.         if(empty($items))
  69.             return;
  70.         if($items[0] instanceof Profile) {
  71.             $profileIds array_map(function(Profile $profile): int {
  72.                 return $profile->getId();
  73.             }, $items);
  74.             $this->eventDispatcher->dispatch(new ProfilesShownEvent($profileIds'pagination_profiles'), ProfilesShownEvent::NAME);
  75.         } elseif($items[0] instanceof ProfileListingReadModel) {
  76.             $profileIds array_map(function(ProfileListingReadModel $profileListingReadModel) {
  77.                 return $profileListingReadModel->id;
  78.             }, $items);
  79.             $this->eventDispatcher->dispatch(new ProfilesShownEvent($profileIds'pagination_profiles_read_model'), ProfilesShownEvent::NAME);
  80.         }
  81.     }
  82. }