Total Complexity | 69 |
Total Lines | 502 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like DirectoryHandler 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 DirectoryHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
35 | class DirectoryHandler extends \XoopsPersistableObjectHandler |
||
36 | { |
||
37 | /** |
||
38 | * Constructor |
||
39 | * |
||
40 | * @param \XoopsDatabase $db |
||
41 | */ |
||
42 | public function __construct(\XoopsDatabase $db) |
||
43 | { |
||
44 | parent::__construct($db, 'wgfilemanager_directory', Directory::class, 'id', 'name'); |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * @param bool $isNew |
||
49 | * |
||
50 | * @return object |
||
51 | */ |
||
52 | public function create($isNew = true) |
||
53 | { |
||
54 | return parent::create($isNew); |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * retrieve a field |
||
59 | * |
||
60 | * @param int $id field id |
||
61 | * @param null $fields fields |
||
|
|||
62 | * @return \XoopsObject|null reference to the {@link Get} object |
||
63 | */ |
||
64 | public function get($id = null, $fields = null) |
||
65 | { |
||
66 | return parent::get($id, $fields); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * get inserted id |
||
71 | * |
||
72 | * @return int reference to the {@link Get} object |
||
73 | */ |
||
74 | public function getInsertId() |
||
75 | { |
||
76 | return $this->db->getInsertId(); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Get Count Directory in the database |
||
81 | * @param int $start |
||
82 | * @param int $limit |
||
83 | * @param string $sort |
||
84 | * @param string $order |
||
85 | * @return int |
||
86 | */ |
||
87 | public function getCountDirectory($start = 0, $limit = 0, $sort = 'id ASC, name', $order = 'ASC') |
||
88 | { |
||
89 | $crCountDirectory = new \CriteriaCompo(); |
||
90 | $crCountDirectory = $this->getDirectoryCriteria($crCountDirectory, $start, $limit, $sort, $order); |
||
91 | return $this->getCount($crCountDirectory); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Get All Directory in the database |
||
96 | * @param int $start |
||
97 | * @param int $limit |
||
98 | * @param string $sort |
||
99 | * @param string $order |
||
100 | * @return array |
||
101 | */ |
||
102 | public function getAllDirectory($start = 0, $limit = 0, $sort = 'id ASC, name', $order = 'ASC') |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Get Criteria Directory |
||
111 | * @param $crDirectory |
||
112 | * @param int $start |
||
113 | * @param int $limit |
||
114 | * @param string $sort |
||
115 | * @param string $order |
||
116 | * @return int |
||
117 | */ |
||
118 | private function getDirectoryCriteria($crDirectory, $start, $limit, $sort, $order) |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Get full path of given parent directory |
||
129 | * |
||
130 | * @param int $parent_id |
||
131 | * @return string |
||
132 | */ |
||
133 | public function getFullPath($parent_id) { |
||
134 | |||
135 | $path = ''; |
||
136 | if ($parent_id > 0) { |
||
137 | $path = $this->getFullPathRecursive($parent_id); |
||
138 | } |
||
139 | if ('' === $path) { |
||
140 | return ''; |
||
141 | } |
||
142 | $pathArray = \explode('/', $path); |
||
143 | \krsort($pathArray); |
||
144 | |||
145 | return \implode('/', \array_filter($pathArray)); |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * Get full path of given parent directory |
||
150 | * |
||
151 | * @param int $parent_id |
||
152 | * @return string |
||
153 | */ |
||
154 | public function getFullPathRecursive($parent_id) { |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * Check whether given path is a directory |
||
168 | * |
||
169 | * @param string $path |
||
170 | * @return boolean |
||
171 | */ |
||
172 | public function existDirectory ($path) { |
||
175 | |||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Create directory from given path |
||
180 | * |
||
181 | * @param string $path |
||
182 | * @return boolean |
||
183 | */ |
||
184 | public function createDirectory($path) { |
||
185 | |||
186 | FilesManagement::createFolder(\WGFILEMANAGER_REPO_PATH . $path); |
||
187 | |||
188 | } |
||
189 | |||
190 | /** |
||
191 | * Rename directory |
||
192 | * |
||
193 | * @param string $oldDirname |
||
194 | * @param string $newDirname |
||
195 | * @return boolean |
||
196 | */ |
||
197 | public function renameDirectory($oldDirname, $newDirname) |
||
210 | } |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * Delete directory from given path |
||
215 | * |
||
216 | * @param string $path |
||
217 | * @return boolean |
||
218 | */ |
||
219 | public function deleteDirectory($path) |
||
220 | { |
||
221 | $fullPath = \WGFILEMANAGER_REPO_PATH . $path; |
||
222 | |||
223 | if (!FilesManagement::deleteDirectory($fullPath)) { |
||
224 | return false; |
||
225 | } |
||
226 | |||
227 | return !$this->existDirectory($fullPath); |
||
228 | |||
229 | } |
||
230 | |||
231 | /** |
||
232 | * Check whether given directory is used as parent |
||
233 | * |
||
234 | * @param int $dirId |
||
235 | * @return boolean |
||
236 | */ |
||
237 | public function dirIsParent ($dirId) { |
||
238 | $crCountDirectory = new \CriteriaCompo(); |
||
239 | $crCountDirectory->add(new \Criteria('parent_id', $dirId)); |
||
240 | |||
241 | return $this->getCount($crCountDirectory) > 0; |
||
242 | |||
243 | } |
||
244 | |||
245 | /** |
||
246 | * Count subdirectories from given directory |
||
247 | * |
||
248 | * @param int $dirId |
||
249 | * @return integer |
||
250 | */ |
||
251 | public function countSubDirs($dirId) { |
||
252 | |||
253 | $crSubDir = new \CriteriaCompo(); |
||
254 | $crSubDir->add(new \Criteria('parent_id', $dirId)); |
||
255 | |||
256 | return $this->getCount($crSubDir) ; |
||
257 | |||
258 | } |
||
259 | |||
260 | /** |
||
261 | * Count files in given directory |
||
262 | * |
||
263 | * @param string $fullpath |
||
264 | * @return integer |
||
265 | */ |
||
266 | public function countFiles($fullpath) { |
||
267 | $file_new = []; |
||
268 | $path = \WGFILEMANAGER_REPO_PATH . $fullpath; |
||
269 | if (!\file_exists($path)) { |
||
270 | return -1; |
||
271 | } |
||
272 | $files = scandir($path); |
||
273 | for($i = 0 ; $i < count($files) ; $i++){ |
||
274 | if(\is_file($path . '/' . $files[$i]) && $files[$i] !='.' && $files[$i] !='..' && $files[$i] !='index.php' && $files[$i] !='index.html') { |
||
275 | $file_new[] = $files[$i]; |
||
276 | } |
||
277 | } |
||
278 | |||
279 | return count($file_new); |
||
280 | } |
||
281 | |||
282 | /** |
||
283 | * Delete data from all subdirectories from given directory |
||
284 | * |
||
285 | * @param int $dirId |
||
286 | * @return boolean |
||
287 | */ |
||
288 | public function deleteSubDirData($dirId) { |
||
306 | |||
307 | } |
||
308 | |||
309 | /** |
||
310 | * Move directory |
||
311 | * |
||
312 | * @param string $pathSource |
||
313 | * @param string $pathDest |
||
314 | * @return boolean |
||
315 | */ |
||
316 | public function moveDirectory($pathSource, $pathDest) { |
||
326 | } |
||
327 | |||
328 | /** |
||
329 | * Returns an array directories |
||
330 | * |
||
331 | * @param int $dirId |
||
332 | * @param int $dirCurrent |
||
333 | * @param int $levelCurrent |
||
334 | * @param string $sortBy |
||
335 | * @param string $orderBy |
||
336 | * @return array |
||
337 | */ |
||
338 | public function getDirList($dirId, $dirCurrent, $levelCurrent = 1, $sortBy = 'weight ASC, id', $orderBy = 'ASC') { |
||
339 | |||
340 | $result = []; |
||
341 | /* if (0 === $dirId) { |
||
342 | $result[0]['id'] = 0; |
||
343 | $result[0]['parent_id'] = 0; |
||
344 | $result[0]['name'] = \_MA_WGFILEMANAGER_DIRECTORY_HOME; |
||
345 | $result[0]['state'] = 0 === $dirCurrent ? 'open' : 'closed'; |
||
346 | $result[0]['class'] = 'home'; |
||
347 | $result[0]['level'] = 0; |
||
348 | $crSubDir = new \CriteriaCompo(); |
||
349 | $crSubDir->add(new \Criteria('parent_id', $dirId)); |
||
350 | $result[0]['count_subdirs'] = $this->getCount($crSubDir); |
||
351 | $result[0]['count_files'] = $this->countFiles(''); |
||
352 | $result[0]['subdirs'] = []; |
||
353 | }*/ |
||
354 | //create list of parents |
||
355 | $parents = []; |
||
356 | $parentId = 0; |
||
357 | $dirCurrObj = $this->get($dirCurrent); |
||
358 | if (\is_object($dirCurrObj)) { |
||
359 | $parentId = $dirCurrObj->getVar('parent_id'); |
||
360 | } |
||
361 | $parents[] = $parentId; |
||
362 | while ($parentId > 0) { |
||
363 | $parentId = $this->get($parentId)->getVar('parent_id'); |
||
364 | $parents[] = $parentId; |
||
365 | } |
||
366 | |||
367 | $levelCurrent++; |
||
368 | $crSubDir = new \CriteriaCompo(); |
||
369 | $crSubDir->add(new \Criteria('parent_id', $dirId)); |
||
370 | $crSubDir->setSort($sortBy); |
||
371 | $crSubDir->setOrder($orderBy); |
||
372 | if ($this->getCount($crSubDir) > 0) { |
||
373 | $directoryAll = $this->getAll($crSubDir); |
||
374 | foreach (\array_keys($directoryAll) as $i) { |
||
375 | $directory = $directoryAll[$i]->getValuesDir(); |
||
376 | $result[$i]['id'] = $directory['id']; |
||
377 | $result[$i]['parent_id'] = $directory['parent_id']; |
||
378 | $result[$i]['name'] = $directory['name']; |
||
379 | $result[$i]['state'] = $i === $dirCurrent ? 'open' : 'closed'; |
||
380 | $result[$i]['highlight'] = $i === $dirCurrent; |
||
381 | $result[$i]['show'] = in_array($i, $parents); |
||
382 | $result[$i]['count_subdirs'] = $directory['count_subdirs']; |
||
383 | $result[$i]['count_files'] = $directory['count_files']; |
||
384 | $result[$i]['level'] = $levelCurrent; |
||
385 | $result[$i]['weight'] = $directory['weight']; |
||
386 | $result[$i]['favorite_id'] = $directory['favorite_id']; |
||
387 | if ($directory['count_subdirs'] > 0) { |
||
388 | $result[$i]['subdirs'] = $this->getDirList($i, $dirCurrent, $levelCurrent); |
||
389 | } |
||
390 | } |
||
391 | } |
||
392 | |||
393 | return $result; |
||
394 | |||
395 | } |
||
396 | |||
397 | /** |
||
398 | * Returns an array directories for form select |
||
399 | * |
||
400 | * @param int $dirId |
||
401 | * @return array |
||
402 | */ |
||
403 | public function getDirListFormSelect($dirId) { |
||
404 | |||
405 | $result = []; |
||
406 | $crSubDir = new \CriteriaCompo(); |
||
407 | $crSubDir->add(new \Criteria('parent_id', $dirId)); |
||
408 | $crSubDir->setSort('weight ASC, id'); |
||
409 | $crSubDir->setOrder('ASC'); |
||
410 | if ($this->getCount($crSubDir) > 0) { |
||
411 | $directoryAll = $this->getAll($crSubDir); |
||
412 | foreach (\array_keys($directoryAll) as $i) { |
||
413 | $directory = $directoryAll[$i]->getValuesDir(); |
||
414 | $name = $directory['name']; |
||
415 | if ($dirId > 0) { |
||
416 | $level = \mb_substr_count($directory['fullpath'], '/'); |
||
417 | $name = \str_repeat('- ', $level) . $name; |
||
418 | } |
||
419 | $result[$i] = [$directory['id'] => $name]; |
||
420 | if ($directory['count_subdirs'] > 0) { |
||
421 | $result[$i][]= $this->getDirListFormSelect($i); |
||
422 | } |
||
423 | } |
||
424 | } |
||
425 | |||
426 | return $result; |
||
427 | } |
||
428 | |||
429 | /** |
||
430 | * Returns an array directories for breadcrumbs |
||
431 | * |
||
432 | * @param int $dirId |
||
433 | * @return array |
||
434 | */ |
||
435 | public function getDirListBreadcrumb($dirId) { |
||
436 | |||
437 | $result = []; |
||
438 | do { |
||
439 | $dirObj = $this->get($dirId); |
||
440 | if (\is_object($dirObj)) { |
||
441 | if ($dirId > 1) { |
||
442 | $result[$dirId] = $dirObj->getVar('name'); |
||
443 | } |
||
444 | $dirId = $dirObj->getVar('parent_id'); |
||
445 | } else { |
||
446 | $dirId = 0; |
||
447 | } |
||
448 | } while ($dirId > 0); |
||
449 | |||
450 | return $result; |
||
451 | } |
||
452 | |||
453 | /** |
||
454 | * Returns an array of directories |
||
455 | * |
||
456 | * @param int $dirId |
||
457 | * @param string $sortBy |
||
458 | * @param string $orderBy |
||
459 | * @return array |
||
460 | */ |
||
461 | public function getSubDirList($dirId, $sortBy = 'weight ASC, id', $orderBy = 'ASC') { |
||
462 | |||
463 | $result = []; |
||
464 | $crSubDir = new \CriteriaCompo(); |
||
465 | $crSubDir->add(new \Criteria('parent_id', $dirId)); |
||
466 | $crSubDir->setSort($sortBy); |
||
467 | $crSubDir->setOrder($orderBy); |
||
468 | if ($this->getCount($crSubDir) > 0) { |
||
469 | $directoryAll = $this->getAll($crSubDir); |
||
470 | foreach (\array_keys($directoryAll) as $i) { |
||
471 | $result[$i] = $directoryAll[$i]->getValuesDir(); |
||
472 | } |
||
473 | } |
||
474 | |||
475 | return $result; |
||
476 | |||
477 | } |
||
478 | |||
479 | /** |
||
480 | * Returns an array of favorite directories |
||
481 | * |
||
482 | * @return array |
||
483 | */ |
||
484 | public function getFavDirList() { |
||
485 | $result = []; |
||
486 | //get current user |
||
487 | $userUid = 0; |
||
488 | if (isset($GLOBALS['xoopsUser']) && \is_object($GLOBALS['xoopsUser'])) { |
||
489 | $userUid = $GLOBALS['xoopsUser']->uid(); |
||
490 | } |
||
491 | if ($userUid > 0) { |
||
492 | $crDirectory = new \CriteriaCompo(); |
||
493 | $directoryCount = $this->getCount($crDirectory); |
||
494 | if ($directoryCount > 0) { |
||
495 | $crDirectory->setSort('name'); |
||
496 | $crDirectory->setOrder('asc'); |
||
497 | $directoryAll = $this->getAll($crDirectory); |
||
498 | foreach (\array_keys($directoryAll) as $i) { |
||
499 | $dirValues = $directoryAll[$i]->getValuesDir(); |
||
500 | if ((int)$dirValues['favorite_id'] > 0) { |
||
501 | $result[] = $dirValues; |
||
502 | } |
||
503 | } |
||
504 | } |
||
505 | } |
||
506 | return $result; |
||
507 | } |
||
508 | |||
509 | /** |
||
510 | * Returns an array directories |
||
511 | * |
||
512 | * @param int $parentId |
||
513 | * @return boolean |
||
514 | */ |
||
515 | public function setDirWeight($parentId) { |
||
537 | |||
538 | } |
||
539 | |||
540 | } |
||
541 |