1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace vfalies\tmdb; |
4
|
|
|
|
5
|
|
|
class Movie implements Interfaces\MovieInterface |
6
|
|
|
{ |
7
|
|
|
|
8
|
|
|
private $tmdb = null; |
9
|
|
|
private $conf = null; |
10
|
|
|
private $data = null; |
11
|
|
|
private $id = null; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Constructor |
15
|
|
|
* @param \vfalies\tmdb\Tmdb $tmdb |
16
|
|
|
* @param int $movie_id |
17
|
|
|
* @param array $options |
18
|
|
|
* @throws Exception |
19
|
|
|
*/ |
20
|
28 |
View Code Duplication |
public function __construct(Tmdb $tmdb, int $movie_id, array $options = array()) |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
try |
23
|
|
|
{ |
24
|
28 |
|
$this->id = (int) $movie_id; |
25
|
28 |
|
$this->tmdb = $tmdb; |
26
|
28 |
|
$this->conf = $this->tmdb->getConfiguration(); |
27
|
|
|
|
28
|
|
|
// Get movie details |
29
|
28 |
|
$params = $this->tmdb->checkOptions($options); |
30
|
28 |
|
$this->data = $this->tmdb->sendRequest(new CurlRequest(), 'movie/' . $movie_id, null, $params); |
31
|
|
|
} |
32
|
1 |
|
catch (\Exception $ex) |
33
|
|
|
{ |
34
|
1 |
|
throw new \Exception($ex->getMessage(), $ex->getCode(), $ex); |
35
|
|
|
} |
36
|
27 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Get movie genres |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
2 |
|
public function getGenres(): array |
43
|
|
|
{ |
44
|
2 |
|
if (isset($this->data->genres)) |
45
|
|
|
{ |
46
|
1 |
|
return $this->data->genres; |
47
|
|
|
} |
48
|
1 |
|
return []; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get movie title |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
2 |
|
public function getTitle(): string |
56
|
|
|
{ |
57
|
2 |
|
if (isset($this->data->title)) |
58
|
|
|
{ |
59
|
1 |
|
return $this->data->title; |
60
|
|
|
} |
61
|
1 |
|
return ''; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get movie overview |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
2 |
|
public function getOverview(): string |
69
|
|
|
{ |
70
|
2 |
|
if (isset($this->data->overview)) |
71
|
|
|
{ |
72
|
1 |
|
return $this->data->overview; |
73
|
|
|
} |
74
|
1 |
|
return ''; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get movie release date |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
2 |
|
public function getReleaseDate(): string |
82
|
|
|
{ |
83
|
2 |
|
if (isset($this->data->release_date)) |
84
|
|
|
{ |
85
|
1 |
|
return $this->data->release_date; |
86
|
|
|
} |
87
|
1 |
|
return ''; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get movie original title |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
2 |
|
public function getOriginalTitle(): string |
95
|
|
|
{ |
96
|
2 |
|
if (isset($this->data->original_title)) |
97
|
|
|
{ |
98
|
1 |
|
return $this->data->original_title; |
99
|
|
|
} |
100
|
1 |
|
return ''; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get movie note |
105
|
|
|
* @return float |
106
|
|
|
*/ |
107
|
2 |
|
public function getNote(): float |
108
|
|
|
{ |
109
|
2 |
|
if (isset($this->data->vote_average)) |
110
|
|
|
{ |
111
|
1 |
|
return $this->data->vote_average; |
112
|
|
|
} |
113
|
1 |
|
return 0; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Get movie id |
118
|
|
|
* @return int |
119
|
|
|
*/ |
120
|
1 |
|
public function getId(): int |
121
|
|
|
{ |
122
|
1 |
|
return $this->id; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Get IMDB movie id |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
2 |
|
public function getIMDBId(): string |
130
|
|
|
{ |
131
|
2 |
|
if (isset($this->data->imdb_id)) |
132
|
|
|
{ |
133
|
1 |
|
return $this->data->imdb_id; |
134
|
|
|
} |
135
|
1 |
|
return ''; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Get movie tagline |
140
|
|
|
* @return string |
141
|
|
|
*/ |
142
|
2 |
|
public function getTagLine(): string |
143
|
|
|
{ |
144
|
2 |
|
if (isset($this->data->tagline)) |
145
|
|
|
{ |
146
|
1 |
|
return $this->data->tagline; |
147
|
|
|
} |
148
|
1 |
|
return ''; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Get collection id |
153
|
|
|
* @return int |
154
|
|
|
*/ |
155
|
2 |
|
public function getCollectionId(): int |
156
|
|
|
{ |
157
|
2 |
|
if (!empty($this->data->belongs_to_collection)) |
158
|
|
|
{ |
159
|
1 |
|
return (int) $this->data->belongs_to_collection->id; |
160
|
|
|
} |
161
|
1 |
|
return 0; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Get movie poster |
166
|
|
|
* @param string $size |
167
|
|
|
* @return string |
168
|
|
|
*/ |
169
|
4 |
View Code Duplication |
public function getPoster(string $size = 'w185'): string |
|
|
|
|
170
|
|
|
{ |
171
|
4 |
|
if (isset($this->data->poster_path)) |
172
|
|
|
{ |
173
|
3 |
|
if (!isset($this->conf->images->base_url)) |
174
|
|
|
{ |
175
|
1 |
|
throw new \Exception('base_url configuration not found'); |
176
|
|
|
} |
177
|
2 |
|
if (!in_array($size, $this->conf->images->poster_sizes)) |
178
|
|
|
{ |
179
|
1 |
|
throw new \Exception('Incorrect poster size : ' . $size); |
180
|
|
|
} |
181
|
1 |
|
return $this->conf->images->base_url . $size . $this->data->poster_path; |
182
|
|
|
} |
183
|
1 |
|
return ''; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Get movie backdrop |
188
|
|
|
* @param string $size |
189
|
|
|
* @return string|null |
190
|
|
|
*/ |
191
|
4 |
View Code Duplication |
public function getBackdrop(string $size = 'w780'): string |
|
|
|
|
192
|
|
|
{ |
193
|
4 |
|
if (isset($this->data->backdrop_path)) |
194
|
|
|
{ |
195
|
3 |
|
if (!isset($this->conf->images->base_url)) |
196
|
|
|
{ |
197
|
1 |
|
throw new \Exception('base_url configuration not found'); |
198
|
|
|
} |
199
|
2 |
|
if (!in_array($size, $this->conf->images->backdrop_sizes)) |
200
|
|
|
{ |
201
|
1 |
|
throw new \Exception('Incorrect backdrop size : ' . $size); |
202
|
|
|
} |
203
|
1 |
|
return $this->conf->images->base_url . $size . $this->data->backdrop_path; |
204
|
|
|
} |
205
|
1 |
|
return ''; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
} |
209
|
|
|
|
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.