Passed
Branch account (1cb1c3)
by vincent
02:52
created

Search::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of the Tmdb package.
4
 *
5
 * (c) Vincent Faliès <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @author Vincent Faliès <[email protected]>
11
 * @copyright Copyright (c) 2017
12
 */
13
14
15
namespace VfacTmdb;
16
17
use VfacTmdb\Exceptions\IncorrectParamException;
18
use VfacTmdb\Exceptions\TmdbException;
19
use VfacTmdb\Interfaces\TmdbInterface;
20
use VfacTmdb\Traits\ListItems;
21
use VfacTmdb\Traits\GeneratorTrait;
22
23
/**
24
 * Search class
25
 * @package Tmdb
26
 * @author Vincent Faliès <[email protected]>
27
 * @copyright Copyright (c) 2017
28
 */
29
class Search
30
{
31
    use ListItems;
32
    use GeneratorTrait;
33
34
    /**
35
     * Tmdb object
36
     * @var TmdbInterface
37
     */
38
    private $tmdb = null;
39
    /**
40
     * Logger
41
     * @var \Psr\Log\LoggerInterface
42
     */
43
    private $logger = null;
44
45
    /**
46
     * Constructor
47
     * @param TmdbInterface $tmdb
48
     */
49 33
    public function __construct(TmdbInterface $tmdb)
50
    {
51 33
        $this->tmdb   = $tmdb;
52 33
        $this->logger = $tmdb->getLogger();
53 33
        $this->setGeneratorTrait($tmdb);
54 33
    }
55
56
    /**
57
     * Search specify item
58
     * @param string $item item to search : movie / tv / collection
59
     * @param string $query Query string to search like a $item
60
     * @param array $options Array of options for the request
61
     * @param string $result_class class name of the wanted result
62
     * @return \Generator
63
     * @throws TmdbException
64
     */
65 33
    private function searchItem(string $item, string $query, array $options, string $result_class) : \Generator
66
    {
67
        try {
68 33
            $this->logger->debug('Starting search item', array('item' => $item, 'query' => $query, 'options' => $options, 'result_class' => $result_class));
69 33
            $query = trim($query);
70 33
            if (empty($query)) {
71 5
                $this->logger->error('Query param cannot be empty', array('item' => $item, 'query' => $query, 'options' => $options, 'result_class' => $result_class));
72 5
                throw new IncorrectParamException;
73
            }
74 28
            $options['query'] = $query;
75 28
            $params           = [];
76 28
            $this->tmdb->checkOptionQuery($options, $params);
77 28
            $this->tmdb->checkOptionPage($options, $params);
78 28
            $this->tmdb->checkOptionLanguage($options, $params);
79 28
            $this->tmdb->checkOptionIncludeAdult($options, $params);
80 28
            $this->tmdb->checkOptionYear($options, $params);
81
82 28
            $response         = $this->tmdb->getRequest('search/' . $item, $params);
83
            
84 28
            $this->page          = (int) $response->page;
85 28
            $this->total_pages   = (int) $response->total_pages;
86 28
            $this->total_results = (int) $response->total_results;
87
88 28
            return $this->searchItemGenerator($response->results, $result_class);
89 5
        } catch (TmdbException $ex) {
90 5
            throw $ex;
91
        }
92
    }
93
94
    /**
95
     * Search a movie
96
     * @param string $query Query string to search like a movie
97
     * @param array $options Array of options for the search
98
     * @return \Generator|Results\Movie
99
     * @throws TmdbException
100
     */
101 8 View Code Duplication
    public function movie(string $query, array $options = array()) : \Generator
0 ignored issues
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...
102
    {
103
        try {
104 8
            $this->logger->debug('Starting search movie', array('query' => $query, 'options' => $options));
105 8
            return $this->searchItem('movie', $query, $options, Results\Movie::class);
106 1
        } catch (TmdbException $ex) {
107 1
            throw $ex;
108
        }
109
    }
110
111
    /**
112
     * Search a TV Show
113
     * @param string $query Query string to search like a TV Show
114
     * @param array $options Array of options for the request
115
     * @return \Generator|Results\TVShow
116
     * @throws TmdbException
117
     */
118 7 View Code Duplication
    public function tvshow(string $query, array $options = array()) : \Generator
0 ignored issues
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...
119
    {
120
        try {
121 7
            $this->logger->debug('Starting search tv show', array('query' => $query, 'options' => $options));
122 7
            return $this->searchItem('tv', $query, $options, Results\TVShow::class);
123 1
        } catch (TmdbException $ex) {
124 1
            throw $ex;
125
        }
126
    }
127
128
    /**
129
     * Search a collection
130
     * @param string $query Query string to search like a collection
131
     * @param array $options Array of option for the request
132
     * @return \Generator|Results\Collection
133
     * @throws TmdbException
134
     */
135 4 View Code Duplication
    public function collection(string $query, array $options = array()) : \Generator
0 ignored issues
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...
136
    {
137
        try {
138 4
            $this->logger->debug('Starting search collection', array('query' => $query, 'options' => $options));
139 4
            return $this->searchItem('collection', $query, $options, Results\Collection::class);
140 1
        } catch (TmdbException $ex) {
141 1
            throw $ex;
142
        }
143
    }
144
145
    /**
146
     * Search a people
147
     * @param string $query Query string to search like a people
148
     * @param array $options Array of option for the request
149
     * @return \Generator|Results\People
150
     * @throws TmdbException
151
     */
152 8 View Code Duplication
    public function people(string $query, array $options = array()) : \Generator
0 ignored issues
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...
153
    {
154
        try {
155 8
            $this->logger->debug('Starting search people', array('query' => $query, 'options' => $options));
156 8
            return $this->searchItem('person', $query, $options, Results\People::class);
157 1
        } catch (TmdbException $ex) {
158 1
            throw $ex;
159
        }
160
    }
161
162
    /**
163
     * Search a company
164
     * @param string $query Query string to search like a company
165
     * @param array $options Array of option for the request
166
     * @return \Generator|Results\Company
167
     * @throws TmdbException
168
     */
169 6 View Code Duplication
    public function company(string $query, array $options = array()) : \Generator
0 ignored issues
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...
170
    {
171
        try {
172 6
            $this->logger->debug('Starting search company', array('query' => $query, 'options' => $options));
173 6
            return $this->searchItem('company', $query, $options, Results\Company::class);
174 1
        } catch (TmdbException $ex) {
175 1
            throw $ex;
176
        }
177
    }
178
}
179