Find::findItem()   A
last analyzed

Complexity

Conditions 3
Paths 8

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 11
c 0
b 0
f 0
nc 8
nop 4
dl 0
loc 16
ccs 11
cts 11
cp 1
crap 3
rs 9.9
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) 2020
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
use VfacTmdb\Results;
23
24
/**
25
 * Find class
26
 * @package Tmdb
27
 * @author Vincent Faliès <[email protected]>
28
 * @copyright Copyright (c) 2020
29
 */
30
class Find
31
{
32
    use ListItems;
33
    use GeneratorTrait;
34
35
    /**
36
     * Tmdb object
37
     * @var TmdbInterface
38
     */
39
    private $tmdb = null;
40
    /**
41
     * Logger
42
     * @var \Psr\Log\LoggerInterface
43
     */
44
    private $logger = null;
45
46
    /**
47
     * Constructor
48
     * @param TmdbInterface $tmdb
49
     */
50 54
    public function __construct(TmdbInterface $tmdb)
51
    {
52 54
        $this->tmdb   = $tmdb;
53 54
        $this->logger = $tmdb->getLogger();
54 54
        $this->setGeneratorTrait($tmdb);
55 54
    }
56
57
    /**
58
     * Find specify item
59
     * @param string $item item to find : {external_id}
60
     * @param string $query Query string to search like a $item
61
     * @param array $options Array of options for the request
62
     * @param string $result_class class name of the wanted result
63
     * @return Results\Find
64
     * @throws TmdbException
65
     */
66 54
    private function findItem(string $item, string $query, array $options, string $result_class) : Results\Find
67
    {
68
        try {
69 54
            $this->logger->debug('Starting find item', array('item' => $item, 'query' => $query, 'options' => $options, 'result_class' => $result_class));
70 54
            $query = trim($query);
71 54
            $options['query'] = $query;
72 54
            $params           = $this->checkSearchItemOption($options);
73
74 54
            $response         = $this->tmdb->getRequest('find/' . $item, $params);
75 54
            if (!$response instanceof \stdClass) {
76 3
                throw new TmdbException('Incorrect find response');
77
            }
78
79 51
            return new Results\Find($this->tmdb, $response);
80 3
        } catch (TmdbException $ex) {
81 3
            throw $ex;
82
        }
83
    }
84
85
    /**
86
     * Check search item api option
87
     * @param array $options
88
     * @return array
89
     */
90 54
    private function checkSearchItemOption(array $options) : array
91
    {
92 54
        $params = [];
93 54
        $this->tmdb->checkOptionLanguage($options, $params);
94 54
        $this->tmdb->checkOptionExternalSource($options, $params);
95
96 54
        return $params;
97
    }
98
99
    /**
100
     * Find by external id
101
     * @param string $external_id Query string to search like a movie
102
     * @param string $external_source Source of the external_id
103
     *               allowed values: imdb_id, freebase_mid, freebase_id, tvdb_id, tvrage_id, facebook_id, twitter_id, instagram_id
104
     * @param array $options Array of options for the search
105
     * @return Results\Find
106
     * @throws TmdbException
107
     */
108 54
    private function external(string $external_id, string $external_source, array $options = array()) : Results\Find
109
    {
110
        try {
111 54
            $this->logger->debug('Starting find by external id', array('external_id' => $external_id, 'options' => $options));
112 54
            $options['external_source'] = $external_source;
113 54
            return $this->findItem($external_id, '', $options, Results\Movie::class);
114 3
        } catch (TmdbException $ex) {
115 3
            throw $ex;
116
        }
117
    }
118
119
    /**
120
     * Find by external id on IMDB
121
     *
122
     * @param string $external_id
123
     * @param array $options
124
     * @return Results\Find
125
     */
126 39
    public function imdb(string $external_id, array $options = array()) : Results\Find
127
    {
128 39
        return $this->external($external_id, 'imdb_id', $options);
129
    }
130
131
    /**
132
     * Find by external id on Freebase
133
     *
134
     * @param string $external_id
135
     * @param array $options
136
     * @deprecated
137
     * @return Results\Find
138
     */
139
    // public function freebase(string $external_id, array $options = array()) : Results\Find
140
    // {
141
    //     return $this->external($external_id, 'freebase_id', $options);
142
    // }
143
144
    /**
145
     * Find by external id on TVdb
146
     *
147
     * @param string $external_id
148
     * @param array $options
149
     * @return Results\Find
150
     */
151 3
    public function tvdb(string $external_id, array $options = array()) : Results\Find
152
    {
153 3
        return $this->external($external_id, 'tvdb_id', $options);
154
    }
155
156
    /**
157
     * Find by external id on TVRage
158
     *
159
     * @param string $external_id
160
     * @param array $options
161
     * @return Results\Find
162
     */
163 3
    public function tvrage(string $external_id, array $options = array()) : Results\Find
164
    {
165 3
        return $this->external($external_id, 'tvrage_id', $options);
166
    }
167
168
    /**
169
     * Find by external id on Facebook
170
     *
171
     * @param string $external_id
172
     * @param array $options
173
     * @return Results\Find
174
     */
175 3
    public function facebook(string $external_id, array $options = array()) : Results\Find
176
    {
177 3
        return $this->external($external_id, 'facebook_id', $options);
178
    }
179
180
    /**
181
     * Find by external id on Twitter
182
     *
183
     * @param string $external_id
184
     * @param array $options
185
     * @return Results\Find
186
     */
187 3
    public function twitter(string $external_id, array $options = array()) : Results\Find
188
    {
189 3
        return $this->external($external_id, 'twitter_id', $options);
190
    }
191
192
    /**
193
     * Instagram
194
     *
195
     * @param string $external_id
196
     * @param array $options
197
     * @return Results\Find
198
     */
199 3
    public function instagram(string $external_id, array $options = array()) : Results\Find
200
    {
201 3
        return $this->external($external_id, 'instagram_id', $options);
202
    }
203
}
204