| Conditions | 5 |
| Paths | 9 |
| Total Lines | 34 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 30 |
| Changes | 0 | ||
| 1 | <?php |
||
| 16 | public static function savePoster(int $movieId, string $posterUrl): ?string |
||
| 17 | { |
||
| 18 | $saveTo = str_replace('{movieId}', $movieId, self::BASE_PATH); |
||
| 19 | $destinationDir = dirname($saveTo); |
||
| 20 | |||
| 21 | if (is_dir($destinationDir) === false) { |
||
| 22 | if (is_file($destinationDir)) { |
||
| 23 | unlink($destinationDir); |
||
| 24 | } |
||
| 25 | mkdir($destinationDir); |
||
| 26 | } |
||
| 27 | |||
| 28 | $ch = curl_init($posterUrl); |
||
| 29 | curl_setopt($ch, CURLOPT_HEADER, 0); |
||
| 30 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
||
| 31 | curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); |
||
| 32 | $raw = curl_exec($ch); |
||
| 33 | |||
| 34 | if (curl_errno($ch) !== 0) { |
||
| 35 | curl_close($ch); |
||
| 36 | return null; |
||
| 37 | } |
||
| 38 | |||
| 39 | curl_close($ch); |
||
| 40 | |||
| 41 | if (file_exists($saveTo)) { |
||
| 42 | unlink($saveTo); |
||
| 43 | } |
||
| 44 | $fp = fopen($saveTo, 'x'); |
||
| 45 | fwrite($fp, $raw); |
||
| 46 | fclose($fp); |
||
| 47 | |||
| 48 | return $saveTo; |
||
| 49 | } |
||
| 50 | |||
| 59 | } |