Passed
Branch account (733a83)
by vincent
02:35
created

Search::searchItemGenerator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 9
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 9
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
crap 2
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 38
    public function __construct(TmdbInterface $tmdb)
50
    {
51 38
        $this->tmdb   = $tmdb;
52 38
        $this->logger = $tmdb->getLogger();
53 38
    }
54
55
    /**
56
     * Search specify item
57
     * @param string $item item to search : movie / tv / collection
58
     * @param string $query Query string to search like a $item
59
     * @param array $options Array of options for the request
60
     * @param string $result_class class name of the wanted result
61
     * @return \Generator
62
     * @throws TmdbException
63
     */
64 38
    private function searchItem(string $item, string $query, array $options, string $result_class) : \Generator
65
    {
66
        try {
67 38
            $this->logger->debug('Starting search item', array('item' => $item, 'query' => $query, 'options' => $options, 'result_class' => $result_class));
68 38
            $query = trim($query);
69 38
            if (empty($query)) {
70 5
                $this->logger->error('Query param cannot be empty', array('item' => $item, 'query' => $query, 'options' => $options, 'result_class' => $result_class));
71 5
                throw new IncorrectParamException;
72
            }
73 33
            $options['query'] = $query;
74 33
            $params           = $this->tmdb->checkOptions($options);
75 28
            $response         = $this->tmdb->getRequest('search/' . $item, $params);
76
77 28
            $this->page          = (int) $response->page;
78 28
            $this->total_pages   = (int) $response->total_pages;
79 28
            $this->total_results = (int) $response->total_results;
80
81 28
            return $this->searchItemGenerator($response->results, $result_class);
82 10
        } catch (TmdbException $ex) {
83 10
            throw $ex;
84
        }
85
    }
86
87
    /**
88
     * Search a movie
89
     * @param string $query Query string to search like a movie
90
     * @param array $options Array of options for the search
91
     * @return \Generator|Results\Movie
92
     * @throws TmdbException
93
     */
94 9 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...
95
    {
96
        try {
97 9
            $this->logger->debug('Starting search movie', array('query' => $query, 'options' => $options));
98 9
            return $this->searchItem('movie', $query, $options, Results\Movie::class);
99 2
        } catch (TmdbException $ex) {
100 2
            throw $ex;
101
        }
102
    }
103
104
    /**
105
     * Search a TV Show
106
     * @param string $query Query string to search like a TV Show
107
     * @param array $options Array of options for the request
108
     * @return \Generator|Results\TVShow
109
     * @throws TmdbException
110
     */
111 8 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...
112
    {
113
        try {
114 8
            $this->logger->debug('Starting search tv show', array('query' => $query, 'options' => $options));
115 8
            return $this->searchItem('tv', $query, $options, Results\TVShow::class);
116 2
        } catch (TmdbException $ex) {
117 2
            throw $ex;
118
        }
119
    }
120
121
    /**
122
     * Search a collection
123
     * @param string $query Query string to search like a collection
124
     * @param array $options Array of option for the request
125
     * @return \Generator|Results\Collection
126
     * @throws TmdbException
127
     */
128 5 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...
129
    {
130
        try {
131 5
            $this->logger->debug('Starting search collection', array('query' => $query, 'options' => $options));
132 5
            return $this->searchItem('collection', $query, $options, Results\Collection::class);
133 2
        } catch (TmdbException $ex) {
134 2
            throw $ex;
135
        }
136
    }
137
138
    /**
139
     * Search a people
140
     * @param string $query Query string to search like a people
141
     * @param array $options Array of option for the request
142
     * @return \Generator|Results\People
143
     * @throws TmdbException
144
     */
145 9 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...
146
    {
147
        try {
148 9
            $this->logger->debug('Starting search people', array('query' => $query, 'options' => $options));
149 9
            return $this->searchItem('person', $query, $options, Results\People::class);
150 2
        } catch (TmdbException $ex) {
151 2
            throw $ex;
152
        }
153
    }
154
155
    /**
156
     * Search a company
157
     * @param string $query Query string to search like a company
158
     * @param array $options Array of option for the request
159
     * @return \Generator|Results\Company
160
     * @throws TmdbException
161
     */
162 7 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...
163
    {
164
        try {
165 7
            $this->logger->debug('Starting search company', array('query' => $query, 'options' => $options));
166 7
            return $this->searchItem('company', $query, $options, Results\Company::class);
167 2
        } catch (TmdbException $ex) {
168 2
            throw $ex;
169
        }
170
    }
171
}
172