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

Element::getPosterPath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 6
1
<?php
2
3
namespace vfalies\tmdb\Abstracts;
4
5
abstract class Element
6
{
7
    /**
8
     * Get item poster
9
     * @param string $size
10
     * @return string
11
     */
12 21
    public function getPoster(string $size = 'w185'): string
13
    {
14 21
        return $this->getImage('poster', $size);
15
    }
16
17
    /**
18
     * Get item backdrop
19
     * @param string $size
20
     * @return string|null
21
     */
22 21
    public function getBackdrop(string $size = 'w780'): string
23
    {
24 21
        return $this->getImage('backdrop', $size);
25
    }
26
27
    /**
28
     * Get poster path
29
     */
30
    public function getPosterPath(): string
31
    {
32
        if (isset($this->data->poster_path))
33
        {
34
            return $this->data->poster_path;
0 ignored issues
show
Bug introduced by
The property data does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
35
        }
36
        return '';
37
    }
38
39
    /**
40
     * Get backdrop
41
     */
42
    public function getBackdropPath(): string
43
    {
44
        if (isset($this->data->backdrop_path))
45
        {
46
            return $this->data->backdrop_path;
47
        }
48
        return '';
49
    }
50
51
    /**
52
     * Get image url from type and size
53
     * @param string $type
54
     * @param string $size
55
     * @return string
56
     * @throws \Exception
57
     */
58 42
    private function getImage(string $type, string $size): string
59
    {
60 42
        $path = $type . '_path';
61 42
        if (isset($this->data->$path))
62
        {
63 36
            if (!isset($this->conf->images->base_url))
0 ignored issues
show
Bug introduced by
The property conf does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
64
            {
65 12
                throw new \Exception('base_url configuration not found');
66
            }
67 24
            $sizes = $type . '_sizes';
68 24
            if (!in_array($size, $this->conf->images->$sizes))
69
            {
70 12
                throw new \Exception('Incorrect ' . $type . ' size : ' . $size);
71
            }
72 12
            return $this->conf->images->base_url . $size . $this->data->$path;
73
        }
74 6
        return '';
75
    }
76
77
}
78