| Conditions | 11 |
| Paths | 11 |
| Total Lines | 94 |
| Code Lines | 56 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php declare(strict_types=1); |
||
| 99 | private function download(Collection $downloads, Path $downloadPath) |
||
| 100 | { |
||
| 101 | // Try to create the downloads directory... 'cause if it fails, nothing will work. |
||
| 102 | (new Filesystem())->mkdir((string) $downloadPath); |
||
| 103 | |||
| 104 | $this->ui->writeln('Download files from YouTube... '.PHP_EOL); |
||
| 105 | |||
| 106 | // Filter out downloads that have already been downloaded |
||
| 107 | /** @var \App\Platform\YouTube\Download[]|\App\Domain\Collection $downloads */ |
||
| 108 | $downloads = $downloads->filter( |
||
| 109 | function (Download $download) { |
||
| 110 | $shouldBeDownloaded = true; |
||
| 111 | try { |
||
| 112 | if ($this->getDownloadFolderFinder($download)->hasResults()) { |
||
| 113 | $shouldBeDownloaded = false; |
||
| 114 | } |
||
| 115 | } catch (\InvalidArgumentException $e) { |
||
| 116 | // Here we know that the download folder will exist. |
||
| 117 | } |
||
| 118 | |||
| 119 | return $shouldBeDownloaded; |
||
| 120 | } |
||
| 121 | ); |
||
| 122 | |||
| 123 | if ($downloads->isEmpty()) { |
||
| 124 | $this->ui->writeln($this->ui->indent().'<comment>Nothing to download.</comment>'.PHP_EOL); |
||
| 125 | |||
| 126 | return; |
||
| 127 | } |
||
| 128 | |||
| 129 | $this->ui->writeln( |
||
| 130 | sprintf( |
||
| 131 | '%sThe script is about to download <question> %s </question> files into <info>%s</info>. '.PHP_EOL, |
||
| 132 | $this->ui->indent(), |
||
| 133 | $downloads->count(), |
||
| 134 | (string) $downloadPath |
||
| 135 | ) |
||
| 136 | ); |
||
| 137 | |||
| 138 | $this->ui->write($this->ui->indent()); |
||
| 139 | if ($this->skip() || !$this->ui->confirm()) { |
||
| 140 | $this->ui->writeln(PHP_EOL.'<info>Done.</info>'.PHP_EOL); |
||
| 141 | |||
| 142 | return; |
||
| 143 | } |
||
| 144 | |||
| 145 | $errors = []; |
||
| 146 | foreach ($downloads as $download) { |
||
| 147 | $this->ui->write( |
||
| 148 | sprintf( |
||
| 149 | '%s* [<comment>%s</comment>][<comment>%s</comment>] Download the %s file in <info>%s</info>... ', |
||
| 150 | $this->ui->indent(2), |
||
| 151 | $download->getVideoId(), |
||
| 152 | $download->getFileType(), |
||
| 153 | $download->getFileExtension(), |
||
| 154 | $download->getPath() |
||
| 155 | ) |
||
| 156 | ); |
||
| 157 | |||
| 158 | $options = $this->options['youtube_dl']['options'][$download->getFileType()]; |
||
| 159 | $nbAttempts = \count($options); |
||
| 160 | |||
| 161 | $attempt = 0; |
||
| 162 | while (true) { |
||
| 163 | try { |
||
| 164 | $this->doDownload($download, $options[$attempt]); |
||
| 165 | |||
| 166 | $this->ui->writeln('<info>Done.</info>'); |
||
| 167 | break; |
||
| 168 | |||
| 169 | } catch (YoutubeDlException\ChannelRemovedByUserException |
||
| 170 | | YoutubeDlException\VideoBlockedByCopyrightException |
||
| 171 | | YoutubeDlException\VideoRemovedByUserException |
||
| 172 | | YoutubeDlException\VideoUnavailableException |
||
| 173 | $e) { |
||
| 174 | $this->ui->logError($e->getMessage(), $errors); |
||
| 175 | break; |
||
| 176 | |||
| 177 | // These are (supposedly) connection/download errors, so we try again |
||
| 178 | } catch (\Exception $e) { |
||
| 179 | $attempt++; |
||
| 180 | |||
| 181 | // Maximum number of attempts reached, move along... |
||
| 182 | if ($attempt === $nbAttempts) { |
||
| 183 | $this->ui->logError($e->getMessage(), $errors); |
||
| 184 | break; |
||
| 185 | } |
||
| 186 | } |
||
| 187 | } |
||
| 188 | } |
||
| 189 | $this->ui->displayErrors($errors, 'download of files', 'error', 1); |
||
| 190 | |||
| 191 | $this->ui->writeln(PHP_EOL.'<info>Done.</info>'.PHP_EOL); |
||
| 192 | } |
||
| 193 | |||
| 236 |