|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace vfalies\tmdb\Catalogs; |
|
4
|
|
|
|
|
5
|
|
|
use vfalies\tmdb\Interfaces\GenresInterface; |
|
6
|
|
|
use vfalies\tmdb\Tmdb; |
|
7
|
|
|
use vfalies\tmdb\lib\CurlRequest; |
|
8
|
|
|
|
|
9
|
|
|
class Genres implements GenresInterface |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
protected $tmdb = null; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Constructor |
|
16
|
|
|
* @param \vfalies\tmdb\Tmdb $tmdb |
|
17
|
|
|
* @throws Exception |
|
18
|
|
|
*/ |
|
19
|
5 |
|
public function __construct(Tmdb $tmdb) |
|
20
|
|
|
{ |
|
21
|
5 |
|
$this->tmdb = $tmdb; |
|
22
|
5 |
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Get movie genres list |
|
26
|
|
|
* @param array $options |
|
27
|
|
|
* @return \Generator |
|
28
|
|
|
* @throws \Exception |
|
29
|
|
|
*/ |
|
30
|
3 |
View Code Duplication |
public function getMovieList(array $options = array()): \Generator |
|
|
|
|
|
|
31
|
|
|
{ |
|
32
|
|
|
try |
|
33
|
|
|
{ |
|
34
|
3 |
|
return $this->getList('genre/movie/list', $options); |
|
35
|
1 |
|
} catch (\Exception $ex) |
|
36
|
|
|
{ |
|
37
|
1 |
|
throw new \Exception($ex->getMessage(), $ex->getCode(), $ex); |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Get TV genres list |
|
43
|
|
|
* @param array $options |
|
44
|
|
|
* @return \Generator |
|
45
|
|
|
* @throws \Exception |
|
46
|
|
|
*/ |
|
47
|
2 |
View Code Duplication |
public function getTVList(array $options = array()): \Generator |
|
|
|
|
|
|
48
|
|
|
{ |
|
49
|
|
|
try |
|
50
|
|
|
{ |
|
51
|
2 |
|
return $this->getList('genre/tv/list', $options); |
|
52
|
1 |
|
} catch (\Exception $ex) |
|
53
|
|
|
{ |
|
54
|
1 |
|
throw new \Exception($ex->getMessage(), $ex->getCode(), $ex); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Generic getter list |
|
60
|
|
|
* @param string $type |
|
61
|
|
|
* @param array $options |
|
62
|
|
|
* @return \Generator |
|
63
|
|
|
* @throws \Exception |
|
64
|
|
|
*/ |
|
65
|
5 |
|
private function getList(string $type, array $options): \Generator |
|
66
|
|
|
{ |
|
67
|
|
|
try |
|
68
|
|
|
{ |
|
69
|
5 |
|
$params = $this->tmdb->checkOptions($options); |
|
70
|
5 |
|
$response = $this->tmdb->sendRequest(new CurlRequest(), $type, null, $params); |
|
71
|
|
|
|
|
72
|
3 |
|
$genres = []; |
|
73
|
3 |
|
if (isset($response->genres)) |
|
74
|
|
|
{ |
|
75
|
2 |
|
$genres = $response->genres; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
3 |
|
return $this->genreItemGenerator($genres); |
|
79
|
2 |
|
} catch (\Exception $ex) |
|
80
|
|
|
{ |
|
81
|
2 |
|
throw new \Exception($ex->getMessage(), $ex->getCode(), $ex); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Genre Item generator method |
|
87
|
|
|
* @param array $results |
|
88
|
|
|
*/ |
|
89
|
3 |
|
private function genreItemGenerator(array $results): \Generator |
|
90
|
|
|
{ |
|
91
|
3 |
|
foreach ($results as $result) |
|
92
|
|
|
{ |
|
93
|
2 |
|
yield $result; |
|
94
|
|
|
} |
|
95
|
1 |
|
} |
|
96
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|
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.