| Total Complexity | 57 |
| Total Lines | 441 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| 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); |
||
| 30 | class PlaylistFileService { |
||
| 31 | private $playlistBusinessLayer; |
||
| 32 | private $radioStationBusinessLayer; |
||
| 33 | private $trackBusinessLayer; |
||
| 34 | private $logger; |
||
| 35 | |||
| 36 | private const PARSE_LOCAL_FILES_ONLY = 1; |
||
| 37 | private const PARSE_URLS_ONLY = 2; |
||
| 38 | private const PARSE_LOCAL_FILES_AND_URLS = 3; |
||
| 39 | |||
| 40 | public function __construct( |
||
| 41 | PlaylistBusinessLayer $playlistBusinessLayer, |
||
| 42 | RadioStationBusinessLayer $radioStationBusinessLayer, |
||
| 43 | TrackBusinessLayer $trackBusinessLayer, |
||
| 44 | Logger $logger) { |
||
| 45 | $this->playlistBusinessLayer = $playlistBusinessLayer; |
||
| 46 | $this->radioStationBusinessLayer = $radioStationBusinessLayer; |
||
| 47 | $this->trackBusinessLayer = $trackBusinessLayer; |
||
| 48 | $this->logger = $logger; |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * export the playlist to a file |
||
| 53 | * @param int $id playlist ID |
||
| 54 | * @param string $userId owner of the playlist |
||
| 55 | * @param Folder $userFolder home dir of the user |
||
| 56 | * @param string $folderPath target parent folder path |
||
| 57 | * @param string $collisionMode action to take on file name collision, |
||
| 58 | * supported values: |
||
| 59 | * - 'overwrite' The existing file will be overwritten |
||
| 60 | * - 'keepboth' The new file is named with a suffix to make it unique |
||
| 61 | * - 'abort' (default) The operation will fail |
||
| 62 | * @return string path of the written file |
||
| 63 | * @throws BusinessLayerException if playlist with ID not found |
||
| 64 | * @throws \OCP\Files\NotFoundException if the $folderPath is not a valid folder |
||
| 65 | * @throws \RuntimeException on name conflict if $collisionMode == 'abort' |
||
| 66 | * @throws \OCP\Files\NotPermittedException if the user is not allowed to write to the given folder |
||
| 67 | */ |
||
| 68 | public function exportToFile( |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * export all the radio stations of a user to a file |
||
| 101 | * @param string $userId user |
||
| 102 | * @param Folder $userFolder home dir of the user |
||
| 103 | * @param string $folderPath target parent folder path |
||
| 104 | * @param string $filename target file name |
||
| 105 | * @param string $collisionMode action to take on file name collision, |
||
| 106 | * supported values: |
||
| 107 | * - 'overwrite' The existing file will be overwritten |
||
| 108 | * - 'keepboth' The new file is named with a suffix to make it unique |
||
| 109 | * - 'abort' (default) The operation will fail |
||
| 110 | * @return string path of the written file |
||
| 111 | * @throws \OCP\Files\NotFoundException if the $folderPath is not a valid folder |
||
| 112 | * @throws \RuntimeException on name conflict if $collisionMode == 'abort' |
||
| 113 | * @throws \OCP\Files\NotPermittedException if the user is not allowed to write to the given folder |
||
| 114 | */ |
||
| 115 | public function exportRadioStationsToFile( |
||
| 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' (dafault) 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 { |
||
| 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 |
||
| 223 | */ |
||
| 224 | public function parseFile(int $fileId, Folder $baseFolder) : array { |
||
| 225 | $node = $baseFolder->getById($fileId)[0] ?? null; |
||
| 226 | if ($node instanceof File) { |
||
| 227 | return self::doParseFile($node, $baseFolder, self::PARSE_LOCAL_FILES_AND_URLS); |
||
| 228 | } else { |
||
| 229 | throw new \OCP\Files\NotFoundException(); |
||
| 230 | } |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @param File $file The playlist file to parse |
||
| 235 | * @param Folder $baseFolder Base folder for the local files |
||
| 236 | * @param int $mode One of self::[PARSE_LOCAL_FILES_ONLY, PARSE_URLS_ONLY, PARSE_LOCAL_FILES_AND_URLS] |
||
| 237 | * @throws \UnexpectedValueException |
||
| 238 | * @return array |
||
| 239 | */ |
||
| 240 | private static function doParseFile(File $file, Folder $baseFolder, int $mode) : array { |
||
| 289 | ]; |
||
| 290 | } |
||
| 291 | |||
| 292 | private static function parseM3uFile(File $file) : array { |
||
| 302 | } |
||
| 303 | |||
| 304 | private static function parseM3uFilePointer($fp, string $encoding) : array { |
||
| 305 | $entries = []; |
||
| 306 | |||
| 307 | $caption = null; |
||
| 308 | |||
| 309 | while ($line = \fgets($fp)) { |
||
| 310 | $line = \mb_convert_encoding($line, /** @scrutinizer ignore-type */ \mb_internal_encoding(), $encoding); |
||
| 311 | $line = \trim(/** @scrutinizer ignore-type */ $line); |
||
| 312 | |||
| 313 | if ($line === '') { |
||
| 314 | // empty line => skip |
||
| 315 | } elseif (Util::startsWith($line, '#')) { |
||
| 316 | // comment or extended format attribute line |
||
| 317 | if ($value = self::extractExtM3uField($line, 'EXTENC')) { |
||
| 318 | // update the used encoding with the explicitly defined one |
||
| 319 | $encoding = $value; |
||
| 320 | } elseif ($value = self::extractExtM3uField($line, 'EXTINF')) { |
||
| 321 | // The format should be "length,caption". Set caption to null if the field is badly formatted. |
||
| 322 | $parts = \explode(',', $value, 2); |
||
| 323 | $caption = $parts[1] ?? null; |
||
| 324 | if (\is_string($caption)) { |
||
| 325 | $caption = \trim($caption); |
||
| 326 | } |
||
| 327 | } |
||
| 328 | } else { |
||
| 329 | $entries[] = [ |
||
| 330 | 'path' => $line, |
||
| 331 | 'caption' => $caption |
||
| 332 | ]; |
||
| 333 | $caption = null; // the caption has been used up |
||
| 334 | } |
||
| 335 | } |
||
| 336 | |||
| 337 | return $entries; |
||
| 338 | } |
||
| 339 | |||
| 340 | public static function parseM3uContent(string $content, string $encoding) { |
||
| 352 | } |
||
| 353 | |||
| 354 | private static function parsePlsFile(File $file) : array { |
||
| 355 | return self::parsePlsContent($file->getContent()); |
||
| 356 | } |
||
| 357 | |||
| 358 | public static function parsePlsContent(string $content) : array { |
||
| 359 | $files = []; |
||
| 360 | $titles = []; |
||
| 361 | |||
| 362 | // If the file doesn't seem to be UTF-8, then assume it to be ISO-8859-1 |
||
| 363 | if (!\mb_check_encoding($content, 'UTF-8')) { |
||
| 364 | $content = \mb_convert_encoding($content, 'UTF-8', 'ISO-8859-1'); |
||
| 365 | } |
||
| 366 | |||
| 367 | $fp = \fopen("php://temp", 'r+'); |
||
| 368 | \assert($fp !== false, 'Unexpected error: opening temporary stream failed'); |
||
| 369 | |||
| 370 | \fputs($fp, /** @scrutinizer ignore-type */ $content); |
||
| 371 | \rewind($fp); |
||
| 372 | |||
| 373 | // the first line should always be [playlist] |
||
| 374 | if (\trim(\fgets($fp)) != '[playlist]') { |
||
| 375 | throw new \UnexpectedValueException('the file is not in valid PLS format'); |
||
| 376 | } |
||
| 377 | |||
| 378 | // the rest of the non-empty lines should be in format "key=value" |
||
| 379 | while ($line = \fgets($fp)) { |
||
| 380 | // ignore empty and malformed lines |
||
| 381 | if (\strpos($line, '=') !== false) { |
||
| 382 | list($key, $value) = \explode('=', $line, 2); |
||
| 383 | $key = \trim($key); |
||
| 384 | $value = \trim($value); |
||
| 385 | // we are interested only on the File# and Title# lines |
||
| 386 | if (Util::startsWith($key, 'File')) { |
||
| 387 | $idx = \substr($key, \strlen('File')); |
||
| 388 | $files[$idx] = $value; |
||
| 389 | } elseif (Util::startsWith($key, 'Title')) { |
||
| 390 | $idx = \substr($key, \strlen('Title')); |
||
| 391 | $titles[$idx] = $value; |
||
| 392 | } |
||
| 393 | } |
||
| 394 | } |
||
| 395 | \fclose($fp); |
||
| 396 | |||
| 397 | $entries = []; |
||
| 398 | foreach ($files as $idx => $filePath) { |
||
| 399 | $entries[] = [ |
||
| 400 | 'path' => $filePath, |
||
| 401 | 'caption' => $titles[$idx] ?? null |
||
| 402 | ]; |
||
| 403 | } |
||
| 404 | |||
| 405 | return $entries; |
||
| 406 | } |
||
| 407 | |||
| 408 | private static function checkFileNameConflict(Folder $targetFolder, string $filename, string $collisionMode) : string { |
||
| 409 | if ($targetFolder->nodeExists($filename)) { |
||
| 410 | switch ($collisionMode) { |
||
| 411 | case 'overwrite': |
||
| 412 | $targetFolder->get($filename)->delete(); |
||
| 413 | break; |
||
| 414 | case 'keepboth': |
||
| 415 | $filename = $targetFolder->getNonExistingName($filename); |
||
| 416 | break; |
||
| 417 | default: |
||
| 418 | throw new \RuntimeException('file already exists'); |
||
| 419 | } |
||
| 420 | } |
||
| 421 | return $filename; |
||
| 422 | } |
||
| 423 | |||
| 424 | private static function captionForTrack(Track $track) : string { |
||
| 425 | $title = $track->getTitle(); |
||
| 426 | $artist = $track->getArtistName(); |
||
| 427 | |||
| 428 | return empty($artist) ? $title : "$artist - $title"; |
||
| 429 | } |
||
| 430 | |||
| 431 | private static function extractExtM3uField($line, $field) : ?string { |
||
| 436 | } |
||
| 437 | } |
||
| 438 | |||
| 439 | private static function findFile(Folder $baseFolder, string $cwd, string $path) : ?File { |
||
| 440 | $absPath = Util::resolveRelativePath($cwd, $path); |
||
| 441 | |||
| 442 | try { |
||
| 443 | $file = $baseFolder->get($absPath); |
||
| 444 | if ($file instanceof File) { |
||
| 445 | return $file; |
||
| 446 | } else { |
||
| 447 | return null; |
||
| 448 | } |
||
| 449 | } catch (\OCP\Files\NotFoundException | \OCP\Files\NotPermittedException $ex) { |
||
| 450 | /* In case the file is not found and the path contains any backslashes, consider the possibility |
||
| 451 | * that the path follows the Windows convention of using backslashes as path separators. |
||
| 452 | */ |
||
| 453 | if (\strpos($path, '\\') !== false) { |
||
| 454 | $path = \str_replace('\\', '/', $path); |
||
| 455 | return self::findFile($baseFolder, $cwd, $path); |
||
| 456 | } else { |
||
| 457 | return null; |
||
| 458 | } |
||
| 459 | } |
||
| 460 | } |
||
| 461 | |||
| 462 | /** |
||
| 463 | * @throws \OCP\Files\NotFoundException if the $path does not point to a file under the $baseFolder |
||
| 464 | */ |
||
| 465 | private static function getFile(Folder $baseFolder, string $path) : File { |
||
| 471 | } |
||
| 472 | |||
| 473 | } |
||
| 474 |