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