1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Setono\SyliusPaginationPlugin\EventHandler; |
||
6 | |||
7 | use Pagerfanta\Pagerfanta; |
||
8 | use Setono\SyliusPaginationPlugin\Exception\UnexpectedEventException; |
||
9 | use Setono\SyliusPaginationPlugin\ParameterBag\PaginationParameterBag; |
||
10 | use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent; |
||
11 | use Sylius\Bundle\ResourceBundle\Grid\View\ResourceGridView; |
||
12 | use Symfony\Component\EventDispatcher\Event; |
||
13 | |||
14 | final class ResourceControllerEventHandler implements EventHandlerInterface |
||
15 | { |
||
16 | public function supports(Event $event): bool |
||
17 | { |
||
18 | if (!$event instanceof ResourceControllerEvent) { |
||
19 | return false; |
||
20 | } |
||
21 | |||
22 | $resourceGridView = $event->getSubject(); |
||
23 | if (!$resourceGridView instanceof ResourceGridView) { |
||
24 | return false; |
||
25 | } |
||
26 | |||
27 | $data = $resourceGridView->getData(); |
||
28 | if (!$data instanceof Pagerfanta) { |
||
29 | return false; |
||
30 | } |
||
31 | |||
32 | return true; |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * @param ResourceControllerEvent $event |
||
37 | * |
||
38 | * @return PaginationParameterBag |
||
39 | */ |
||
40 | public function handle(Event $event): PaginationParameterBag |
||
41 | { |
||
42 | if (!$this->supports($event)) { |
||
43 | throw new UnexpectedEventException($event, ResourceControllerEvent::class); |
||
44 | } |
||
45 | |||
46 | /** @var ResourceGridView $resourceGridView */ |
||
47 | $resourceGridView = $event->getSubject(); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
48 | |||
49 | /** @var Pagerfanta $data */ |
||
50 | $data = $resourceGridView->getData(); |
||
51 | |||
52 | return new PaginationParameterBag($resourceGridView->getRequestConfiguration()->getRequest(), $data->getNbResults(), $data->getMaxPerPage(), $data->getCurrentPage()); |
||
53 | } |
||
54 | } |
||
55 |