Total Complexity | 82 |
Total Lines | 524 |
Duplicated Lines | 0 % |
Changes | 25 | ||
Bugs | 1 | Features | 1 |
Complex classes like CategoryController 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 CategoryController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class CategoryController extends Controller |
||
29 | { |
||
30 | |||
31 | private $userId; |
||
32 | private $l10n; |
||
33 | private $db; |
||
34 | private $tagger; |
||
35 | private $tagManager; |
||
36 | private $rootFolder; |
||
37 | private $logger; |
||
38 | private $DBController; |
||
39 | |||
40 | public function __construct( |
||
41 | $appName, |
||
42 | IRequest $request, |
||
43 | $userId, |
||
44 | IL10N $l10n, |
||
45 | IDBConnection $db, |
||
46 | ITagManager $tagManager, |
||
47 | IRootFolder $rootFolder, |
||
48 | ILogger $logger, |
||
49 | DbController $DBController |
||
50 | ) |
||
51 | { |
||
52 | parent::__construct($appName, $request); |
||
53 | $this->userId = $userId; |
||
54 | $this->l10n = $l10n; |
||
55 | $this->db = $db; |
||
56 | $this->tagManager = $tagManager; |
||
57 | $this->tagger = null; |
||
58 | $this->rootFolder = $rootFolder; |
||
59 | $this->logger = $logger; |
||
60 | $this->DBController = $DBController; |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * Get the items for the selected category |
||
65 | * |
||
66 | * @NoAdminRequired |
||
67 | * @param $category |
||
68 | * @return JSONResponse |
||
69 | */ |
||
70 | public function getCategoryItems($category) |
||
71 | { |
||
72 | $SQL = null; |
||
73 | $aPlaylists = array(); |
||
74 | if ($category === 'Artist') { |
||
75 | $SQL = 'SELECT DISTINCT(`AT`.`artist_id`) AS `id`, `AA`.`name`, LOWER(`AA`.`name`) AS `lower` |
||
76 | FROM `*PREFIX*audioplayer_tracks` `AT` |
||
77 | JOIN `*PREFIX*audioplayer_artists` `AA` |
||
78 | ON `AA`.`id` = `AT`.`artist_id` |
||
79 | WHERE `AT`.`user_id` = ? |
||
80 | ORDER BY LOWER(`AA`.`name`) ASC |
||
81 | '; |
||
82 | } elseif ($category === 'Genre') { |
||
83 | $SQL = 'SELECT `id`, `name`, LOWER(`name`) AS `lower` |
||
84 | FROM `*PREFIX*audioplayer_genre` |
||
85 | WHERE `user_id` = ? |
||
86 | ORDER BY LOWER(`name`) ASC |
||
87 | '; |
||
88 | } elseif ($category === 'Year') { |
||
89 | $SQL = 'SELECT DISTINCT(`year`) AS `id` ,`year` AS `name` |
||
90 | FROM `*PREFIX*audioplayer_tracks` |
||
91 | WHERE `user_id` = ? |
||
92 | ORDER BY `id` ASC |
||
93 | '; |
||
94 | } elseif ($category === 'Title') { |
||
95 | $SQL = "SELECT distinct('0') as `id` ,'" . $this->l10n->t('All Titles') . "' as `name` |
||
96 | FROM `*PREFIX*audioplayer_tracks` |
||
97 | WHERE `user_id` = ? |
||
98 | "; |
||
99 | } elseif ($category === 'Playlist') { |
||
100 | $aPlaylists[] = array('id' => 'X1', 'name' => $this->l10n->t('Favorites')); |
||
101 | $aPlaylists[] = array('id' => 'X2', 'name' => $this->l10n->t('Recently Added')); |
||
102 | $aPlaylists[] = array('id' => 'X3', 'name' => $this->l10n->t('Recently Played')); |
||
103 | $aPlaylists[] = array('id' => 'X4', 'name' => $this->l10n->t('Most Played')); |
||
104 | //https://github.com/Rello/audioplayer/issues/442 |
||
105 | $aPlaylists[] = array('id' => 'X5', 'name' => $this->l10n->t('50 Random Tracks')); |
||
106 | $aPlaylists[] = array('id' => '', 'name' => ''); |
||
107 | |||
108 | // Stream files are shown directly |
||
109 | $SQL = 'SELECT `file_id` AS `id`, `title` AS `name`, LOWER(`title`) AS `lower` |
||
110 | FROM `*PREFIX*audioplayer_streams` |
||
111 | WHERE `user_id` = ? |
||
112 | ORDER BY LOWER(`title`) ASC |
||
113 | '; |
||
114 | $stmt = $this->db->prepare($SQL); |
||
115 | $stmt->execute(array($this->userId)); |
||
116 | $results = $stmt->fetchAll(); |
||
117 | foreach ($results as $row) { |
||
118 | array_splice($row, 2, 1); |
||
119 | $row['id'] = 'S' . $row['id']; |
||
120 | $aPlaylists[] = $row; |
||
121 | } |
||
122 | $aPlaylists[] = array('id' => '', 'name' => ''); |
||
123 | |||
124 | // regular playlists are selected |
||
125 | $SQL = 'SELECT `id`,`name`, LOWER(`name`) AS `lower` |
||
126 | FROM `*PREFIX*audioplayer_playlists` |
||
127 | WHERE `user_id` = ? |
||
128 | ORDER BY LOWER(`name`) ASC |
||
129 | '; |
||
130 | |||
131 | } elseif ($category === 'Folder') { |
||
132 | $SQL = 'SELECT DISTINCT(`FC`.`fileid`) AS `id`, `FC`.`name`, LOWER(`FC`.`name`) AS `lower` |
||
133 | FROM `*PREFIX*audioplayer_tracks` `AT` |
||
134 | JOIN `*PREFIX*filecache` `FC` |
||
135 | ON `FC`.`fileid` = `AT`.`folder_id` |
||
136 | WHERE `AT`.`user_id` = ? |
||
137 | ORDER BY LOWER(`FC`.`name`) ASC |
||
138 | '; |
||
139 | } elseif ($category === 'Album') { |
||
140 | $SQL = 'SELECT `AB`.`id` , `AB`.`name`, LOWER(`AB`.`name`) AS `lower` |
||
141 | FROM `*PREFIX*audioplayer_albums` `AB` |
||
142 | LEFT JOIN `*PREFIX*audioplayer_artists` `AA` |
||
143 | ON `AB`.`artist_id` = `AA`.`id` |
||
144 | WHERE `AB`.`user_id` = ? |
||
145 | ORDER BY LOWER(`AB`.`name`) ASC |
||
146 | '; |
||
147 | } elseif ($category === 'Album Artist') { |
||
148 | $SQL = 'SELECT DISTINCT(`AB`.`artist_id`) AS `id`, `AA`.`name`, LOWER(`AA`.`name`) AS `lower` |
||
149 | FROM `*PREFIX*audioplayer_albums` `AB` |
||
150 | JOIN `*PREFIX*audioplayer_artists` `AA` |
||
151 | ON `AB`.`artist_id` = `AA`.`id` |
||
152 | WHERE `AB`.`user_id` = ? |
||
153 | ORDER BY LOWER(`AA`.`name`) ASC |
||
154 | '; |
||
155 | } |
||
156 | |||
157 | if (isset($SQL)) { |
||
158 | $stmt = $this->db->prepare($SQL); |
||
159 | $stmt->execute(array($this->userId)); |
||
160 | $results = $stmt->fetchAll(); |
||
161 | foreach ($results as $row) { |
||
162 | array_splice($row, 2, 1); |
||
163 | if ($row['name'] === '0' OR $row['name'] === '') $row['name'] = $this->l10n->t('Unknown'); |
||
164 | $row['cnt'] = $this->getTrackCount($category, $row['id']); |
||
165 | $aPlaylists[] = $row; |
||
166 | } |
||
167 | } |
||
168 | |||
169 | $result = empty($aPlaylists) ? [ |
||
170 | 'status' => 'nodata' |
||
171 | ] : [ |
||
172 | 'status' => 'success', |
||
173 | 'data' => $aPlaylists |
||
174 | ]; |
||
175 | return new JSONResponse($result); |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Get the covers for the "Album Covers" view |
||
180 | * |
||
181 | * @NoAdminRequired |
||
182 | * @param $category |
||
183 | * @param $categoryId |
||
184 | * @return JSONResponse |
||
185 | */ |
||
186 | public function getCategoryItemCovers($category, $categoryId) |
||
187 | { |
||
188 | $whereMatching = array('Artist' => '`AT`.`artist_id`', 'Genre' => '`AT`.`genre_id`', 'Album' => '`AB`.`id`', 'Album Artist' => '`AB`.`artist_id`', 'Year' => '`AT`.`year`', 'Folder' => '`AT`.`folder_id`'); |
||
189 | |||
190 | $aPlaylists = array(); |
||
191 | $SQL = 'SELECT `AB`.`id` , `AB`.`name`, LOWER(`AB`.`name`) AS `lower` , `AA`.`id` AS `art`, (CASE WHEN `AB`.`cover` IS NOT NULL THEN `AB`.`id` ELSE NULL END) AS `cid`'; |
||
192 | $SQL .= ' FROM `*PREFIX*audioplayer_tracks` `AT`'; |
||
193 | $SQL .= ' LEFT JOIN `*PREFIX*audioplayer_albums` `AB` ON `AB`.`id` = `AT`.`album_id`'; |
||
194 | $SQL .= ' LEFT JOIN `*PREFIX*audioplayer_artists` `AA` ON `AA`.`id` = `AB`.`artist_id`'; |
||
195 | $SQL .= ' WHERE `AT`.`user_id` = ? '; |
||
196 | if ($categoryId) $SQL .= 'AND ' . $whereMatching[$category] . '= ?'; |
||
197 | $SQL .= ' GROUP BY `AB`.`id`, `AA`.`id`, `AB`.`name` ORDER BY LOWER(`AB`.`name`) ASC'; |
||
198 | |||
199 | if (isset($SQL)) { |
||
200 | $stmt = $this->db->prepare($SQL); |
||
201 | if ($categoryId) { |
||
202 | $stmt->execute(array($this->userId, $categoryId)); |
||
203 | } else { |
||
204 | $stmt->execute(array($this->userId)); |
||
205 | } |
||
206 | $results = $stmt->fetchAll(); |
||
207 | foreach ($results as $row) { |
||
208 | $row['art'] = $this->DBController->loadArtistsToAlbum($row['id'], $row['art']); |
||
209 | array_splice($row, 2, 1); |
||
210 | if ($row['name'] === '0' OR $row['name'] === '') $row['name'] = $this->l10n->t('Unknown'); |
||
211 | $aPlaylists[] = $row; |
||
212 | } |
||
213 | } |
||
214 | $result = empty($aPlaylists) ? [ |
||
215 | 'status' => 'nodata' |
||
216 | ] : [ |
||
217 | 'status' => 'success', |
||
218 | 'data' => $aPlaylists |
||
219 | ]; |
||
220 | return new JSONResponse($result); |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * Get the number of tracks for a category item |
||
225 | * |
||
226 | * @param string $category |
||
227 | * @param integer $categoryId |
||
228 | * @return integer |
||
229 | */ |
||
230 | private function getTrackCount($category, $categoryId) |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * get the tracks for a selected category or album |
||
254 | * |
||
255 | * @NoAdminRequired |
||
256 | * @param string $category |
||
257 | * @param string $categoryId |
||
258 | * @return JSONResponse |
||
259 | * @throws InvalidPathException |
||
260 | * @throws NotFoundException |
||
261 | */ |
||
262 | public function getTracks($category, $categoryId) |
||
277 | } |
||
278 | |||
279 | /** |
||
280 | * Get the tracks for a selected category or album |
||
281 | * |
||
282 | * @param string $category |
||
283 | * @param string $categoryId |
||
284 | * @return array |
||
285 | * @throws InvalidPathException |
||
286 | */ |
||
287 | private function getTracksDetails($category, $categoryId) |
||
409 | } |
||
410 | |||
411 | /** |
||
412 | * Extract steam urls from playlist files |
||
413 | * |
||
414 | * @param integer $fileId |
||
415 | * @return array |
||
416 | * @throws InvalidPathException |
||
417 | */ |
||
418 | private function StreamParser($fileId) |
||
419 | { |
||
420 | $tracks = array(); |
||
421 | $x = 0; |
||
422 | $title = null; |
||
423 | $userView = $this->rootFolder->getUserFolder($this->userId); |
||
424 | //$this->logger->debug('removed/unshared file found => remove '.$row['fid'], array('app' => 'audioplayer')); |
||
425 | |||
426 | $streamfile = $userView->getById($fileId); |
||
427 | $file_type = $streamfile[0]->getMimetype(); |
||
428 | $file_content = $streamfile[0]->getContent(); |
||
429 | |||
430 | if ($file_type === 'audio/x-scpls') { |
||
431 | $stream_data = parse_ini_string($file_content, true, INI_SCANNER_RAW); |
||
432 | $stream_rows = isset($stream_data['playlist']['NumberOfEntries']) ? $stream_data['playlist']['NumberOfEntries'] : $stream_data['playlist']['numberofentries']; |
||
433 | for ($i = 1; $i <= $stream_rows; $i++) { |
||
434 | $title = $stream_data['playlist']['Title' . $i]; |
||
435 | $file = $stream_data['playlist']['File' . $i]; |
||
436 | preg_match_all('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $file, $matches); |
||
437 | |||
438 | if ($matches[0]) { |
||
439 | $row = array(); |
||
440 | $row['id'] = $fileId . $i; |
||
441 | $row['cl1'] = $matches[0][0]; |
||
442 | $row['cl2'] = ''; |
||
443 | $row['cl3'] = ''; |
||
444 | $row['len'] = ''; |
||
445 | $row['mim'] = $file_type; |
||
446 | $row['cid'] = ''; |
||
447 | $row['lin'] = $matches[0][0]; |
||
448 | if ($title) $row['cl1'] = $title; |
||
449 | $tracks[] = $row; |
||
450 | } |
||
451 | } |
||
452 | } else { |
||
453 | // get the path of the playlist file as reference |
||
454 | $playlistFilePath = explode('/', $streamfile[0]->getInternalPath()); |
||
455 | // remove leading "files/" |
||
456 | array_shift($playlistFilePath); |
||
457 | // remove the filename itself |
||
458 | array_pop($playlistFilePath); |
||
459 | |||
460 | foreach (preg_split("/((\r?\n)|(\r\n?))/", $file_content) as $line) { |
||
461 | $title = null; |
||
462 | $artist = null; |
||
463 | if (empty($line) || $line === '#EXTM3U') continue; |
||
464 | if (substr($line, 0, 8) === '#EXTINF:') { |
||
465 | $extinf = explode(',', substr($line, 8)); |
||
466 | $extNoDuration = $extinf[1]; |
||
467 | $extinf = explode(' - ', $extNoDuration); |
||
468 | $title = $extinf[1]; |
||
469 | $artist = $extinf[0]; |
||
470 | $line = $extinf[2]; |
||
471 | } |
||
472 | preg_match_all('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $line, $matches); |
||
473 | |||
474 | if ($matches[0]) { |
||
475 | $x++; |
||
476 | $row = array(); |
||
477 | $row['id'] = $fileId . $x; |
||
478 | $row['cl1'] = $matches[0][0]; |
||
479 | $row['cl2'] = ''; |
||
480 | $row['cl3'] = ''; |
||
481 | $row['len'] = ''; |
||
482 | $row['mim'] = $file_type; |
||
483 | $row['cid'] = ''; |
||
484 | $row['lin'] = $matches[0][0]; |
||
485 | if ($title) $row['cl1'] = $title; |
||
486 | if ($artist) $row['cl2'] = $artist; |
||
487 | $tracks[] = $row; |
||
488 | } elseif (preg_match('/^[^"<>|:]*$/',$line)) { |
||
489 | |||
490 | if ($line[0] === '/') { |
||
491 | // Absolut path |
||
492 | $path = $line; |
||
493 | } elseif (substr($line, 0, 3) === '../') { |
||
494 | // relative one level up => remove the parent folder of the playlist file |
||
495 | array_shift($playlistFilePath); |
||
496 | $path = $playlistFilePath; |
||
497 | array_push($path, substr($line, 3)); |
||
498 | $path = implode('/', $path); |
||
499 | } else { |
||
500 | // normal relative path |
||
501 | $path = $playlistFilePath; |
||
502 | array_push($path, $line); |
||
503 | $path = implode('/', $path); |
||
504 | } |
||
505 | $x++; |
||
506 | $this->logger->debug('Final path of playlist track: '.$path); |
||
507 | |||
508 | try { |
||
509 | $fileId = $this->rootFolder->getUserFolder($this->userId)->get($path)->getId(); |
||
510 | $track = $this->DBController->getTrackInfo(null,$fileId); |
||
511 | if (!isset($track['id'])) continue; |
||
512 | |||
513 | $row = array(); |
||
514 | $row['id'] = $track['id']; |
||
515 | $row['cl1'] = $track['Title']; |
||
516 | $row['cl2'] = $track['Artist']; |
||
517 | $row['cl3'] = $track['Album']; |
||
518 | $row['len'] = $track['Length']; |
||
519 | $row['mim'] = $track['MIME type']; |
||
520 | $row['cid'] = ''; |
||
521 | $row['lin'] = $track['id']; |
||
522 | $row['fav'] = $track['fav']; |
||
523 | if ($title) $row['cl1'] = $title; |
||
524 | if ($artist) $row['cl2'] = $artist; |
||
525 | $tracks[] = $row; |
||
526 | } catch (NotFoundException $e) { |
||
527 | $this->logger->debug('Path is not a valid file: '.$path); |
||
528 | // File is not known in the filecache and will be ignored; |
||
529 | } |
||
530 | } |
||
531 | } |
||
532 | } |
||
533 | return $tracks; |
||
534 | } |
||
535 | |||
536 | /** |
||
537 | * Get selection dependend headers for the list view |
||
538 | * |
||
539 | * @param string $category |
||
540 | * @return array |
||
541 | */ |
||
542 | private function getListViewHeaders($category) |
||
552 | } |
||
553 | } |
||
554 | } |
||
555 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths