Completed
Pull Request — master (#23)
by vincent
04:12 queued 02:01
created

Search   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 199
Duplicated Lines 24.12 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 6
dl 48
loc 199
ccs 55
cts 55
cp 1
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B searchItem() 0 25 3
A searchItemGenerator() 0 10 2
A searchCompany() 12 12 2
A getPage() 0 4 1
A getTotalPages() 0 4 1
A getTotalResults() 0 4 1
A searchMovie() 9 12 2
A searchTVShow() 9 12 2
A searchCollection() 9 12 2
A searchPeople() 9 12 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 searchMovie($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, __NAMESPACE__."\\Results\\".'Movie');
93
        }
94 2
        catch (TmdbException $ex)
95
        {
96 2
            throw $ex;
97
        }
98
    }
99
100
    /**
101
     * Search a TV Show
102
     * @param string $query Query string to search like a TV Show
103
     * @param array $options Array of options for the request
104
     * @return \Generator|Results\TVShow
105
     * @throws TmdbException
106
     */
107 8 View Code Duplication
    public function searchTVShow($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...
108
    {
109
        try
110
        {
111 8
            $this->logger->debug('Starting search tv show');
112 8
            return $this->searchItem('tv', $query, $options, __NAMESPACE__."\\Results\\".'TVShow');
113
        }
114 2
        catch (TmdbException $ex)
115
        {
116 2
            throw $ex;
117
        }
118
    }
119
120
    /**
121
     * Search a collection
122
     * @param string $query Query string to search like a collection
123
     * @param array $options Array of option for the request
124
     * @return \Generator|Results\Collection
125
     * @throws TmdbException
126
     */
127 5 View Code Duplication
    public function searchCollection($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...
128
    {
129
        try
130
        {
131 5
            $this->logger->debug('Starting search collection');
132 5
            return $this->searchItem('collection', $query, $options, __NAMESPACE__."\\Results\\".'Collection');
133
        }
134 2
        catch (TmdbException $ex)
135
        {
136 2
            throw $ex;
137
        }
138
    }
139
140
    /**
141
     * Search a people
142
     * @param string $query Query string to search like a people
143
     * @param array $options Array of option for the request
144
     * @return \Generator|Results\People
145
     * @throws TmdbException
146
     */
147 9 View Code Duplication
    public function searchPeople($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...
148
    {
149
        try
150
        {
151 9
            $this->logger->debug('Starting search people');
152 9
            return $this->searchItem('people', $query, $options, __NAMESPACE__."\\Results\\".'People');
153
        }
154 2
        catch (TmdbException $ex)
155
        {
156 2
            throw $ex;
157
        }
158
    }
159
160
    /**
161
     * Search a company
162
     * @param string $query Query string to search like a company
163
     * @param array $options Array of option for the request
164
     * @return \Generator|Results\Company
165
     * @throws TmdbException
166
     */
167 7 View Code Duplication
    public function searchCompany($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...
168
    {
169
        try
170
        {
171 7
            $this->logger->debug('Starting search company');
172 7
            return $this->searchItem('company', $query, $options, __NAMESPACE__."\\Results\\".'Company');
173
        }
174 2
        catch (TmdbException $ex)
175
        {
176 2
            throw $ex;
177
        }
178
    }
179
180
    /**
181
     * Get page from result search
182
     * @return int
183
     */
184 1
    public function getPage()
185
    {
186 1
        return $this->page;
187
    }
188
189
    /**
190
     * Get total page from result search
191
     * @return int
192
     */
193 1
    public function getTotalPages()
194
    {
195 1
        return $this->total_pages;
196
    }
197
198
    /**
199
     * Get total results from search
200
     * @return int
201
     */
202 1
    public function getTotalResults()
203
    {
204 1
        return $this->total_results;
205
    }
206
207
}
208