GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

PaginationFactory::getPagerAdapter()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Halapi\Factory;
4
5
use Halapi\ObjectManager\ObjectManagerInterface;
6
use Halapi\Representation\PaginatedRepresentation;
7
use Pagerfanta\Adapter\AdapterInterface;
8
use Pagerfanta\Pagerfanta;
9
use Symfony\Component\HttpFoundation\RequestStack;
10
use Symfony\Component\OptionsResolver\OptionsResolver;
11
use Halapi\UrlGenerator\UrlGeneratorInterface;
12
13
/**
14
 * Class PaginationFactory.
15
 *
16
 * @author Romain Richard
17
 */
18
class PaginationFactory
19
{
20
    /**
21
     * @var ObjectManagerInterface
22
     */
23
    public $objectManager;
24
25
    /**
26
     * @var UrlGeneratorInterface
27
     */
28
    public $urlGenerator;
29
30
    /**
31
     * @var RequestStack
32
     */
33
    private $requestStack;
34
35
    /**
36
     * @var string
37
     */
38
    private $pagerStrategy;
39
40
    /**
41
     * PaginationFactory constructor.
42
     *
43
     * @param UrlGeneratorInterface  $urlGenerator
44
     * @param ObjectManagerInterface $objectManager
45
     * @param RequestStack           $requestStack
46
     * @param string                 $pagerStrategy
47
     */
48
    public function __construct(
49
        UrlGeneratorInterface $urlGenerator,
50
        ObjectManagerInterface $objectManager,
51
        RequestStack $requestStack,
52
        $pagerStrategy = 'DoctrineORM'
53
    ) {
54
        $this->urlGenerator = $urlGenerator;
55
        $this->objectManager = $objectManager;
56
        $this->requestStack = $requestStack;
57
        $this->setPagerStrategy($pagerStrategy);
58
    }
59
60
    /**
61
     * Get a paginated representation of a collection of entities.
62
     * Your repository for the object $className must implement the 'findAllSorted' method
63
     * @param string $className
64
     *
65
     * @return PaginatedRepresentation
66
     */
67
    public function getRepresentation($className)
68
    {
69
        $shortName = (new \ReflectionClass($className))->getShortName();
70
        list($page, $limit, $sorting, $filterValues, $filerOperators) = array_values($this->addPaginationParams());
71
        $results = $this->objectManager->findAllSorted($className, $sorting, $filterValues, $filerOperators);
72
73
        $pagerAdapter = $this->getPagerAdapter($results);
74
        $pager = new Pagerfanta($pagerAdapter);
75
        $pager->setMaxPerPage($limit);
76
        $pager->setCurrentPage($page);
77
78
        return new PaginatedRepresentation(
79
            $page,
80
            $limit,
81
            [
82
                'self' => $this->getPaginatedRoute($shortName, $limit, $page, $sorting),
83
                'first' => $this->getPaginatedRoute($shortName, $limit, 1, $sorting),
84
                'next' => $this->getPaginatedRoute(
85
                    $shortName,
86
                    $limit,
87
                    $page < $pager->getNbPages() ? $page + 1 : $pager->getNbPages(),
88
                    $sorting
89
                ),
90
                'last' => $this->getPaginatedRoute($shortName, $limit, $pager->getNbPages(), $sorting),
91
            ],
92
            (array) $pager->getCurrentPageResults()
93
        );
94
    }
95
96
    /**
97
     * @param string $pagerStrategy
98
     */
99
    public function setPagerStrategy($pagerStrategy)
100
    {
101
        if (!class_exists('Pagerfanta\Adapter\\'.$pagerStrategy.'Adapter')) {
102
            throw new \InvalidArgumentException(sprintf(
103
                'No adapter named %s found in %s namespace',
104
                'Doctrine'.$pagerStrategy.'Adapter',
105
                'Pagerfanta\Adapter'
106
            ));
107
        }
108
109
        $this->pagerStrategy = $pagerStrategy;
110
    }
111
112
    /**
113
     * @param array $results
114
     *
115
     * @return AdapterInterface
116
     */
117
    private function getPagerAdapter($results)
118
    {
119
        $adapterClassName = 'Pagerfanta\Adapter\\'.$this->pagerStrategy.'Adapter';
120
121
        return new $adapterClassName(...$results);
122
    }
123
124
    /**
125
     * Get the pagination parameters, filtered.
126
     *
127
     * @return array
128
     */
129
    private function addPaginationParams()
130
    {
131
        $resolver = new OptionsResolver();
132
133
        $resolver->setDefaults(array(
134
            'page' => '1',
135
            'limit' => '20',
136
            'sorting' => [],
137
            'filtervalue' => [],
138
            'filteroperator' => [],
139
        ));
140
141
        $resolver->setAllowedTypes('page', ['NULL', 'string']);
142
        $resolver->setAllowedTypes('limit', ['NULL', 'string']);
143
        $resolver->setAllowedTypes('sorting', ['NULL', 'array']);
144
        $resolver->setAllowedTypes('filtervalue', ['NULL', 'array']);
145
        $resolver->setAllowedTypes('filteroperator', ['NULL', 'array']);
146
147
        $request = $this->requestStack->getMasterRequest();
148
149
        return $resolver->resolve(array_filter([
150
            'page' => $request->query->get('page'),
151
            'limit' => $request->query->get('limit'),
152
            'sorting' => $request->query->get('sorting'),
153
            'filtervalue' => $request->query->get('filtervalue'),
154
            'filteroperator' => $request->query->get('filteroperator'),
155
        ]));
156
    }
157
158
    /**
159
     * Return the url of a resource based on the 'get_entity' route name convention.
160
     *
161
     * @param string $name
162
     * @param $limit
163
     * @param $page
164
     * @param $sorting
165
     *
166
     * @return string
167
     */
168
    private function getPaginatedRoute($name, $limit, $page, $sorting)
169
    {
170
        return $this->urlGenerator->generate(
171
            'get_'.strtolower($name).'s',
172
            [
173
                'sorting' => $sorting,
174
                'page' => $page,
175
                'limit' => $limit,
176
            ]
177
        );
178
    }
179
}
180