Completed
Push — master ( 9819c1...f2d6e9 )
by Rafael
06:19
created

AbstractPaginateWidgetController   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 3
dl 0
loc 107
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A initializeAction() 0 7 2
B calculateDisplayRange() 0 18 5
A buildPagination() 0 16 4
getItemsPerPage() 0 1 ?
1
<?php
2
namespace ApacheSolrForTypo3\Solr\ViewHelpers\Widget\Controller;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
use ApacheSolrForTypo3\Solr\Widget\AbstractWidgetController;
18
use TYPO3\CMS\Core\Utility\ArrayUtility;
19
20
/**
21
 * Class ResultPaginateController
22
 *
23
 * @author Frans Saris <[email protected]>
24
 * @author Timo Hund <[email protected]>
25
 * @package ApacheSolrForTypo3\Solr\ViewHelpers\Widget\Controller
26
 */
27
abstract class AbstractPaginateWidgetController extends AbstractWidgetController
28
{
29
30
    /**
31
     * @var array
32
     */
33
    protected $configuration = [
34
        'insertAbove' => true,
35
        'insertBelow' => true,
36
        'maximumNumberOfLinks' => 10,
37
        'addQueryStringMethod' => '',
38
        'templatePath' => ''
39
    ];
40
41
    /**
42
     * @var int
43
     */
44
    protected $currentPage = 1;
45
46
    /**
47
     * @var int
48
     */
49
    protected $displayRangeStart;
50
51
    /**
52
     * @var int
53
     */
54
    protected $displayRangeEnd;
55
56
    /**
57
     * @var int
58
     */
59
    protected $maximumNumberOfLinks = 99;
60
61
    /**
62
     * @var int
63
     */
64
    protected $numberOfPages = 1;
65
66
    /**
67
     * @var string
68
     */
69
    protected $templatePath = '';
70
71
    /**
72
     * @return void
73
     */
74
    public function initializeAction() {
75
        ArrayUtility::mergeRecursiveWithOverrule($this->configuration, $this->widgetConfiguration['configuration'], false);
76
        $this->maximumNumberOfLinks = (int)$this->configuration['maximumNumberOfLinks'];
77
        if (!empty($this->configuration['templatePath'])) {
78
            $this->templatePath = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($this->configuration['templatePath']);
79
        }
80
    }
81
82
    /**
83
     * If a certain number of links should be displayed, adjust before and after
84
     * amounts accordingly.
85
     *
86
     * @return void
87
     */
88
    protected function calculateDisplayRange()
89
    {
90
        $maximumNumberOfLinks = $this->maximumNumberOfLinks;
91
        if ($maximumNumberOfLinks > $this->numberOfPages) {
92
            $maximumNumberOfLinks = $this->numberOfPages;
93
        }
94
        $delta = floor($maximumNumberOfLinks / 2);
95
        $this->displayRangeStart = $this->currentPage - $delta;
0 ignored issues
show
Documentation Bug introduced by
The property $displayRangeStart was declared of type integer, but $this->currentPage - $delta is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
96
        $this->displayRangeEnd = $this->currentPage + $delta - ($maximumNumberOfLinks % 2 === 0 ? 1 : 0);
0 ignored issues
show
Documentation Bug introduced by
The property $displayRangeEnd was declared of type integer, but $this->currentPage + $de...inks % 2 === 0 ? 1 : 0) is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
97
        if ($this->displayRangeStart < 1) {
98
            $this->displayRangeEnd -= $this->displayRangeStart - 1;
99
        }
100
        if ($this->displayRangeEnd > $this->numberOfPages) {
101
            $this->displayRangeStart -= $this->displayRangeEnd - $this->numberOfPages;
102
        }
103
        $this->displayRangeStart = (int)max($this->displayRangeStart, 1);
104
        $this->displayRangeEnd = (int)min($this->displayRangeEnd, $this->numberOfPages);
105
    }
106
107
    /**
108
     * Returns an array with the keys "pages", "current", "numberOfPages", "nextPage" & "previousPage"
109
     *
110
     * @return array
111
     */
112
    protected function buildPagination()
113
    {
114
        $this->calculateDisplayRange();
115
        $pages = [];
116
        for ($i = $this->displayRangeStart; $i <= $this->displayRangeEnd; $i++) {
117
            $pages[] = ['number' => $i, 'isCurrent' => $i === $this->currentPage];
118
        }
119
        $pagination = ['pages' => $pages, 'current' => $this->currentPage, 'numberOfPages' => $this->numberOfPages, 'displayRangeStart' => $this->displayRangeStart, 'displayRangeEnd' => $this->displayRangeEnd, 'hasLessPages' => $this->displayRangeStart > 2, 'hasMorePages' => $this->displayRangeEnd + 1 < $this->numberOfPages];
120
        if ($this->currentPage < $this->numberOfPages) {
121
            $pagination['nextPage'] = $this->currentPage + 1;
122
        }
123
        if ($this->currentPage > 1) {
124
            $pagination['previousPage'] = $this->currentPage - 1;
125
        }
126
        return $pagination;
127
    }
128
129
    /**
130
     * @return int
131
     */
132
    abstract protected function getItemsPerPage();
133
}
134