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.

InformationCollectionAdminView   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 226
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 2
dl 0
loc 226
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setDefaultTemplate() 0 4 1
A getName() 0 4 1
A render() 0 16 1
A initializeProximity() 0 6 2
A calculateStartAndEndPage() 0 21 3
A calculateEndPageForStartPageUnderflow() 0 4 1
A calculateStartPageForEndPageOverflow() 0 4 1
F getPages() 0 42 12
A generateUrl() 0 8 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Netgen\InformationCollection\Core\Pagination\Pagerfanta\View;
6
7
use Pagerfanta\PagerfantaInterface;
8
use Pagerfanta\View\ViewInterface;
9
use Twig\Environment;
10
11
class InformationCollectionAdminView implements ViewInterface
12
{
13
    /**
14
     * @var \Twig\Environment
15
     */
16
    protected $twig;
17
18
    /**
19
     * @var string
20
     */
21
    protected $template;
22
23
    /**
24
     * @var \Pagerfanta\Pagerfanta
25
     */
26
    protected $pagerfanta;
27
28
    /**
29
     * @var \Closure
30
     */
31
    protected $routeGenerator;
32
33
    /**
34
     * @var int
35
     */
36
    protected $proximity;
37
38
    /**
39
     * @var int
40
     */
41
    protected $startPage;
42
43
    /**
44
     * @var int
45
     */
46
    protected $endPage;
47
48
    /**
49
     * Constructor.
50
     *
51
     * @param \Twig\Environment $twig
52
     */
53
    public function __construct(Environment $twig)
54
    {
55
        $this->twig = $twig;
56
    }
57
58
    /**
59
     * Sets the default template.
60
     *
61
     * @param string $template
62
     */
63
    public function setDefaultTemplate($template)
64
    {
65
        $this->template = $template;
66
    }
67
68
    /**
69
     * Returns the canonical name.
70
     *
71
     * @return string
72
     */
73
    public function getName()
74
    {
75
        return 'netgen_information_collection_admin';
76
    }
77
78
    /**
79
     * Renders a Pagerfanta.
80
     *
81
     * The route generator can be any callable to generate
82
     * the routes receiving the page number as first and
83
     * unique argument.
84
     *
85
     * @param \Pagerfanta\PagerfantaInterface $pagerfanta A pagerfanta
86
     * @param \Closure $routeGenerator A callable to generate the routes
87
     * @param array $options An array of options (optional)
88
     *
89
     * @return string
90
     */
91
    public function render(PagerfantaInterface $pagerfanta, $routeGenerator, array $options = [])
92
    {
93
        $this->pagerfanta = $pagerfanta;
0 ignored issues
show
Documentation Bug introduced by
$pagerfanta is of type object<Pagerfanta\PagerfantaInterface>, but the property $pagerfanta was declared to be of type object<Pagerfanta\Pagerfanta>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
94
        $this->routeGenerator = $routeGenerator;
95
96
        $this->initializeProximity($options);
97
        $this->calculateStartAndEndPage();
98
99
        return $this->twig->render(
100
            $options['template'] ?? $this->template,
101
            [
102
                'pager' => $pagerfanta,
103
                'pages' => $this->getPages(),
104
            ]
105
        );
106
    }
107
108
    /**
109
     * Initializes the proximity.
110
     *
111
     * @param array $options
112
     */
113
    protected function initializeProximity($options)
114
    {
115
        $this->proximity = isset($options['proximity']) ?
116
            (int) $options['proximity'] :
117
            2;
118
    }
119
120
    /**
121
     * Calculates start and end page that will be shown in the middle of pager.
122
     */
123
    protected function calculateStartAndEndPage()
124
    {
125
        $currentPage = $this->pagerfanta->getCurrentPage();
126
        $nbPages = $this->pagerfanta->getNbPages();
127
128
        $startPage = $currentPage - $this->proximity;
129
        $endPage = $currentPage + $this->proximity;
130
131
        if ($startPage < 1) {
132
            $endPage = $this->calculateEndPageForStartPageUnderflow($startPage, $endPage, $nbPages);
133
            $startPage = 1;
134
        }
135
136
        if ($endPage > $nbPages) {
137
            $startPage = $this->calculateStartPageForEndPageOverflow($startPage, $endPage, $nbPages);
138
            $endPage = $nbPages;
139
        }
140
141
        $this->startPage = (int) $startPage;
142
        $this->endPage = (int) $endPage;
143
    }
144
145
    /**
146
     * Calculates the end page when start page is underflowed.
147
     *
148
     * @param int $startPage
149
     * @param int $endPage
150
     * @param int $nbPages
151
     *
152
     * @return int
153
     */
154
    protected function calculateEndPageForStartPageUnderflow($startPage, $endPage, $nbPages)
155
    {
156
        return min($endPage + (1 - $startPage), $nbPages);
157
    }
158
159
    /**
160
     * Calculates the start page when end page is overflowed.
161
     *
162
     * @param int $startPage
163
     * @param int $endPage
164
     * @param int $nbPages
165
     *
166
     * @return int
167
     */
168
    protected function calculateStartPageForEndPageOverflow($startPage, $endPage, $nbPages)
169
    {
170
        return max($startPage - ($endPage - $nbPages), 1);
171
    }
172
173
    /**
174
     * Returns the list of all pages that need to be displayed.
175
     *
176
     * @return array
177
     */
178
    protected function getPages()
179
    {
180
        $pages = [];
181
182
        $pages['previous_page'] = $this->pagerfanta->hasPreviousPage() ?
183
            $this->generateUrl($this->pagerfanta->getPreviousPage()) :
184
            false;
185
186
        $pages['first_page'] = $this->startPage > 1 ? $this->generateUrl(1) : false;
187
        $pages['mobile_first_page'] = $this->pagerfanta->getCurrentPage() > 2 ? $this->generateUrl(1) : false;
188
189
        $pages['second_page'] = $this->startPage === 3 ? $this->generateUrl(2) : false;
190
191
        $pages['separator_before'] = $this->startPage > 3 ? true : false;
192
193
        $middlePages = [];
194
        for ($i = $this->startPage, $end = $this->endPage; $i <= $end; ++$i) {
195
            $middlePages[$i] = $this->generateUrl($i);
196
        }
197
198
        $pages['middle_pages'] = $middlePages;
199
200
        $pages['separator_after'] = $this->endPage < $this->pagerfanta->getNbPages() - 2 ? true : false;
201
202
        $pages['second_to_last_page'] = $this->endPage === $this->pagerfanta->getNbPages() - 2 ?
203
            $this->generateUrl($this->pagerfanta->getNbPages() - 1) :
204
            false;
205
206
        $pages['last_page'] = $this->pagerfanta->getNbPages() > $this->endPage ?
207
            $this->generateUrl($this->pagerfanta->getNbPages()) :
208
            false;
209
210
        $pages['mobile_last_page'] = $this->pagerfanta->getCurrentPage() < $this->pagerfanta->getNbPages() - 1 ?
211
            $this->generateUrl($this->pagerfanta->getNbPages()) :
212
            false;
213
214
        $pages['next_page'] = $this->pagerfanta->hasNextPage() ?
215
            $this->generateUrl($this->pagerfanta->getNextPage()) :
216
            false;
217
218
        return $pages;
219
    }
220
221
    /**
222
     * Generates the URL based on provided page.
223
     *
224
     * @param int $page
225
     *
226
     * @return string
227
     */
228
    protected function generateUrl($page)
229
    {
230
        $routeGenerator = $this->routeGenerator;
231
232
        // We use trim here because Pagerfanta (or Symfony?) adds an extra '?'
233
        // at the end of page when there are no other query params
234
        return trim($routeGenerator($page), '?');
235
    }
236
}
237