Completed
Push — master ( a5ff95...a715f7 )
by Valentyn
04:45
created

Poster   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 66.67%

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 0
dl 0
loc 83
ccs 24
cts 36
cp 0.6667
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A savePoster() 0 37 5
A removePoster() 0 12 4
A getUrl() 0 4 1
A getPath() 0 4 1
1
<?php
2
3
namespace App\Movies\Utils;
4
5
class Poster
6
{
7
    const TMDB_BASE_URL = 'https://image.tmdb.org/t/p/original';
8
    const BASE_URL = '/f/movies/{movieId}/poster.jpg';
9
    const BASE_PATH = __DIR__.'/../../../public/f/movies/{movieId}/poster.jpg';
10
11
    /**
12
     * @param int    $movieId
13
     * @param string $posterUrl
14
     *
15
     * @return null|string
16
     */
17 1
    public static function savePoster(int $movieId, string $posterUrl): ?string
18
    {
19 1
        $saveTo = str_replace('{movieId}', $movieId, self::BASE_PATH);
20 1
        $destinationDir = dirname($saveTo);
21
22 1
        if (is_dir($destinationDir) === false) {
23 1
            if (is_file($destinationDir)) {
24
                unlink($destinationDir);
25
            }
26 1
            mkdir($destinationDir);
27
        }
28
29 1
        $ch = curl_init($posterUrl);
30 1
        curl_setopt($ch, CURLOPT_HEADER, 0);
31 1
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
32 1
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
33 1
        $raw = curl_exec($ch);
34
35 1
        if (curl_errno($ch) !== 0) {
36
            curl_close($ch);
37
38
            return null;
39
        }
40
41 1
        curl_close($ch);
42
43 1
        if (file_exists($saveTo)) {
44
            self::removePoster($movieId);
45
        }
46 1
        $fp = fopen($saveTo, 'x');
47 1
        fwrite($fp, $raw);
48 1
        fclose($fp);
49 1
        chmod($saveTo, 0777);
50 1
        chmod($destinationDir, 0777);
51
52 1
        return $saveTo;
53
    }
54
55
    public static function removePoster(int $movieId): void
56
    {
57
        $saveTo = \str_replace('{movieId}', $movieId, self::BASE_PATH);
58
        $dir = \dirname($saveTo);
59
        $files = \scandir($dir);
60
        foreach ($files as $file) {
61
            if (substr($file, 0, 6) === 'poster' && strpos($file, '.') !== false) {
62
                // if its file like poster.jpg or poster.260x380.jpg - remove it
63
                unlink($dir . DIRECTORY_SEPARATOR . $file);
64
            }
65
        }
66
    }
67
68
    /**
69
     * @param int $movieId
70
     *
71
     * @return string
72
     */
73 1
    public static function getUrl(int $movieId): string
74
    {
75 1
        return str_replace('{movieId}', $movieId, self::BASE_URL);
76
    }
77
78
    /**
79
     * @param int $movieId
80
     *
81
     * @return string
82
     */
83 1
    public static function getPath(int $movieId): string
84
    {
85 1
        return str_replace('{movieId}', $movieId, self::BASE_PATH);
86
    }
87
}
88