| Total Complexity | 60 |
| Total Lines | 463 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 0 |
Complex classes like PlaylistFileService 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 PlaylistFileService, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 31 | class PlaylistFileService { |
||
| 32 | private PlaylistBusinessLayer $playlistBusinessLayer; |
||
| 33 | private RadioStationBusinessLayer $radioStationBusinessLayer; |
||
| 34 | private TrackBusinessLayer $trackBusinessLayer; |
||
| 35 | private StreamTokenService $tokenService; |
||
| 36 | private Logger $logger; |
||
| 37 | |||
| 38 | private const PARSE_LOCAL_FILES_ONLY = 1; |
||
| 39 | private const PARSE_URLS_ONLY = 2; |
||
| 40 | private const PARSE_LOCAL_FILES_AND_URLS = 3; |
||
| 41 | |||
| 42 | public function __construct( |
||
| 43 | PlaylistBusinessLayer $playlistBusinessLayer, |
||
| 44 | RadioStationBusinessLayer $radioStationBusinessLayer, |
||
| 45 | TrackBusinessLayer $trackBusinessLayer, |
||
| 46 | StreamTokenService $tokenService, |
||
| 47 | Logger $logger) { |
||
| 48 | $this->playlistBusinessLayer = $playlistBusinessLayer; |
||
| 49 | $this->radioStationBusinessLayer = $radioStationBusinessLayer; |
||
| 50 | $this->trackBusinessLayer = $trackBusinessLayer; |
||
| 51 | $this->tokenService = $tokenService; |
||
| 52 | $this->logger = $logger; |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * export the playlist to a file |
||
| 57 | * @param int $id playlist ID |
||
| 58 | * @param string $userId owner of the playlist |
||
| 59 | * @param Folder $userFolder home dir of the user |
||
| 60 | * @param string $folderPath target parent folder path |
||
| 61 | * @param ?string $filename target file name, omit to use the list name |
||
| 62 | * @param string $collisionMode action to take on file name collision, |
||
| 63 | * supported values: |
||
| 64 | * - 'overwrite' The existing file will be overwritten |
||
| 65 | * - 'keepboth' The new file is named with a suffix to make it unique |
||
| 66 | * - 'abort' (default) The operation will fail |
||
| 67 | * @return string path of the written file |
||
| 68 | * @throws BusinessLayerException if playlist with ID not found |
||
| 69 | * @throws \OCP\Files\NotFoundException if the $folderPath is not a valid folder |
||
| 70 | * @throws FileExistsException on name conflict if $collisionMode == 'abort' |
||
| 71 | * @throws \OCP\Files\NotPermittedException if the user is not allowed to write to the given folder |
||
| 72 | */ |
||
| 73 | public function exportToFile( |
||
| 74 | int $id, string $userId, Folder $userFolder, string $folderPath, ?string $filename=null, string $collisionMode='abort') : string { |
||
| 75 | $playlist = $this->playlistBusinessLayer->find($id, $userId); |
||
| 76 | $tracks = $this->playlistBusinessLayer->getPlaylistTracks($id, $userId); |
||
| 77 | $targetFolder = FilesUtil::getFolderFromRelativePath($userFolder, $folderPath); |
||
| 78 | |||
| 79 | $filename = $filename ?: $playlist->getName(); |
||
| 80 | $filename = FilesUtil::sanitizeFileName($filename, ['m3u8', 'm3u']); |
||
| 81 | |||
| 82 | $file = FilesUtil::createFile($targetFolder, $filename, $collisionMode); |
||
| 83 | |||
| 84 | $content = "#EXTM3U\n#EXTENC: UTF-8\n"; |
||
| 85 | foreach ($tracks as $track) { |
||
| 86 | $nodes = $userFolder->getById($track->getFileId()); |
||
| 87 | if (\count($nodes) > 0) { |
||
| 88 | $caption = self::captionForTrack($track); |
||
| 89 | $content .= "#EXTINF:{$track->getLength()},$caption\n"; |
||
| 90 | $content .= FilesUtil::relativePath($targetFolder->getPath(), $nodes[0]->getPath()) . "\n"; |
||
| 91 | } |
||
| 92 | } |
||
| 93 | $file->putContent($content); |
||
| 94 | |||
| 95 | return $userFolder->getRelativePath($file->getPath()); |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * export all the radio stations of a user to a file |
||
| 100 | * @param string $userId user |
||
| 101 | * @param Folder $userFolder home dir of the user |
||
| 102 | * @param string $folderPath target parent folder path |
||
| 103 | * @param string $filename target file name |
||
| 104 | * @param string $collisionMode action to take on file name collision, |
||
| 105 | * supported values: |
||
| 106 | * - 'overwrite' The existing file will be overwritten |
||
| 107 | * - 'keepboth' The new file is named with a suffix to make it unique |
||
| 108 | * - 'abort' (default) The operation will fail |
||
| 109 | * @return string path of the written file |
||
| 110 | * @throws \OCP\Files\NotFoundException if the $folderPath is not a valid folder |
||
| 111 | * @throws FileExistsException on name conflict if $collisionMode == 'abort' |
||
| 112 | * @throws \OCP\Files\NotPermittedException if the user is not allowed to write to the given folder |
||
| 113 | */ |
||
| 114 | public function exportRadioStationsToFile( |
||
| 115 | string $userId, Folder $userFolder, string $folderPath, string $filename, string $collisionMode='abort') : string { |
||
| 116 | $targetFolder = FilesUtil::getFolderFromRelativePath($userFolder, $folderPath); |
||
| 117 | |||
| 118 | $filename = FilesUtil::sanitizeFileName($filename, ['m3u8', 'm3u']); |
||
| 119 | |||
| 120 | $file = FilesUtil::createFile($targetFolder, $filename, $collisionMode); |
||
| 121 | |||
| 122 | $stations = $this->radioStationBusinessLayer->findAll($userId, SortBy::Name); |
||
| 123 | |||
| 124 | $content = "#EXTM3U\n#EXTENC: UTF-8\n"; |
||
| 125 | foreach ($stations as $station) { |
||
| 126 | $content .= "#EXTINF:1,{$station->getName()}\n"; |
||
| 127 | $content .= $station->getStreamUrl() . "\n"; |
||
| 128 | } |
||
| 129 | $file->putContent($content); |
||
| 130 | |||
| 131 | return $userFolder->getRelativePath($file->getPath()); |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * import playlist contents from a file |
||
| 136 | * @param int $id playlist ID |
||
| 137 | * @param string $userId owner of the playlist |
||
| 138 | * @param Folder $userFolder user home dir |
||
| 139 | * @param string $filePath path of the file to import |
||
| 140 | * @parma string $mode one of the following: |
||
| 141 | * - 'append' (default) Append the imported tracks after the existing tracks on the list |
||
| 142 | * - 'overwrite' Replace any previous tracks on the list with the imported tracks |
||
| 143 | * @return array with three keys: |
||
| 144 | * - 'playlist': The Playlist entity after the modification |
||
| 145 | * - 'imported_count': An integer showing the number of tracks imported |
||
| 146 | * - 'failed_count': An integer showing the number of tracks in the file which could not be imported |
||
| 147 | * @throws BusinessLayerException if playlist with ID not found |
||
| 148 | * @throws \OCP\Files\NotFoundException if the $filePath is not a valid file |
||
| 149 | * @throws \UnexpectedValueException if the $filePath points to a file of unsupported type |
||
| 150 | */ |
||
| 151 | public function importFromFile(int $id, string $userId, Folder $userFolder, string $filePath, string $mode='append') : array { |
||
| 152 | $parsed = self::doParseFile(self::getFile($userFolder, $filePath), $userFolder, self::PARSE_LOCAL_FILES_ONLY); |
||
| 153 | $trackFilesAndCaptions = $parsed['files']; |
||
| 154 | $invalidPaths = $parsed['invalid_paths']; |
||
| 155 | |||
| 156 | $trackIds = []; |
||
| 157 | foreach ($trackFilesAndCaptions as $trackFileAndCaption) { |
||
| 158 | $trackFile = $trackFileAndCaption['file']; |
||
| 159 | if ($track = $this->trackBusinessLayer->findByFileId($trackFile->getId(), $userId)) { |
||
| 160 | $trackIds[] = $track->getId(); |
||
| 161 | } else { |
||
| 162 | $invalidPaths[] = $trackFile->getPath(); |
||
| 163 | } |
||
| 164 | } |
||
| 165 | |||
| 166 | if ($mode === 'overwrite') { |
||
| 167 | $this->playlistBusinessLayer->removeAllTracks($id, $userId); |
||
| 168 | } |
||
| 169 | $playlist = $this->playlistBusinessLayer->addTracks($trackIds, $id, $userId); |
||
| 170 | |||
| 171 | if (\count($invalidPaths) > 0) { |
||
| 172 | $this->logger->log('Some files were not found from the user\'s music library: ' |
||
| 173 | . \json_encode($invalidPaths, JSON_PARTIAL_OUTPUT_ON_ERROR), 'warn'); |
||
| 174 | } |
||
| 175 | |||
| 176 | return [ |
||
| 177 | 'playlist' => $playlist, |
||
| 178 | 'imported_count' => \count($trackIds), |
||
| 179 | 'failed_count' => \count($invalidPaths) |
||
| 180 | ]; |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * import stream URLs from a playlist file and store them as internet radio stations |
||
| 185 | * @param string $userId user |
||
| 186 | * @param Folder $userFolder user home dir |
||
| 187 | * @param string $filePath path of the file to import |
||
| 188 | * @return array with two keys: |
||
| 189 | * - 'stations': Array of RadioStation objects imported from the file |
||
| 190 | * - 'failed_count': An integer showing the number of entries in the file which were not valid URLs |
||
| 191 | * @throws \OCP\Files\NotFoundException if the $filePath is not a valid file |
||
| 192 | * @throws \UnexpectedValueException if the $filePath points to a file of unsupported type |
||
| 193 | */ |
||
| 194 | public function importRadioStationsFromFile(string $userId, Folder $userFolder, string $filePath) : array { |
||
| 213 | ]; |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Parse a playlist file and return the contained files |
||
| 218 | * @param int $fileId playlist file ID |
||
| 219 | * @param Folder $baseFolder ancestor folder of the playlist and the track files (e.g. user folder) |
||
| 220 | * @throws \OCP\Files\NotFoundException if the $fileId is not a valid file under the $baseFolder |
||
| 221 | * @throws \UnexpectedValueException if the $filePath points to a file of unsupported type |
||
| 222 | * @return array ['files' => array, 'invalid_paths' => string[]] |
||
| 223 | */ |
||
| 224 | public function parseFile(int $fileId, Folder $baseFolder) : array { |
||
| 225 | $node = $baseFolder->getById($fileId)[0] ?? null; |
||
| 226 | if ($node instanceof File) { |
||
| 227 | $parsed = self::doParseFile($node, $baseFolder, self::PARSE_LOCAL_FILES_AND_URLS); |
||
| 228 | // add security tokens for the external URL entries |
||
| 229 | foreach ($parsed['files'] as &$entry) { |
||
| 230 | if (isset($entry['url'])) { |
||
| 231 | $entry['token'] = $this->tokenService->tokenForUrl($entry['url']); |
||
| 232 | } |
||
| 233 | } |
||
| 234 | return $parsed; |
||
| 235 | } else { |
||
| 236 | throw new \OCP\Files\NotFoundException(); |
||
| 237 | } |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @param File $file The playlist file to parse |
||
| 242 | * @param Folder $baseFolder Base folder for the local files |
||
| 243 | * @param int $mode One of self::[PARSE_LOCAL_FILES_ONLY, PARSE_URLS_ONLY, PARSE_LOCAL_FILES_AND_URLS] |
||
| 244 | * @throws \UnexpectedValueException |
||
| 245 | * @return array ['files' => array, 'invalid_paths' => string[]] |
||
| 246 | */ |
||
| 247 | private static function doParseFile(File $file, Folder $baseFolder, int $mode) : array { |
||
| 248 | $mime = $file->getMimeType(); |
||
| 249 | |||
| 250 | if ($mime == 'audio/mpegurl') { |
||
| 251 | $entries = self::parseM3uFile($file); |
||
| 252 | } elseif ($mime == 'audio/x-scpls') { |
||
| 253 | $entries = self::parsePlsFile($file); |
||
| 254 | } elseif ($mime == 'application/vnd.ms-wpl') { |
||
| 255 | $entries = self::parseWplFile($file); |
||
| 256 | } else { |
||
| 257 | throw new \UnexpectedValueException("file mime type '$mime' is not supported"); |
||
| 258 | } |
||
| 259 | |||
| 260 | // find the parsed entries from the file system |
||
| 261 | $trackFiles = []; |
||
| 262 | $invalidPaths = []; |
||
| 263 | $cwd = $baseFolder->getRelativePath($file->getParent()->getPath()); |
||
| 264 | |||
| 265 | foreach ($entries as $entry) { |
||
| 266 | $path = $entry['path']; |
||
| 267 | |||
| 268 | if (Util::startsWith($path, 'http', /*ignoreCase=*/true)) { |
||
| 269 | if ($mode !== self::PARSE_LOCAL_FILES_ONLY) { |
||
| 270 | $trackFiles[] = [ |
||
| 271 | 'url' => $path, |
||
| 272 | 'caption' => $entry['caption'] |
||
| 273 | ]; |
||
| 274 | } else { |
||
| 275 | $invalidPaths[] = $path; |
||
| 276 | } |
||
| 277 | } else { |
||
| 278 | if ($mode !== self::PARSE_URLS_ONLY) { |
||
| 279 | $entryFile = self::findFile($baseFolder, $cwd, $path); |
||
| 280 | |||
| 281 | if ($entryFile !== null) { |
||
| 282 | $trackFiles[] = [ |
||
| 283 | 'file' => $entryFile, |
||
| 284 | 'caption' => $entry['caption'] |
||
| 285 | ]; |
||
| 286 | } else { |
||
| 287 | $invalidPaths[] = $path; |
||
| 288 | } |
||
| 289 | } else { |
||
| 290 | $invalidPaths[] = $path; |
||
| 291 | } |
||
| 292 | } |
||
| 293 | } |
||
| 294 | |||
| 295 | return [ |
||
| 296 | 'files' => $trackFiles, |
||
| 297 | 'invalid_paths' => $invalidPaths |
||
| 298 | ]; |
||
| 299 | } |
||
| 300 | |||
| 301 | private static function parseM3uFile(File $file) : array { |
||
| 316 | } |
||
| 317 | |||
| 318 | public static function parseM3uContent(string $content) { |
||
| 332 | } |
||
| 333 | |||
| 334 | private static function parseM3uFilePointer($fp, string $encoding) : array { |
||
| 368 | } |
||
| 369 | |||
| 370 | private static function parsePlsFile(File $file) : array { |
||
| 371 | return self::parsePlsContent($file->getContent()); |
||
| 372 | } |
||
| 373 | |||
| 374 | public static function parsePlsContent(string $content) : array { |
||
| 375 | $files = []; |
||
| 376 | $titles = []; |
||
| 377 | |||
| 378 | // If the file doesn't seem to be UTF-8, then assume it to be ISO-8859-1 |
||
| 379 | if (!\mb_check_encoding($content, 'UTF-8')) { |
||
| 380 | $content = \mb_convert_encoding($content, 'UTF-8', 'ISO-8859-1'); |
||
| 381 | } |
||
| 382 | |||
| 383 | $fp = \fopen("php://temp", 'r+'); |
||
| 384 | \assert($fp !== false, 'Unexpected error: opening temporary stream failed'); |
||
| 385 | |||
| 386 | \fputs($fp, /** @scrutinizer ignore-type */ $content); |
||
| 387 | \rewind($fp); |
||
| 388 | |||
| 389 | // the first line should always be [playlist] |
||
| 390 | if (\trim(\fgets($fp)) != '[playlist]') { |
||
| 391 | throw new \UnexpectedValueException('the file is not in valid PLS format'); |
||
| 392 | } |
||
| 393 | |||
| 394 | // the rest of the non-empty lines should be in format "key=value" |
||
| 395 | while ($line = \fgets($fp)) { |
||
| 396 | // ignore empty and malformed lines |
||
| 397 | if (\strpos($line, '=') !== false) { |
||
| 398 | list($key, $value) = \explode('=', $line, 2); |
||
| 399 | $key = \trim($key); |
||
| 400 | $value = \trim($value); |
||
| 401 | // we are interested only on the File# and Title# lines |
||
| 402 | if (Util::startsWith($key, 'File')) { |
||
| 403 | $idx = \substr($key, \strlen('File')); |
||
| 404 | $files[$idx] = $value; |
||
| 405 | } elseif (Util::startsWith($key, 'Title')) { |
||
| 406 | $idx = \substr($key, \strlen('Title')); |
||
| 407 | $titles[$idx] = $value; |
||
| 408 | } |
||
| 409 | } |
||
| 410 | } |
||
| 411 | \fclose($fp); |
||
| 412 | |||
| 413 | $entries = []; |
||
| 414 | foreach ($files as $idx => $filePath) { |
||
| 415 | $entries[] = [ |
||
| 416 | 'path' => $filePath, |
||
| 417 | 'caption' => $titles[$idx] ?? null |
||
| 418 | ]; |
||
| 419 | } |
||
| 420 | |||
| 421 | return $entries; |
||
| 422 | } |
||
| 423 | |||
| 424 | public static function parseWplFile(File $file) : array { |
||
| 444 | } |
||
| 445 | |||
| 446 | private static function captionForTrack(Track $track) : string { |
||
| 447 | $title = $track->getTitle(); |
||
| 448 | $artist = $track->getArtistName(); |
||
| 449 | |||
| 450 | return empty($artist) ? $title : "$artist - $title"; |
||
| 451 | } |
||
| 452 | |||
| 453 | private static function extractExtM3uField($line, $field) : ?string { |
||
| 458 | } |
||
| 459 | } |
||
| 460 | |||
| 461 | private static function findFile(Folder $baseFolder, string $cwd, string $path) : ?File { |
||
| 462 | $absPath = FilesUtil::resolveRelativePath($cwd, $path); |
||
| 463 | |||
| 464 | try { |
||
| 465 | /** @throws \OCP\Files\NotFoundException | \OCP\Files\NotPermittedException */ |
||
| 466 | $file = $baseFolder->get($absPath); |
||
| 467 | if ($file instanceof File) { |
||
| 468 | return $file; |
||
| 469 | } else { |
||
| 470 | return null; |
||
| 471 | } |
||
| 472 | } catch (\OCP\Files\NotFoundException | \OCP\Files\NotPermittedException $ex) { |
||
| 473 | /* In case the file is not found and the path contains any backslashes, consider the possibility |
||
| 474 | * that the path follows the Windows convention of using backslashes as path separators. |
||
| 475 | */ |
||
| 476 | if (\strpos($path, '\\') !== false) { |
||
| 477 | $path = \str_replace('\\', '/', $path); |
||
| 478 | return self::findFile($baseFolder, $cwd, $path); |
||
| 479 | } else { |
||
| 480 | return null; |
||
| 481 | } |
||
| 482 | } |
||
| 483 | } |
||
| 484 | |||
| 485 | /** |
||
| 486 | * @throws \OCP\Files\NotFoundException if the $path does not point to a file under the $baseFolder |
||
| 487 | */ |
||
| 488 | private static function getFile(Folder $baseFolder, string $path) : File { |
||
| 494 | } |
||
| 495 | |||
| 496 | } |
||
| 497 |