Completed
Branch master (33af51)
by Timo
04:12
created

PageBrowser::render()   C

Complexity

Conditions 12
Paths 65

Size

Total Lines 93
Code Lines 58

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 43
CRAP Score 20.2624

Importance

Changes 0
Metric Value
dl 0
loc 93
ccs 43
cts 70
cp 0.6143
rs 5.034
c 0
b 0
f 0
cc 12
eloc 58
nc 65
nop 0
crap 20.2624

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace ApacheSolrForTypo3\Solr\Plugin\Results;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2008-2014 Dmitry Dulepov ([email protected])
8
 *  (c) 2015 Ingo Renner ([email protected])
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 2 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *  A copy is found in the textfile GPL.txt and important notices to the license
20
 *  from the author is found in LICENSE.txt distributed with these scripts.
21
 *
22
 *
23
 *  This script is distributed in the hope that it will be useful,
24
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
25
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26
 *  GNU General Public License for more details.
27
 *
28
 *  This copyright notice MUST APPEAR in all copies of the script!
29
 ***************************************************************/
30
31
use TYPO3\CMS\Core\Utility\GeneralUtility;
32
33
/**
34
 * The page browser used in search result listings
35
 *
36
 */
37
class PageBrowser
38
{
39
    protected $numberOfPages;
40
    protected $pageParameterName = 'tx_solr[page]';
41
    protected $currentPage;
42
    protected $pagesBefore = 3;
43
    protected $pagesAfter = 3;
44
45
    protected $template;
46
47
    protected $configuration = array();
48
    protected $labels = array();
49
    protected $contentObject = null;
50
51
    /**
52
     * PageBrowser constructor.
53
     *
54
     * @param array $configuration
55
     * @param array $labels
56
     */
57 18
    public function __construct(array $configuration, array $labels)
58
    {
59 18
        $this->configuration = $configuration;
60 18
        $this->labels = $labels;
61
62 18
        $this->contentObject = GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\ContentObject\\ContentObjectRenderer');
63
64 18
        $this->numberOfPages = $configuration['numberOfPages'];
65 18
        $this->currentPage = $configuration['currentPage'];
66
67 18
        $this->pagesBefore = (int)$configuration['pagesBefore'];
68 18
        $this->pagesAfter = (int)$configuration['pagesAfter'];
69
70 18
        $path = $GLOBALS['TSFE']->tmpl->getFileName($configuration['templateFile']);
71 18
        if ($path !== null && file_exists($path)) {
72 18
            $this->template = file_get_contents($path);
73
        }
74 18
    }
75
76
    /**
77
     * Produces the page browser HTML
78
     *
79
     * @return string Generated content
80
     */
81 18
    public function render()
82
    {
83 18
        $pageBrowser = '';
84
85 18
        if ($this->numberOfPages > 1) {
86
            // Set up
87
            $markers = array(
88 2
                '###TEXT_FIRST###' => htmlspecialchars($this->labels['pagebrowser_first']),
89 2
                '###TEXT_NEXT###' => htmlspecialchars($this->labels['pagebrowser_next']),
90 2
                '###TEXT_PREV###' => htmlspecialchars($this->labels['pagebrowser_prev']),
91 2
                '###TEXT_LAST###' => htmlspecialchars($this->labels['pagebrowser_last']),
92
            );
93 2
            $subPartMarkers = array();
94 2
            $subPart = $this->contentObject->getSubpart($this->template,
95 2
                '###PAGE_BROWSER###');
96
97
            // First page link
98 2
            if ($this->currentPage == 0) {
99 2
                $subPartMarkers['###ACTIVE_FIRST###'] = '';
100
            } else {
101
                $markers['###FIRST_LINK###'] = $this->getPageLink(0);
102
                $subPartMarkers['###INACTIVE_FIRST###'] = '';
103
            }
104
105
            // Prev page link
106 2
            if ($this->currentPage == 0) {
107 2
                $subPartMarkers['###ACTIVE_PREV###'] = '';
108
            } else {
109
                $markers['###PREV_LINK###'] = $this->getPageLink($this->currentPage - 1);
110
                $subPartMarkers['###INACTIVE_PREV###'] = '';
111
            }
112
113
            // Next link
114 2
            if ($this->currentPage >= $this->numberOfPages - 1) {
115
                $subPartMarkers['###ACTIVE_NEXT###'] = '';
116
            } else {
117 2
                $markers['###NEXT_LINK###'] = $this->getPageLink($this->currentPage + 1);
118 2
                $subPartMarkers['###INACTIVE_NEXT###'] = '';
119
            }
120
121
            // Last link
122 2
            if ($this->currentPage == $this->numberOfPages - 1) {
123
                $subPartMarkers['###ACTIVE_LAST###'] = '';
124
            } else {
125 2
                $markers['###LAST_LINK###'] = $this->getPageLink($this->numberOfPages - 1);
126 2
                $subPartMarkers['###INACTIVE_LAST###'] = '';
127
            }
128
129
            // Page links
130 2
            $actPageLinkSubPart = trim($this->contentObject->getSubpart($subPart,
131 2
                '###CURRENT###'));
132 2
            $inactivePageLinkSubPart = trim($this->contentObject->getSubpart($subPart,
133 2
                '###PAGE###'));
134 2
            $pageLinks = '';
135 2
            $start = max($this->currentPage - $this->pagesBefore, 0);
136 2
            $end = min($this->numberOfPages,
137 2
                $this->currentPage + $this->pagesAfter + 1);
138
139 2
            for ($i = $start; $i < $end; $i++) {
140 2
                $template = ($i == $this->currentPage ? $actPageLinkSubPart : $inactivePageLinkSubPart);
141
                $localMarkers = array(
142 2
                    '###NUMBER###' => $i,
143 2
                    '###NUMBER_DISPLAY###' => $i + 1,
144 2
                    '###LINK###' => $this->getPageLink($i),
145
                );
146 2
                $pageLinks .= $this->contentObject->substituteMarkerArray($template,
147
                    $localMarkers);
148
            }
149 2
            $subPartMarkers['###PAGE###'] = $pageLinks;
150 2
            $subPartMarkers['###CURRENT###'] = '';
151
152
            // Less pages part
153 2
            if ($start == 0 || !$this->configuration['enableLessPages']) {
154 2
                $subPartMarkers['###LESS_PAGES###'] = '';
155
            }
156
157
            // More pages part
158 2
            if ($end == $this->numberOfPages || !$this->configuration['enableMorePages']) {
159
                // We have all pages covered. Remove this part.
160 2
                $subPartMarkers['###MORE_PAGES###'] = '';
161
            }
162
163
            // Render / substitute markers
164 2
            $pageBrowser = $this->contentObject->substituteMarkerArrayCached($subPart,
165
                $markers, $subPartMarkers);
166
167
            // Clean up comments
168 2
            $pageBrowser = preg_replace('/<!--\s*###.*?-->/', ' ',
169
                $pageBrowser);
170
        }
171
172 18
        return $pageBrowser;
173
    }
174
175
    /**
176
     * Generates page link. Keeps all current URL parameters except for cHash and page parameter.
177
     *
178
     * @param int $page Page number starting from 1
179
     * @return string Generated link
180
     */
181 2
    protected function getPageLink($page)
182
    {
183
        // Prepare query string. We do both urlencoded and non-encoded version
184
        // because older TYPO3 versions use un-encoded parameter names
185
        $queryConf = array(
186 2
            'exclude' => $this->pageParameterName . ',' .
187 2
                rawurlencode($this->pageParameterName) .
188 2
                ',cHash',
189
        );
190 2
        $additionalParams = urldecode($this->contentObject->getQueryArguments($queryConf));
191
192
        // Add page number
193 2
        if ($page > 0) {
194 2
            $additionalParams .= '&' . $this->pageParameterName . '=' . $page;
195
        }
196
197
        // Add extra query string from config
198 2
        $extraQueryString = trim($this->configuration['extraQueryString']);
199 2
        if (is_array($this->configuration['extraQueryString.'])) {
200
            $extraQueryString = $this->contentObject->stdWrap($extraQueryString,
201
                $this->configuration['extraQueryString.']);
202
        }
203
204 2
        if (strlen($extraQueryString) > 2 && $extraQueryString{0} == '&') {
205
            $additionalParams .= $extraQueryString;
206
        }
207
208
        // Assemble typolink configuration
209
        $conf = array(
210 2
            'parameter' => $GLOBALS['TSFE']->id,
211 2
            'additionalParams' => $additionalParams,
212
            'useCacheHash' => false,
213
        );
214
215 2
        return htmlspecialchars($this->contentObject->typoLink_URL($conf));
216
    }
217
}
218