1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace vfalies\tmdb\Items; |
4
|
|
|
|
5
|
|
|
abstract class Item |
6
|
|
|
{ |
7
|
|
|
protected $data = null; |
8
|
|
|
protected $conf = null; |
9
|
|
|
protected $id = null; |
10
|
|
|
protected $tmdb = null; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Constructor |
14
|
|
|
* @param \vfalies\tmdb\Tmdb $tmdb |
15
|
|
|
* @param int $item_id |
16
|
|
|
* @param array $options |
17
|
|
|
* @param string $item_name |
18
|
|
|
* @throws \Exception |
19
|
|
|
*/ |
20
|
71 |
|
public function __construct(\vfalies\tmdb\Tmdb $tmdb, int $item_id, array $options, string $item_name) |
21
|
|
|
{ |
22
|
|
|
try |
23
|
|
|
{ |
24
|
71 |
|
$this->id = (int) $item_id; |
25
|
71 |
|
$this->tmdb = $tmdb; |
26
|
71 |
|
$this->conf = $this->tmdb->getConfiguration(); |
27
|
71 |
|
$params = $this->tmdb->checkOptions($options); |
28
|
71 |
|
$this->data = $this->tmdb->sendRequest(new \vfalies\tmdb\CurlRequest(), $item_name . '/'.(int) $item_id, null, $params); |
29
|
|
|
} |
30
|
3 |
|
catch (\Exception $ex) |
31
|
|
|
{ |
32
|
3 |
|
throw new \Exception($ex->getMessage(), $ex->getCode(), $ex); |
33
|
|
|
} |
34
|
68 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get item poster |
38
|
|
|
* @param string $size |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
12 |
View Code Duplication |
public function getPoster(string $size = 'w185'): string |
|
|
|
|
42
|
|
|
{ |
43
|
12 |
|
if (isset($this->data->poster_path)) |
44
|
|
|
{ |
45
|
9 |
|
if (!isset($this->conf->images->base_url)) |
46
|
|
|
{ |
47
|
3 |
|
throw new \Exception('base_url configuration not found'); |
48
|
|
|
} |
49
|
6 |
|
if (!in_array($size, $this->conf->images->poster_sizes)) |
50
|
|
|
{ |
51
|
3 |
|
throw new \Exception('Incorrect poster size : ' . $size); |
52
|
|
|
} |
53
|
3 |
|
return $this->conf->images->base_url . $size . $this->data->poster_path; |
54
|
|
|
} |
55
|
3 |
|
return ''; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get item backdrop |
60
|
|
|
* @param string $size |
61
|
|
|
* @return string|null |
62
|
|
|
*/ |
63
|
12 |
View Code Duplication |
public function getBackdrop(string $size = 'w780'): string |
|
|
|
|
64
|
|
|
{ |
65
|
12 |
|
if (isset($this->data->backdrop_path)) |
66
|
|
|
{ |
67
|
9 |
|
if (!isset($this->conf->images->base_url)) |
68
|
|
|
{ |
69
|
3 |
|
throw new \Exception('base_url configuration not found'); |
70
|
|
|
} |
71
|
6 |
|
if (!in_array($size, $this->conf->images->backdrop_sizes)) |
72
|
|
|
{ |
73
|
3 |
|
throw new \Exception('Incorrect backdrop size : ' . $size); |
74
|
|
|
} |
75
|
3 |
|
return $this->conf->images->base_url . $size . $this->data->backdrop_path; |
76
|
|
|
} |
77
|
3 |
|
return ''; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.