Passed
Push — master ( 67aa47...d37a32 )
by vincent
02:27
created

Genres::getTVList()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 10
loc 10
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
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