Passed
Push — master ( 8f9ec7...80523f )
by Timo
23:39
created

initializeAction()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.1406

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 6
cts 8
cp 0.75
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 4
nop 0
crap 3.1406
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 27
    public function initializeAction() {
75 27
        $configuration = is_array($this->widgetConfiguration['configuration']) ? $this->widgetConfiguration['configuration'] : [];
76 27
        ArrayUtility::mergeRecursiveWithOverrule($this->configuration, $configuration, false);
77 27
        $this->maximumNumberOfLinks = (int)$this->configuration['maximumNumberOfLinks'];
78 27
        if (!empty($this->configuration['templatePath'])) {
79
            $this->templatePath = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($this->configuration['templatePath']);
80
        }
81 27
    }
82
83
    /**
84
     * If a certain number of links should be displayed, adjust before and after
85
     * amounts accordingly.
86
     *
87
     * @return void
88
     */
89 27
    protected function calculateDisplayRange()
90
    {
91 27
        $maximumNumberOfLinks = $this->maximumNumberOfLinks;
92 27
        if ($maximumNumberOfLinks > $this->numberOfPages) {
93 27
            $maximumNumberOfLinks = $this->numberOfPages;
94
        }
95 27
        $delta = floor($maximumNumberOfLinks / 2);
96 27
        $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...
97 27
        $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...
98 27
        if ($this->displayRangeStart < 1) {
99 8
            $this->displayRangeEnd -= $this->displayRangeStart - 1;
100
        }
101 27
        if ($this->displayRangeEnd > $this->numberOfPages) {
102
            $this->displayRangeStart -= $this->displayRangeEnd - $this->numberOfPages;
103
        }
104 27
        $this->displayRangeStart = (int)max($this->displayRangeStart, 1);
105 27
        $this->displayRangeEnd = (int)min($this->displayRangeEnd, $this->numberOfPages);
106 27
    }
107
108
    /**
109
     * Returns an array with the keys "pages", "current", "numberOfPages", "nextPage" & "previousPage"
110
     *
111
     * @return array
112
     */
113 27
    protected function buildPagination()
114
    {
115 27
        $this->calculateDisplayRange();
116 27
        $pages = [];
117 27
        for ($i = $this->displayRangeStart; $i <= $this->displayRangeEnd; $i++) {
118 22
            $pages[] = ['number' => $i, 'isCurrent' => $i === $this->currentPage];
119
        }
120 27
        $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];
121 27
        if ($this->currentPage < $this->numberOfPages) {
122 8
            $pagination['nextPage'] = $this->currentPage + 1;
123
        }
124 27
        if ($this->currentPage > 1) {
125 1
            $pagination['previousPage'] = $this->currentPage - 1;
126
        }
127
128
        // calculate starting count for <ol> (items per page multiplied by (number of pages -1) and adding +1)
129 27
        $pagination['resultCountStart'] = (($this->getItemsPerPage() * ($this->currentPage - 1)) + 1);
130 27
        return $pagination;
131
    }
132
133
    /**
134
     * @return int
135
     */
136
    abstract protected function getItemsPerPage();
137
}
138