src/EventSubscriber/PaginationSubscriber.php line 76

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