Passed
Push — master ( d37a32...6985b0 )
by vincent
39s
created

Element::getBackdropPath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php
2
3
namespace vfalies\tmdb\Abstracts;
4
5
abstract class Element
6
{
7
    protected $data = null;
8
    protected $conf = null;
9
10
    /**
11
     * Get item poster
12
     * @param string $size
13
     * @return string
14
     */
15 21
    public function getPoster(string $size = 'w185'): string
16
    {
17 21
        return $this->getImage('poster', $size);
18
    }
19
20
    /**
21
     * Get item backdrop
22
     * @param string $size
23
     * @return string|null
24
     */
25 21
    public function getBackdrop(string $size = 'w780'): string
26
    {
27 21
        return $this->getImage('backdrop', $size);
28
    }
29
30
    /**
31
     * Get poster path
32
     */
33 6
    public function getPosterPath(): string
34
    {
35 6
        if (isset($this->data->poster_path))
36
        {
37 3
            return $this->data->poster_path;
38
        }
39 3
        return '';
40
    }
41
42
    /**
43
     * Get backdrop
44
     */
45 6
    public function getBackdropPath(): string
46
    {
47 6
        if (isset($this->data->backdrop_path))
48
        {
49 3
            return $this->data->backdrop_path;
50
        }
51 3
        return '';
52
    }
53
54
    /**
55
     * Get image url from type and size
56
     * @param string $type
57
     * @param string $size
58
     * @return string
59
     * @throws \Exception
60
     */
61 42
    private function getImage(string $type, string $size): string
62
    {
63 42
        $path = $type . '_path';
64 42
        if (isset($this->data->$path))
65
        {
66 36
            if (!isset($this->conf->images->base_url))
67
            {
68 12
                throw new \Exception('base_url configuration not found');
69
            }
70 24
            $sizes = $type . '_sizes';
71 24
            if (!in_array($size, $this->conf->images->$sizes))
72
            {
73 12
                throw new \Exception('Incorrect ' . $type . ' size : ' . $size);
74
            }
75 12
            return $this->conf->images->base_url . $size . $this->data->$path;
76
        }
77 6
        return '';
78
    }
79
80
}
81