Failed Conditions
Pull Request — newinternal (#527)
by Simon
17:20 queued 07:22
created

PagedInternalPageBase   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
B setupPageData() 0 47 6
A setSearchHelper() 0 4 1
A setupLimits() 0 19 3
A setupFormParameters() 0 9 2
1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 *                                                                            *
5
 * All code in this file is released into the public domain by the ACC        *
6
 * Development Team. Please see team.json for a list of contributors.         *
7
 ******************************************************************************/
8
9
namespace Waca\Tasks;
10
11
use Waca\Helpers\SearchHelpers\SearchHelperBase;
12
use Waca\WebRequest;
13
14
abstract class PagedInternalPageBase extends InternalPageBase
15
{
16
    /** @var SearchHelperBase */
17
    private $searchHelper;
18
    private $page;
19
    private $limit;
20
21
    /**
22
     * Sets up the pager with the current page, current limit, and total number of records.
23
     *
24
     * @param int   $count
25
     * @param array $formParameters
26
     */
27
    protected function setupPageData($count, $formParameters)
28
    {
29
        $page = $this->page;
30
        $limit = $this->limit;
31
32
        // The number of pages on the pager to show. Must be odd
33
        $pageLimit = 9;
34
35
        $pageData = array(
36
            // Can the user go to the previous page?
37
            'canprev'   => $page != 1,
38
            // Can the user go to the next page?
39
            'cannext'   => ($page * $limit) < $count,
40
            // Maximum page number
41
            'maxpage'   => ceil($count / $limit),
42
            // Limit to the number of pages to display
43
            'pagelimit' => $pageLimit,
44
        );
45
46
        // number of pages either side of the current to show
47
        $pageMargin = (($pageLimit - 1) / 2);
48
49
        // Calculate the number of pages either side to show - this is for situations like:
50
        //  [1]  [2] [[3]] [4]  [5]  [6]  [7]  [8]  [9] - where you can't just use the page margin calculated
51
        $pageData['lowpage'] = max(1, $page - $pageMargin);
52
        $pageData['hipage'] = min($pageData['maxpage'], $page + $pageMargin);
53
        $pageCount = ($pageData['hipage'] - $pageData['lowpage']) + 1;
54
55
        if ($pageCount < $pageLimit) {
56
            if ($pageData['lowpage'] == 1 && $pageData['hipage'] < $pageData['maxpage']) {
57
                $pageData['hipage'] = min($pageLimit, $pageData['maxpage']);
58
            }
59
            elseif ($pageData['lowpage'] > 1 && $pageData['hipage'] == $pageData['maxpage']) {
60
                $pageData['lowpage'] = max(1, $pageData['maxpage'] - $pageLimit + 1);
61
            }
62
        }
63
64
        // Put the range of pages into the page data
65
        $pageData['pages'] = range($pageData['lowpage'], $pageData['hipage']);
66
67
        $this->assign("pagedata", $pageData);
68
69
        $this->assign("limit", $limit);
70
        $this->assign("page", $page);
71
72
        $this->setupFormParameters($formParameters);
73
    }
74
75
    protected function setSearchHelper(SearchHelperBase $searchHelper)
76
    {
77
        $this->searchHelper = $searchHelper;
78
    }
79
80
    protected function setupLimits()
81
    {
82
        $limit = WebRequest::getInt('limit');
83
        if ($limit === null) {
84
            $limit = 100;
85
        }
86
87
        $page = WebRequest::getInt('page');
88
        if ($page === null) {
89
            $page = 1;
90
        }
91
92
        $offset = ($page - 1) * $limit;
93
94
        $this->searchHelper->limit($limit, $offset);
95
96
        $this->page = $page;
97
        $this->limit = $limit;
98
    }
99
100
    private function setupFormParameters($formParameters)
101
    {
102
        $formParameters['limit'] = $this->limit;
103
        $this->assign('searchParamsUrl', http_build_query($formParameters, '', '&amp;'));
104
105
        foreach ($formParameters as $key => $value)  {
106
            $this->assign($key, $value);
107
        }
108
    }
109
}