DataSetPager   A
last analyzed

Complexity

Total Complexity 40

Size/Duplication

Total Lines 184
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 184
rs 9.2
c 0
b 0
f 0
wmc 40
lcom 1
cbo 2

8 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 27 6
A getRenderedPager() 0 3 1
A getRenderedRows() 0 3 1
A setDataSource() 0 3 1
A setPageSize() 0 3 1
A setRenderRowFnc() 0 6 1
A setRenderPagerFnc() 0 4 1
F render() 0 105 28

How to fix   Complexity   

Complex Class

Complex classes like DataSetPager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use DataSetPager, and based on these observations, apply Extract Interface, too.

1
<?php namespace EvolutionCMS\Support;
2
3
use EvolutionCMS\Interfaces\DataSetPagerInterface;
4
5
#
6
# DataSetPager Class
7
# Created By Raymond Irving 2-Dec,2004
8
# Based on CLASP 2.0 (www.claspdev.com)
9
# -----------------------------------------
10
# Licensed under the GPL
11
# -----------------------------------------
12
#
13
14
class DataSetPager implements DataSetPagerInterface{
15
16
	public $ds; // datasource
17
    public $pageSize;
18
    public $pageNumber;
19
    public $rows;
20
    public $pager;
21
    public $id;
22
23
	// normal page
24
    public $pageStyle;
25
    public $pageClass;
26
27
	// selected page
28
    public $selPageStyle;
29
    public $selPageClass;
30
    public $renderRowFnc;
31
    public $renderRowFncArgs;
32
    public $renderPagerFnc;
33
    public $renderPagerFncArgs;
34
    public static $dataSetPagerCnt;
35
36
    public function __construct($id, $ds, $pageSize = 10, $pageNumber = -1) {
37
		global $_PAGE; // use view state object
38
39
		// set id
40
		self::$dataSetPagerCnt++;
41
		$this->id = !empty($id) ? $id : "dsp" . self::$dataSetPagerCnt;
42
43
		// get pagenumber
44
		// by setting pager to -1 cause pager to load it's last page number
45
		if($pageNumber == -1) {
46
			$pageNumber = 1;
47
			if(isset($_GET["dpgn" . $this->id])) {
48
				$pageNumber = $_GET["dpgn" . $this->id];
49
			} elseif(isset($_PAGE['vs'][$id . '_dpgn'])) {
50
				$pageNumber = $_PAGE['vs'][$id . '_dpgn'];
51
			}
52
		}
53
		if(!is_numeric($pageNumber)) {
54
			$pageNumber = 1;
55
		}
56
57
		$this->ds = $ds; // datasource
58
		$this->pageSize = $pageSize;
59
		$this->pageNumber = $pageNumber;
60
		$this->rows = '';
61
		$this->pager = '';
62
	}
63
64
    public function getRenderedPager() {
65
		return $this->pager;
66
	}
67
68
    public function getRenderedRows() {
69
		return $this->rows;
70
	}
71
72
    public function setDataSource($ds) {
73
		$this->ds = $ds;
74
	}
75
76
    public function setPageSize($ps) {
77
		$this->pageSize = $ps;
78
	}
79
80
    public function setRenderRowFnc($fncName, $args = "") {
81
		$this->renderRowFnc = &$fncName;
82
		$this->renderRowFncArgs = $args;    // extra agruments
83
84
85
	}
86
87
    public function setRenderPagerFnc($fncName, $args = "") {
88
		$this->renderPagerFnc = $fncName;
89
		$this->renderPagerFncArgs = $args;    // extra agruments
90
	}
91
92
    public function render() {
93
		$modx = evolutionCMS(); global $_PAGE;
94
95
		$isDataset = $modx->getDatabase()->isResult($this->ds);
96
97
		if(!$this->selPageStyle) {
98
			$this->selPageStyle = "font-weight:bold";
99
		}
100
101
		// get total number of rows
102
		$tnr = ($isDataset) ? $modx->getDatabase()->getRecordCount($this->ds) : count($this->ds);
103
104
		// render: no records found
105
		if($tnr <= 0) {
106
			$fnc = $this->renderRowFnc;
107
			$args = $this->renderRowFncArgs;
108
			if(isset($fnc)) {
109
				if($args != "") {
110
					$this->rows .= $fnc(0, null, $args);
111
				} // if agrs was specified then we will pass three params
112
				else {
113
					$this->rows .= $fnc(0, null);
114
				}                 // otherwise two will be passed
115
			}
116
			return;
117
		}
118
119
		// get total pages
120
		$tp = ceil($tnr / $this->pageSize);
121
		if($this->pageNumber > $tp) {
122
			$this->pageNumber = 1;
123
		}
124
125
		// get page number
126
		$p = $this->pageNumber;
127
128
		// save page number to view state if available
129
		if(isset($_PAGE['vs'])) {
130
			$_PAGE['vs'][$this->id . '_dpgn'] = $p;
131
		}
132
133
		// render pager : renderPagerFnc($cuurentPage,$pagerNumber,$arguments="");
134
		if($tp > 1) {
135
		    $url = '';
136
			$fnc = $this->renderPagerFnc;
137
			$args = $this->renderPagerFncArgs;
138
			if(!isset($fnc)) {
139
				if($modx->isFrontend()) {
140
					$url = $modx->makeUrl($modx->documentIdentifier, '', '', 'full') . '?';
0 ignored issues
show
Deprecated Code introduced by
The method EvolutionCMS\Core::makeUrl() has been deprecated with message: use UrlProcessor::makeUrl()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
141
				} else {
142
					$url = $_SERVER['PHP_SELF'] . '?';
143
				}
144
				$i = 0;
145
				foreach($_GET as $n => $v) if($n != 'dpgn' . $this->id) {
146
					$i++;
147
					$url .= (($i > 1) ? "&" : "") . "$n=$v";
148
				}
149
				if($i >= 1) {
150
					$url .= "&";
151
				}
152
			}
153
			for($i = 1; $i <= $tp; $i++) {
154
				if(isset($fnc)) {
155
					if($args != "") {
156
						$this->pager .= $fnc($p, $i, $args);
157
					} else {
158
						$this->pager .= $fnc($p, $i);
159
					}
160
				} else {
161
					$this->pager .= ($p == $i) ? " <span class='" . $this->selPageClass . "' style='" . $this->selPageStyle . "'>$i</span> " : " <a href='" . $url . "dpgn" . $this->id . "=$i' class='" . $this->pageClass . "' style='" . $this->pageStyle . "'>$i</a> ";
162
				}
163
			}
164
		}
165
166
		// render row : renderRowFnc($rowNumber,$row,$arguments="")
167
		$fnc = $this->renderRowFnc;
168
		$args = $this->renderRowFncArgs;
169
170
		if(isset($fnc)) {
171
			$i = 1;
172
			$fncObject = is_object($fnc);
173
			$minitems = (($p - 1) * $this->pageSize) + 1;
174
			$maxitems = (($p - 1) * $this->pageSize) + $this->pageSize;
175
			while($i <= $maxitems && ($row = ($isDataset) ? $modx->getDatabase()->getRow($this->ds) : $this->ds[$i - 1])) {
176
				if($i >= $minitems && $i <= $maxitems) {
177
					if($fncObject) {
178
						if($args != "") {
179
							$this->rows .= $fnc->RenderRowFnc($i, $row, $args);
180
						} else {
181
							$this->rows .= $fnc->RenderRowFnc($i, $row);
182
						}
183
					} else {
184
						if($args != "") {
185
							$this->rows .= $fnc($i, $row, $args);
186
						} // if agrs was specified then we wil pass three params
187
						else {
188
							$this->rows .= $fnc($i, $row);
189
						}                 // otherwise two will be passed
190
					}
191
192
				}
193
				$i++;
194
			}
195
		}
196
	}
197
}
198