Total Complexity | 74 |
Total Lines | 567 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like CategoryHandler 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.
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 CategoryHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class CategoryHandler extends Extgallery\PersistableObjectHandler |
||
27 | { |
||
28 | //var $_nestedTree; |
||
29 | public $_photoHandler; |
||
30 | |||
31 | /** |
||
32 | * @param \XoopsDatabase|null $db |
||
33 | * @param $type |
||
34 | */ |
||
35 | public function __construct(\XoopsDatabase $db, $type) |
||
36 | { |
||
37 | parent::__construct($db, 'extgallery_' . $type . 'cat', \ucfirst($type) . 'Category', 'cat_id'); |
||
38 | //$this->_nestedTree = new NestedTree($db, 'extgallery_'.$type.'cat', 'cat_id', 'cat_pid', 'cat_id'); |
||
39 | $this->_photoHandler = Extgallery\Helper::getInstance()->getHandler(\ucfirst($type) . 'Photo'); |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * @param $data |
||
44 | * |
||
45 | * @return bool |
||
46 | */ |
||
47 | public function createCat($data) |
||
48 | { |
||
49 | $cat = $this->create(); |
||
50 | $cat->setVars($data); |
||
51 | |||
52 | if (!$this->hasValidParent($cat)) { |
||
53 | return false; |
||
54 | } |
||
55 | |||
56 | $this->insert($cat, true); |
||
57 | $this->rebuild(); |
||
58 | |||
59 | return true; |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * @param $data |
||
64 | * |
||
65 | * @return bool |
||
66 | */ |
||
67 | public function modifyCat($data) |
||
68 | { |
||
69 | $cat = $this->get($data['cat_id']); |
||
70 | $cat->setVars($data); |
||
71 | |||
72 | if (!$this->hasValidParent($cat)) { |
||
73 | return false; |
||
74 | } |
||
75 | $this->insert($cat, true); |
||
76 | |||
77 | // Rebluid the tree only if the structure is modified |
||
78 | if (isset($data['cat_pid']) || isset($data['nlevel']) || isset($data['nright']) || isset($data['nleft'])) { |
||
79 | $this->rebuild(); |
||
80 | } |
||
81 | |||
82 | return ''; |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * @param int $catId |
||
87 | */ |
||
88 | public function deleteCat($catId) |
||
89 | { |
||
90 | $children = $this->getDescendants($catId, false, true); |
||
91 | foreach ($children as $child) { |
||
92 | $this->_photoHandler->deletePhotoByCat($child->getVar('cat_id')); |
||
93 | $this->deleteCat($child->getVar('cat_id')); |
||
94 | } |
||
95 | $this->_photoHandler->deletePhotoByCat($catId); |
||
96 | $this->deleteById($catId); |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * @param int $id |
||
101 | * @param bool $includeSelf |
||
102 | * @param bool $childrenOnly |
||
103 | * @param bool $withRestrict |
||
104 | * @param string $permType |
||
105 | * |
||
106 | * @return array |
||
107 | */ |
||
108 | public function getDescendants( |
||
109 | $id = 0, |
||
110 | $includeSelf = false, |
||
111 | $childrenOnly = false, |
||
112 | $withRestrict = true, |
||
113 | $permType = 'public_access' |
||
114 | ) { |
||
115 | $cat = $this->get($id); |
||
116 | |||
117 | $nleft = $cat->getVar('nleft'); |
||
118 | $nright = $cat->getVar('nright'); |
||
119 | $parent_id = $cat->getVar('cat_id'); |
||
120 | |||
121 | $criteria = new \CriteriaCompo(); |
||
122 | |||
123 | if ($childrenOnly) { |
||
124 | $criteria->add(new \Criteria('cat_pid', $parent_id), 'OR'); |
||
125 | if ($includeSelf) { |
||
126 | $criteria->add(new \Criteria('cat_id', $parent_id)); |
||
127 | //$query = sprintf('select * from %s where %s = %d or %s = %d order by nleft', $this->table, $this->fields['id'], $parent_id, $this->fields['parent'], $parent_id); |
||
128 | }/* else { |
||
129 | //$query = sprintf('select * from %s where %s = %d order by nleft', $this->table, $this->fields['parent'], $parent_id); |
||
130 | }*/ |
||
131 | } else { |
||
132 | if ($nleft > 0 && $includeSelf) { |
||
133 | $criteria->add(new \Criteria('nleft', $nleft, '>=')); |
||
134 | $criteria->add(new \Criteria('nright', $nright, '<=')); |
||
135 | //$query = sprintf('select * from %s where nleft >= %d and nright <= %d order by nleft', $this->table, $nleft, $nright); |
||
136 | } else { |
||
137 | if ($nleft > 0) { |
||
138 | $criteria->add(new \Criteria('nleft', $nleft, '>')); |
||
139 | $criteria->add(new \Criteria('nright', $nright, '<')); |
||
140 | //$query = sprintf('select * from %s where nleft > %d and nright < %d order by nleft', $this->table, $nleft, $nright); |
||
141 | }/* else { |
||
142 | $query = sprintf('select * from %s order by nleft', $this->table); |
||
143 | }*/ |
||
144 | } |
||
145 | } |
||
146 | if ($withRestrict) { |
||
147 | $temp = $this->getCatRestrictCriteria($permType); |
||
148 | if (false !== $temp) { |
||
149 | $criteria->add($temp); |
||
150 | } |
||
151 | $temp = $this->getCatRestrictCriteria('public_displayed'); |
||
152 | if (false !== $temp) { |
||
153 | $criteria->add($temp); |
||
154 | } |
||
155 | } |
||
156 | $criteria->setSort('nleft'); |
||
157 | |||
158 | return $this->getObjects($criteria); |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * @param int $id |
||
163 | * @return mixed|null |
||
164 | */ |
||
165 | public function getCat($id = 0) |
||
166 | { |
||
167 | $criteria = new \CriteriaCompo(); |
||
168 | $temp = $this->getCatRestrictCriteria('public_displayed'); |
||
169 | if (false !== $temp) { |
||
170 | $criteria->add($temp); |
||
171 | } |
||
172 | |||
173 | $criteria->add(new \Criteria('cat_id', $id)); |
||
174 | $ret = $this->getObjects($criteria); |
||
175 | |||
176 | if (\count($ret) > 0) { |
||
177 | return $ret[0]; |
||
178 | } |
||
179 | |||
180 | return null; |
||
181 | } |
||
182 | |||
183 | public function hasValidParent() |
||
184 | { |
||
185 | exit('hasValidParent() method must be defined on sub classes'); |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * @param $cat |
||
190 | * |
||
191 | * @return bool |
||
192 | */ |
||
193 | public function _isAlbum($cat) |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * @param Extgallery\Category $cat |
||
202 | * |
||
203 | * @return mixed |
||
204 | */ |
||
205 | public function nbPhoto(&$cat) |
||
206 | { |
||
207 | /** @var Extgallery\CategoryHandler $this ->_photoHandler */ |
||
208 | return $this->_photoHandler->nbPhoto($cat); |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * @param int $id |
||
213 | * @param bool $includeSelf |
||
214 | * |
||
215 | * @return array |
||
216 | */ |
||
217 | public function getPath($id = 0, $includeSelf = false) |
||
218 | { |
||
219 | $cat = $this->get($id); |
||
220 | if (null === $cat) { |
||
221 | return []; |
||
222 | } |
||
223 | |||
224 | $criteria = new \CriteriaCompo(); |
||
225 | if ($includeSelf) { |
||
226 | $criteria->add(new \Criteria('nleft', $cat->getVar('nleft'), '<=')); |
||
227 | $criteria->add(new \Criteria('nright', $cat->getVar('nright'), '>=')); |
||
228 | //$query = sprintf('select * from %s where nleft <= %d and nright >= %d order by nlevel', $this->table, $node['nleft'], $node['nright']); |
||
229 | } else { |
||
230 | $criteria->add(new \Criteria('nleft', $cat->getVar('nleft'), '<')); |
||
231 | $criteria->add(new \Criteria('nright', $cat->getVar('nright'), '>')); |
||
232 | //$query = sprintf('select * from %s where nleft < %d and nright > %d order by nlevel', $this->table, $node['nleft'], $node['nright']); |
||
233 | } |
||
234 | $temp = $this->getCatRestrictCriteria(); |
||
235 | if (\is_object($temp)) { |
||
236 | $criteria->add($temp); |
||
237 | } |
||
238 | $temp2 = $this->getCatRestrictCriteria('public_displayed'); |
||
239 | if (\is_object($temp2)) { |
||
240 | $criteria->add($temp2); |
||
241 | } |
||
242 | $criteria->setSort('nlevel'); |
||
243 | |||
244 | return $this->getObjects($criteria); |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * @return array |
||
249 | */ |
||
250 | public function getTree() |
||
251 | { |
||
252 | return $this->getDescendants(0, false, false, false); |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * @param int $id |
||
257 | * @param bool $includeSelf |
||
258 | * |
||
259 | * @return array |
||
260 | */ |
||
261 | public function getChildren($id = 0, $includeSelf = false) |
||
262 | { |
||
263 | return $this->getDescendants($id, $includeSelf, true); |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * @param int $id |
||
268 | * |
||
269 | * @return int |
||
270 | */ |
||
271 | public function nbAlbum($id = 0) |
||
272 | { |
||
273 | $criteria = new \CriteriaCompo(new \Criteria('nright - nleft', 1)); |
||
274 | //$query = sprintf('select count(*) as num_leef from %s where nright - nleft = 1', $this->table); |
||
275 | if (0 != $id) { |
||
276 | $cat = $this->get($id); |
||
277 | $criteria->add(new \Criteria('nleft', $cat->getVar('nleft'), '>')); |
||
278 | $criteria->add(new \Criteria('nright', $cat->getVar('nright'), '<')); |
||
279 | //$query .= sprintf(' AND nleft > %d AND nright < %d', $node['nleft'], $node['nright']); |
||
280 | } |
||
281 | |||
282 | return $this->getCount($criteria); |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * @param $name |
||
287 | * @param $selectMode |
||
288 | * @param bool $addEmpty |
||
289 | * @param int $selected |
||
290 | * @param string $extra |
||
291 | * @param bool $displayWeight |
||
292 | * @param string $permType |
||
293 | * |
||
294 | * @return string |
||
295 | */ |
||
296 | public function getSelect( |
||
297 | $name, |
||
298 | $selectMode, |
||
299 | $addEmpty = false, |
||
300 | $selected = 0, |
||
301 | $extra = '', |
||
302 | $displayWeight = false, |
||
303 | $permType = 'public_access' |
||
304 | ) { |
||
305 | $cats = $this->getDescendants(0, false, false, true, $permType); |
||
306 | |||
307 | return $this->makeSelect($cats, $name, $selectMode, $addEmpty, $selected, $extra, $displayWeight); |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * @param $name |
||
312 | * @param bool $addEmpty |
||
313 | * @param int $selected |
||
314 | * @param string $extra |
||
315 | * @param string $permType |
||
316 | * |
||
317 | * @return string |
||
318 | */ |
||
319 | public function getLeafSelect($name, $addEmpty = false, $selected = 0, $extra = '', $permType = 'public_access') |
||
320 | { |
||
321 | return $this->getSelect($name, 'node', $addEmpty, $selected, $extra, false, $permType); |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * @param $name |
||
326 | * @param bool $addEmpty |
||
327 | * @param int $selected |
||
328 | * @param string $extra |
||
329 | * |
||
330 | * @return string |
||
331 | */ |
||
332 | public function getNodeSelect($name, $addEmpty = false, $selected = 0, $extra = '') |
||
333 | { |
||
334 | return $this->getSelect($name, 'leaf', $addEmpty, $selected, $extra); |
||
335 | } |
||
336 | |||
337 | /** |
||
338 | * @param array $cats |
||
339 | * @param string $name |
||
340 | * @param string $selectMode |
||
341 | * @param $addEmpty |
||
342 | * @param $selected |
||
343 | * @param $extra |
||
344 | * @param $displayWeight |
||
345 | * |
||
346 | * @return string |
||
347 | */ |
||
348 | public function makeSelect($cats, $name, $selectMode, $addEmpty, $selected, $extra, $displayWeight) |
||
349 | { |
||
350 | $ret = '<select name="' . $name . '" id="' . $name . '"' . $extra . '>'; |
||
351 | if ($addEmpty) { |
||
352 | $ret .= '<option value="0">-----</option>'; |
||
353 | } |
||
354 | /** @var Extgallery\Category $cat */ |
||
355 | foreach ($cats as $cat) { |
||
356 | $disableOption = ''; |
||
357 | if ('node' === $selectMode && (1 != $cat->getVar('nright') - $cat->getVar('nleft'))) { |
||
358 | // If the brownser is IE the parent cat isn't displayed |
||
359 | // if (preg_match('`MSIE`', $_SERVER['HTTP_USER_AGENT'])) { |
||
360 | if (false !== mb_strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')) { |
||
361 | continue; |
||
362 | } |
||
363 | $disableOption = ' disabled="disabled"'; |
||
364 | } elseif ('leaf' === $selectMode && (1 == $cat->getVar('cat_isalbum'))) { |
||
365 | continue; |
||
366 | } |
||
367 | |||
368 | $selectedOption = ''; |
||
369 | if ($cat->getVar('cat_id') == $selected) { |
||
370 | $selectedOption = ' selected'; |
||
371 | } |
||
372 | |||
373 | $prefix = ''; |
||
374 | for ($i = 0; $i < $cat->getVar('nlevel') - 1; ++$i) { |
||
375 | $prefix .= '--'; |
||
376 | } |
||
377 | $catName = $prefix . ' ' . $cat->getVar('cat_name'); |
||
378 | if ($displayWeight) { |
||
379 | $catName .= ' [' . $cat->getVar('cat_weight') . ']'; |
||
380 | } |
||
381 | |||
382 | $ret .= '<option value="' . $cat->getVar('cat_id') . '"' . $selectedOption . '' . $disableOption . '>' . $catName . '</option>'; |
||
383 | } |
||
384 | $ret .= '</select>'; |
||
385 | |||
386 | return $ret; |
||
387 | } |
||
388 | |||
389 | /** |
||
390 | * @param array $selected |
||
391 | * |
||
392 | * @return string |
||
393 | */ |
||
394 | public function getBlockSelect($selected = []) |
||
395 | { |
||
396 | $cats = $this->getDescendants(); |
||
397 | $ret = '<select name="options[]" multiple="multiple">'; |
||
398 | $selectedOption = ''; |
||
399 | $allCat = \in_array(0, $selected); |
||
400 | if ($allCat) { |
||
401 | $selectedOption = ' selected'; |
||
402 | } |
||
403 | $ret .= '<option value="0"' . $selectedOption . '>' . \_MB_EXTGALLERY_ALL_CATEGORIES . '</option>'; |
||
404 | foreach ($cats as $cat) { |
||
405 | $prefix = ''; |
||
406 | for ($i = 0; $i < $cat->getVar('nlevel') - 1; ++$i) { |
||
407 | $prefix .= '-'; |
||
408 | } |
||
409 | $selectedOption = ''; |
||
410 | $disableOption = ''; |
||
411 | |||
412 | if (!$allCat && \in_array($cat->getVar('cat_id'), $selected)) { |
||
413 | $selectedOption = ' selected'; |
||
414 | } |
||
415 | |||
416 | if (1 != $cat->getVar('nright') - $cat->getVar('nleft')) { |
||
417 | $disableOption = ' disabled="disabled"'; |
||
418 | } |
||
419 | |||
420 | $ret .= '<option value="' . $cat->getVar('cat_id') . '"' . $selectedOption . '' . $disableOption . '>' . $prefix . ' ' . $cat->getVar('cat_name') . '</option>'; |
||
421 | } |
||
422 | $ret .= '</select>'; |
||
423 | |||
424 | return $ret; |
||
425 | } |
||
426 | |||
427 | /** |
||
428 | * @return array |
||
429 | */ |
||
430 | public function getTreeWithChildren() |
||
466 | } |
||
467 | |||
468 | /** |
||
469 | * Rebuilds the tree data and saves it to the database |
||
470 | */ |
||
471 | public function rebuild() |
||
525 | } |
||
526 | } |
||
527 | |||
528 | /** |
||
529 | * Generate the tree data. A single call to this generates the n-values for |
||
530 | * 1 node in the tree. This function assigns the passed in n value as the |
||
531 | * node's nleft value. It then processes all the node's children (which |
||
532 | * in turn recursively processes that node's children and so on), and when |
||
533 | * it is finally done, it takes the update n-value and assigns it as its |
||
534 | * nright value. Because it is passed as a reference, the subsequent changes |
||
535 | * in subrequests are held over to when control is returned so the nright |
||
536 | * can be assigned. |
||
537 | * |
||
538 | * @param array &$arr A reference to the data array, since we need to |
||
539 | * be able to update the data in it |
||
540 | * @param int $id The ID of the current node to process |
||
541 | * @param int $level The nlevel to assign to the current node |
||
542 | * @param int &$n A reference to the running tally for the n-value |
||
543 | */ |
||
544 | public function _generateTreeData(&$arr, $id, $level, &$n) |
||
555 | } |
||
556 | |||
557 | /** |
||
558 | * @param string $permType |
||
559 | * |
||
560 | * @return bool|\Criteria |
||
561 | */ |
||
562 | public function getCatRestrictCriteria($permType = 'public_access') |
||
585 | } |
||
586 | |||
587 | /** |
||
588 | * @return Extgallery\PublicPermHandler |
||
589 | */ |
||
590 | public function getPermHandler() |
||
593 | } |
||
594 | } |
||
595 |
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.