Passed
Pull Request — master (#9)
by vincent
02:46
created

Element   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 0
dl 0
loc 76
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getPoster() 0 4 1
A getBackdrop() 0 4 1
A getPosterPath() 0 8 2
A getBackdropPath() 0 8 2
A getImage() 0 18 4
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