PaginationListener::getPages()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 15
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Damax\Common\Bridge\Symfony\Bundle\Listener;
6
7
use Pagerfanta\Pagerfanta;
8
use Psr\Link\EvolvableLinkProviderInterface;
9
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
10
use Symfony\Component\HttpKernel\Event\ResponseEvent;
11
use Symfony\Component\HttpKernel\Event\ViewEvent;
12
use Symfony\Component\HttpKernel\KernelEvents;
13
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
14
use Symfony\Component\WebLink\GenericLinkProvider;
15
use Symfony\Component\WebLink\Link;
16
17
class PaginationListener implements EventSubscriberInterface
18
{
19
    private $urlGenerator;
20
21
    public function __construct(UrlGeneratorInterface $urlGenerator)
22
    {
23
        $this->urlGenerator = $urlGenerator;
24
    }
25
26
    public static function getSubscribedEvents(): array
27
    {
28
        return [
29
            KernelEvents::VIEW => ['onKernelView', 8],
30
            KernelEvents::RESPONSE => 'onKernelResponse',
31
        ];
32
    }
33
34
    public function onKernelView(ViewEvent $event)
35
    {
36
        $paginator = $event->getControllerResult();
37
38
        if (!$event->isMasterRequest() || !$paginator instanceof Pagerfanta) {
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Component\HttpKe...vent::isMasterRequest() has been deprecated: since symfony/http-kernel 5.3, use isMainRequest() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

38
        if (!/** @scrutinizer ignore-deprecated */ $event->isMasterRequest() || !$paginator instanceof Pagerfanta) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
39
            return;
40
        }
41
42
        $attributes = $event->getRequest()->attributes;
43
        $route = $attributes->get('_route');
44
        $params = $attributes->get('_route_params', []);
45
46
        /** @var EvolvableLinkProviderInterface $linkProvider */
47
        $linkProvider = $attributes->get('_links', new GenericLinkProvider());
48
49
        foreach ($this->getPages($paginator) as $rel => $page) {
50
            $href = $this->urlGenerator->generate($route, $params + ['page' => $page], UrlGeneratorInterface::ABSOLUTE_URL);
51
52
            $linkProvider = $linkProvider->withLink(new Link($rel, $href));
53
        }
54
55
        $attributes->set('_links', $linkProvider);
56
        $attributes->set('_pager', $paginator);
57
    }
58
59
    public function onKernelResponse(ResponseEvent $event)
60
    {
61
        $paginator = $event->getRequest()->attributes->get('_pager');
62
63
        if (!$paginator instanceof Pagerfanta) {
64
            return;
65
        }
66
67
        $response = $event->getResponse();
68
69
        $total = $paginator->getNbResults();
70
        $count = $total ? $paginator->getCurrentPageOffsetEnd() - $paginator->getCurrentPageOffsetStart() + 1 : 0;
71
72
        $response->headers->set('X-Page', $paginator->getCurrentPage());
73
        $response->headers->set('X-Per-Page', $paginator->getMaxPerPage());
74
        $response->headers->set('X-Total-Count', $total);
75
        $response->headers->set('X-Count', $count);
76
    }
77
78
    private function getPages(Pagerfanta $paginator): array
79
    {
80
        $page = (int) $paginator->getCurrentPage();
81
        $last = (int) $paginator->getNbPages();
82
83
        $pages = ['first' => 1, 'last' => $last];
84
85
        if ($page > 1) {
86
            $pages['prev'] = $page - 1;
87
        }
88
        if ($page < $last) {
89
            $pages['next'] = $page + 1;
90
        }
91
92
        return $pages;
93
    }
94
}
95