| Total Complexity | 40 |
| Total Lines | 348 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 1 |
Complex classes like Export 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 Export, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | abstract class Export |
||
| 27 | { |
||
| 28 | use Formats; |
||
| 29 | |||
| 30 | /** @var object */ |
||
| 31 | protected $media; |
||
| 32 | |||
| 33 | /** @var array */ |
||
| 34 | protected $path_info; |
||
| 35 | |||
| 36 | /** @var string */ |
||
| 37 | protected $strict = "-2"; |
||
| 38 | |||
| 39 | /** @var string */ |
||
| 40 | protected $tmp_dir; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Export constructor. |
||
| 44 | * @param Media $media |
||
| 45 | */ |
||
| 46 | public function __construct(Media $media) |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @param string $path |
||
| 54 | * @param array $clouds |
||
| 55 | * @param bool $metadata |
||
| 56 | * @return mixed |
||
| 57 | * @throws Exception |
||
| 58 | */ |
||
| 59 | public function save(string $path = null, array $clouds = [], bool $metadata = true) |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param $path |
||
| 83 | * @param $clouds |
||
| 84 | * @throws Exception |
||
| 85 | */ |
||
| 86 | private function createPathInfoAndTmpDir($path, $clouds): void |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @param $path |
||
| 104 | * @throws Exception |
||
| 105 | */ |
||
| 106 | private function tmpDirectory($path) |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Run FFmpeg to package media content |
||
| 120 | */ |
||
| 121 | private function runFFmpeg(): void |
||
| 131 | ); |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @return Filter |
||
| 137 | */ |
||
| 138 | abstract protected function getFilter(): Filter; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @return string |
||
| 142 | */ |
||
| 143 | private function getPath(): string |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @param string|null $path |
||
| 162 | * @throws Exception |
||
| 163 | */ |
||
| 164 | private function moveTmpFolder(?string $path) |
||
| 165 | { |
||
| 166 | if ($this->tmp_dir && $path) { |
||
| 167 | FileManager::moveDir($this->tmp_dir, pathinfo($path, PATHINFO_DIRNAME) . DIRECTORY_SEPARATOR); |
||
| 168 | $this->path_info = pathinfo($path); |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @return array |
||
| 174 | */ |
||
| 175 | public function getPathInfo(): array |
||
| 176 | { |
||
| 177 | return $this->path_info; |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @return object|Media |
||
| 182 | */ |
||
| 183 | public function getMedia(): Media |
||
| 184 | { |
||
| 185 | return $this->media; |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @param string $strict |
||
| 190 | * @return Export |
||
| 191 | */ |
||
| 192 | public function setStrict(string $strict): Export |
||
| 193 | { |
||
| 194 | $this->strict = $strict; |
||
| 195 | return $this; |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @return string |
||
| 200 | */ |
||
| 201 | public function getStrict(): string |
||
| 202 | { |
||
| 203 | return $this->strict; |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * clear tmp files |
||
| 208 | */ |
||
| 209 | public function __destruct() |
||
| 210 | { |
||
| 211 | sleep(1); |
||
| 212 | |||
| 213 | if ($this->media->isTmp()) { |
||
| 214 | @unlink($this->media->getPath()); |
||
|
|
|||
| 215 | } |
||
| 216 | |||
| 217 | if ($this->tmp_dir) { |
||
| 218 | FileManager::deleteDirectory($this->tmp_dir); |
||
| 219 | } |
||
| 220 | |||
| 221 | if ($this instanceof HLS && $this->tmp_key_info_file) { |
||
| 222 | @unlink($this->getHlsKeyInfoFile()); |
||
| 223 | } |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @param string $url |
||
| 228 | * @param string $name |
||
| 229 | * @param string|null $path |
||
| 230 | * @param string $method |
||
| 231 | * @param array $headers |
||
| 232 | * @param array $options |
||
| 233 | * @return mixed |
||
| 234 | * @throws Exception |
||
| 235 | * @deprecated this method is deprecated |
||
| 236 | */ |
||
| 237 | // @TODO: should be removed in the next releases. |
||
| 238 | public function saveToCloud( |
||
| 239 | string $url, |
||
| 240 | string $name, |
||
| 241 | string $path = null, |
||
| 242 | string $method = 'GET', |
||
| 243 | array $headers = [], |
||
| 244 | array $options = [] |
||
| 245 | ) |
||
| 246 | { |
||
| 247 | @trigger_error('saveToCloud method is deprecated and will be removed in a future release. Use Cloud instead', E_USER_DEPRECATED); |
||
| 248 | if ($this instanceof HLS && $this->getTsSubDirectory()) { |
||
| 249 | throw new InvalidArgumentException("It is not possible to create subdirectory in a cloud"); |
||
| 250 | } |
||
| 251 | $results = $this->saveToTemporaryFolder($path); |
||
| 252 | sleep(1); |
||
| 253 | |||
| 254 | $cloud = new Cloud($url, $method, $options); |
||
| 255 | $cloud->uploadDirectory($this->tmp_dir, ['name' => $name, 'headers' => $headers]); |
||
| 256 | |||
| 257 | $this->moveTmpFolder($path); |
||
| 258 | |||
| 259 | return $results; |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @param array $config |
||
| 264 | * @param string $dest |
||
| 265 | * @param string|null $path |
||
| 266 | * @return mixed |
||
| 267 | * @throws Exception |
||
| 268 | * @deprecated this method is deprecated |
||
| 269 | */ |
||
| 270 | // @TODO: should be removed in the next releases. |
||
| 271 | public function saveToS3( |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @param array $config |
||
| 291 | * @param string $bucket |
||
| 292 | * @param string|null $path |
||
| 293 | * @param array $options |
||
| 294 | * @param bool $userProject |
||
| 295 | * @return mixed |
||
| 296 | * @throws Exception |
||
| 297 | * @deprecated this method is deprecated |
||
| 298 | */ |
||
| 299 | // @TODO: should be removed in the next releases. |
||
| 300 | public function saveToGCS( |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @param string $connectionString |
||
| 326 | * @param string $container |
||
| 327 | * @param string|null $path |
||
| 328 | * @return mixed |
||
| 329 | * @throws Exception |
||
| 330 | * @deprecated this method is deprecated |
||
| 331 | */ |
||
| 332 | // @TODO: should be removed in the next releases. |
||
| 333 | public function saveToMAS( |
||
| 334 | string $connectionString, |
||
| 335 | string $container, |
||
| 336 | string $path = null |
||
| 337 | ) |
||
| 338 | { |
||
| 339 | @trigger_error('saveToMAS method is deprecated and will be removed in a future release. Use MicrosoftAzure instead', E_USER_DEPRECATED); |
||
| 340 | |||
| 341 | if ($this instanceof HLS && $this->getTsSubDirectory()) { |
||
| 342 | throw new InvalidArgumentException("It is not possible to create subdirectory in a cloud"); |
||
| 343 | } |
||
| 344 | |||
| 345 | $results = $this->saveToTemporaryFolder($path); |
||
| 346 | sleep(1); |
||
| 347 | |||
| 348 | $google_cloud = new MicrosoftAzure($connectionString); |
||
| 349 | $google_cloud->uploadDirectory($this->tmp_dir, ['container' => $container]); |
||
| 350 | |||
| 351 | $this->moveTmpFolder($path); |
||
| 352 | |||
| 353 | return $results; |
||
| 354 | } |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @param $path |
||
| 358 | * @return array |
||
| 359 | * @throws Exception |
||
| 360 | * @deprecated this method is deprecated |
||
| 361 | */ |
||
| 362 | // @TODO: should be removed in the next releases. |
||
| 363 | private function saveToTemporaryFolder($path) |
||
| 374 | } |
||
| 375 | } |
If you suppress an error, we recommend checking for the error condition explicitly: