Passed
Push — master ( 1077fc...b3c432 )
by vincent
49s
created

Search   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 161
Duplicated Lines 22.36 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 6
dl 36
loc 161
ccs 50
cts 50
cp 1
rs 10
c 0
b 0
f 0

10 Methods

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

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 31
    public function __construct(Tmdb $tmdb)
24
    {
25 31
        $this->tmdb   = $tmdb;
26 31
        $this->logger = $tmdb->logger;
27 31
    }
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 31
    private function searchItem($item, $query, array $options, $result_class)
39
    {
40
        try {
41 31
            $this->logger->debug('Starting search item');
42 31
            $query = trim($query);
43 31
            if (empty($query)) {
44 4
                $this->logger->error('Query param cannot be empty', array('item' => $item, 'query' => $query, 'options' => $options, 'result_class' => $result_class));
45 4
                throw new IncorrectParamException;
46
            }
47 27
            $params   = $this->tmdb->checkOptions($options);
48 23
            $response = $this->tmdb->sendRequest(new HttpClient(new \GuzzleHttp\Client()), 'search/' . $item, $query, $params);
49
50 23
            $this->page          = (int) $response->page;
51 23
            $this->total_pages   = (int) $response->total_pages;
52 23
            $this->total_results = (int) $response->total_results;
53
54 23
            return $this->searchItemGenerator($response->results, $result_class);
55 8
        } catch (TmdbException $ex) {
56 8
            throw $ex;
57
        }
58
    }
59
60
    /**
61
     * Search Item generator method
62
     * @param array $results
63
     * @param string $class
64
     */
65 23
    private function searchItemGenerator(array $results, $class)
66
    {
67 23
        $this->logger->debug('Starting search item generator');
68 23
        foreach ($results as $result) {
69 21
            $element = new $class($this->tmdb, $result);
70
71 21
            yield $element;
72
        }
73 2
    }
74
75
    /**
76
     * Search a movie
77
     * @param string $query Query string to search like a movie
78
     * @param array $options Array of options for the search
79
     * @return \Generator|Results\Movie
80
     * @throws TmdbException
81
     */
82 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...
83
    {
84
        try {
85 9
            $this->logger->debug('Starting search movie');
86 9
            return $this->searchItem('movie', $query, $options, __NAMESPACE__ . "\\Results\\" . 'Movie');
87 2
        } catch (TmdbException $ex) {
88 2
            throw $ex;
89
        }
90
    }
91
92
    /**
93
     * Search a TV Show
94
     * @param string $query Query string to search like a TV Show
95
     * @param array $options Array of options for the request
96
     * @return \Generator|Results\TVShow
97
     * @throws TmdbException
98
     */
99 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...
100
    {
101
        try {
102 8
            $this->logger->debug('Starting search tv show');
103 8
            return $this->searchItem('tv', $query, $options, __NAMESPACE__ . "\\Results\\" . 'TVShow');
104 2
        } catch (TmdbException $ex) {
105 2
            throw $ex;
106
        }
107
    }
108
109
    /**
110
     * Search a collection
111
     * @param string $query Query string to search like a collection
112
     * @param array $options Array of option for the request
113
     * @return \Generator|Results\Collection
114
     * @throws TmdbException
115
     */
116 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...
117
    {
118
        try {
119 5
            $this->logger->debug('Starting search collection');
120 5
            return $this->searchItem('collection', $query, $options, __NAMESPACE__ . "\\Results\\" . 'Collection');
121 2
        } catch (TmdbException $ex) {
122 2
            throw $ex;
123
        }
124
    }
125
126
    /**
127
     * Search a people
128
     * @param string $query Query string to search like a people
129
     * @param array $options Array of option for the request
130
     * @return \Generator|Results\People
131
     * @throws TmdbException
132
     */
133 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...
134
    {
135
        try {
136 9
            $this->logger->debug('Starting search people');
137 9
            return $this->searchItem('people', $query, $options, __NAMESPACE__ . "\\Results\\" . 'People');
138 2
        } catch (TmdbException $ex) {
139 2
            throw $ex;
140
        }
141
    }
142
143
    /**
144
     * Get page from result search
145
     * @return int
146
     */
147 1
    public function getPage()
148
    {
149 1
        return $this->page;
150
    }
151
152
    /**
153
     * Get total page from result search
154
     * @return int
155
     */
156 1
    public function getTotalPages()
157
    {
158 1
        return $this->total_pages;
159
    }
160
161
    /**
162
     * Get total results from search
163
     * @return int
164
     */
165 1
    public function getTotalResults()
166
    {
167 1
        return $this->total_results;
168
    }
169
}
170