Passed
Branch master (fc0106)
by vincent
02:48
created

Search::people()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 12
loc 12
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 3
nop 2
crap 2
1
<?php
2
3
namespace vfalies\tmdb;
4
5
use vfalies\tmdb\lib\Guzzle\Client as HttpClient;
6
use vfalies\tmdb\Exceptions\IncorrectParamException;
7
use vfalies\tmdb\Exceptions\TmdbException;
8
9
class Search
10
{
11
12
    private $tmdb          = null;
13
    private $logger        = null;
14
    private $page          = 1; // Page number of the search result
15
    private $total_pages   = 1; // Total pages of the search result
16
    private $total_results = 0; // Total results of the search result
17
18
    /**
19
     * Constructor
20
     * @param \vfalies\tmdb\Tmdb $tmdb
21
     */
22
23 38
    public function __construct(Tmdb $tmdb)
24
    {
25 38
        $this->tmdb   = $tmdb;
26 38
        $this->logger = $tmdb->logger;
27 38
    }
28
29
    /**
30
     * Search specify item
31
     * @param string $item item to search : movie / tv / collection
32
     * @param string $query Query string to search like a $item
33
     * @param array $options Array of options for the request
34
     * @param string $result_class class name of the wanted result
35
     * @return \Generator
36
     * @throws TmdbException
37
     */
38 38
    private function searchItem($item, $query, array $options, $result_class)
39
    {
40
        try
41
        {
42 38
            $this->logger->debug('Starting search item');
43 38
            $query = trim($query);
44 38
            if (empty($query))
45
            {
46 5
                $this->logger->error('Query param cannot be empty', array('item' => $item, 'query' => $query, 'options' => $options, 'result_class' => $result_class));
47 5
                throw new IncorrectParamException;
48
            }
49 33
            $params   = $this->tmdb->checkOptions($options);
50 28
            $response = $this->tmdb->sendRequest(new HttpClient(new \GuzzleHttp\Client()), 'search/'.$item, $query, $params);
51
52 28
            $this->page          = (int) $response->page;
53 28
            $this->total_pages   = (int) $response->total_pages;
54 28
            $this->total_results = (int) $response->total_results;
55
56 28
            return $this->searchItemGenerator($response->results, $result_class);
57
        }
58 10
        catch (TmdbException $ex)
59
        {
60 10
            throw $ex;
61
        }
62
    }
63
64
    /**
65
     * Search Item generator method
66
     * @param array $results
67
     * @param string $class
68
     */
69 28
    private function searchItemGenerator(array $results, $class)
70
    {
71 28
        $this->logger->debug('Starting search item generator');
72 28
        foreach ($results as $result)
73
        {
74 25
            $element = new $class($this->tmdb, $result);
75
76 25
            yield $element;
77
        }
78 3
    }
79
80
    /**
81
     * Search a movie
82
     * @param string $query Query string to search like a movie
83
     * @param array $options Array of options for the search
84
     * @return \Generator|Results\Movie
85
     * @throws TmdbException
86
     */
87 9 View Code Duplication
    public function movie($query, array $options = array())
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
88
    {
89
        try
90
        {
91 9
            $this->logger->debug('Starting search movie');
92 9
            return $this->searchItem('movie', $query, $options, Results\Movie::class);
93 2
        } catch (TmdbException $ex) {
94 2
            throw $ex;
95
        }
96
    }
97
98
    /**
99
     * Search a TV Show
100
     * @param string $query Query string to search like a TV Show
101
     * @param array $options Array of options for the request
102
     * @return \Generator|Results\TVShow
103
     * @throws TmdbException
104
     */
105 8 View Code Duplication
    public function tvshow($query, array $options = array())
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
106
    {
107
        try
108
        {
109 8
            $this->logger->debug('Starting search tv show');
110 8
            return $this->searchItem('tv', $query, $options, Results\TVShow::class);
111 2
        } catch (TmdbException $ex) {
112 2
            throw $ex;
113
        }
114
    }
115
116
    /**
117
     * Search a collection
118
     * @param string $query Query string to search like a collection
119
     * @param array $options Array of option for the request
120
     * @return \Generator|Results\Collection
121
     * @throws TmdbException
122
     */
123 5 View Code Duplication
    public function collection($query, array $options = array())
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
124
    {
125
        try
126
        {
127 5
            $this->logger->debug('Starting search collection');
128 5
            return $this->searchItem('collection', $query, $options, Results\Collection::class);
129 2
        } catch (TmdbException $ex) {
130 2
            throw $ex;
131
        }
132
    }
133
134
    /**
135
     * Search a people
136
     * @param string $query Query string to search like a people
137
     * @param array $options Array of option for the request
138
     * @return \Generator|Results\People
139
     * @throws TmdbException
140
     */
141 9 View Code Duplication
    public function people($query, array $options = array())
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
142
    {
143
        try
144
        {
145 9
            $this->logger->debug('Starting search people');
146 9
            return $this->searchItem('people', $query, $options, Results\People::class);           
147
        }
148 2
        catch (TmdbException $ex)
149
        {
150 2
            throw $ex;
151
        }
152
    }
153
154
    /**
155
     * Search a company
156
     * @param string $query Query string to search like a company
157
     * @param array $options Array of option for the request
158
     * @return \Generator|Results\Company
159
     * @throws TmdbException
160
     */
161 7 View Code Duplication
    public function company($query, array $options = array())
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
162
    {
163
        try
164
        {
165 7
            $this->logger->debug('Starting search company');
166 7
            return $this->searchItem('people', $query, $options, Results\Company::class);
167
        }
168 2
        catch (TmdbException $ex)
169
        {
170 2
            throw $ex;
171
        }
172
    }
173
174
    /**
175
     * Get page from result search
176
     * @return int
177
     */
178 1
    public function getPage()
179
    {
180 1
        return $this->page;
181
    }
182
183
    /**
184
     * Get total page from result search
185
     * @return int
186
     */
187 1
    public function getTotalPages()
188
    {
189 1
        return $this->total_pages;
190
    }
191
192
    /**
193
     * Get total results from search
194
     * @return int
195
     */
196 1
    public function getTotalResults()
197
    {
198 1
        return $this->total_results;
199
    }
200
201
}
202