| 1 | <?php declare(strict_types=1); |
||
| 15 | final class YouTube implements Platform |
||
| 16 | { |
||
| 17 | use FilesystemManager; |
||
| 18 | |||
| 19 | // See https://stackoverflow.com/a/37704433/389519 |
||
| 20 | private const YOUTUBE_URL_REGEX = <<<REGEX |
||
| 21 | /\b((?:https?:)?\/\/)?((?:www|m)\.)?((?:youtube\.com|youtu.be))(\/(?:[\w\-]+\?v=|embed\/|v\/)?)([\w\-]+)(\S+)?/i |
||
| 22 | REGEX; |
||
| 23 | private const YOUTUBE_URL_REGEX_MATCHES_ID_INDEX = 5; |
||
| 24 | private const YOUTUBE_URL_PREFIX = 'https://www.youtube.com/watch?v='; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @param UserInterface $ui |
||
| 28 | * @param array $options |
||
| 29 | * @param bool $dryRun |
||
| 30 | */ |
||
| 31 | public function __construct(UserInterface $ui, array $options, bool $dryRun = false) |
||
| 32 | { |
||
| 33 | $this->ui = $ui; |
||
| 34 | $this->options = $options; |
||
| 35 | $this->dryRun = $dryRun; |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @param \App\Domain\Content[]|\App\Domain\Collection $contents |
||
| 40 | * @param \App\Domain\PathPart $rootPathPart |
||
| 41 | * @throws \RuntimeException |
||
| 42 | */ |
||
| 43 | public function synchronizeContents(Collection $contents, PathPart $rootPathPart): void |
||
| 44 | { |
||
| 45 | if ($contents->isEmpty()) { |
||
| 46 | return; |
||
| 47 | } |
||
| 48 | |||
| 49 | $platformPathPart = new PathPart($this->options['path_part']); |
||
| 50 | $downloadPath = new Path([$rootPathPart, $platformPathPart]); |
||
| 51 | |||
| 52 | // Try to create the downloads directory... 'cause if it fails, nothing will work. |
||
| 53 | (new Filesystem())->mkdir((string) $downloadPath); |
||
| 54 | |||
| 55 | // Add the platform path part and get a collection of downloads |
||
| 56 | $downloads = new Collection(); |
||
| 57 | foreach ($contents as $content) { |
||
| 58 | $content->getPath()->add($platformPathPart); |
||
| 59 | |||
| 60 | foreach ($this->extractDownloads($content) as $download) { |
||
| 61 | $downloads->add($download); |
||
| 62 | } |
||
| 63 | } |
||
| 64 | |||
| 65 | $this->cleanFilesystem($downloads, $downloadPath); |
||
| 66 | |||
| 67 | $downloads = $this->filterAlreadyDownloaded($downloads); |
||
| 68 | |||
| 69 | if ($this->shouldDownload($downloads, $downloadPath)) { |
||
|
1 ignored issue
–
show
|
|||
| 70 | $this->download($downloads); |
||
|
1 ignored issue
–
show
|
|||
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @param \App\Domain\Content $content |
||
| 76 | * |
||
| 77 | * @return \App\Platform\YouTube\Download[]|\App\Domain\Collection |
||
| 78 | */ |
||
| 79 | private function extractDownloads(Content $content): Collection |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @param \App\Platform\YouTube\Download[]|\App\Domain\Collection $downloads |
||
| 103 | * |
||
| 104 | * @return \App\Platform\YouTube\Download[]|\App\Domain\Collection |
||
| 105 | */ |
||
| 106 | private function filterAlreadyDownloaded(Collection $downloads): Collection |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @param \App\Platform\YouTube\Download[]|\App\Domain\Collection $downloads |
||
| 126 | * @param \App\Domain\Path $downloadPath |
||
| 127 | * |
||
| 128 | * @return bool |
||
| 129 | */ |
||
| 130 | private function shouldDownload(Collection $downloads, Path $downloadPath): bool |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @param \App\Platform\YouTube\Download[]|\App\Domain\Collection $downloads |
||
| 161 | * |
||
| 162 | * @throws \RuntimeException |
||
| 163 | */ |
||
| 164 | private function download(Collection $downloads) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @param \App\Platform\YouTube\Download $download |
||
| 217 | * @param array $youtubeDlOptions |
||
| 218 | * |
||
| 219 | * @throws \Exception |
||
| 220 | */ |
||
| 221 | private function doDownload(Download $download, array $youtubeDlOptions) |
||
| 256 | } |
||
| 257 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.