1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace App\Movies\Service; |
5
|
|
|
|
6
|
|
|
use App\Movies\Exception\TmdbMovieNotFoundException; |
7
|
|
|
use GuzzleHttp\Client; |
8
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
9
|
|
|
use Psr\Log\LoggerInterface; |
10
|
|
|
|
11
|
|
|
class TmdbSearchService |
12
|
|
|
{ |
13
|
|
|
private $apiKey; |
14
|
|
|
private $client; |
15
|
|
|
private $logger; |
16
|
|
|
private const ApiUrl = 'https://api.themoviedb.org/3'; |
17
|
|
|
|
18
|
7 |
|
public function __construct(LoggerInterface $logger) |
19
|
|
|
{ |
20
|
7 |
|
$this->apiKey = \getenv('MOVIE_DB_API_KEY'); |
21
|
7 |
|
$this->client = new Client(); |
22
|
7 |
|
$this->logger = $logger; |
23
|
7 |
|
} |
24
|
|
|
|
25
|
1 |
|
public function findMoviesByQuery(string $query, string $locale = 'en', $data = []): array |
26
|
|
|
{ |
27
|
1 |
|
$data = array_merge([ |
28
|
1 |
|
'api_key' => $this->apiKey, |
29
|
1 |
|
'language' => $locale, |
30
|
1 |
|
'query' => $query |
31
|
1 |
|
], $data); |
32
|
|
|
|
33
|
1 |
|
$movies = $this->request('/search/movie', 'GET', [ |
34
|
1 |
|
'query' => $data, |
35
|
|
|
]); |
36
|
|
|
|
37
|
1 |
|
return $movies; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param int $tmdb_id |
42
|
|
|
* @param string $locale |
43
|
|
|
* @return array |
44
|
|
|
* @throws TmdbMovieNotFoundException |
45
|
|
|
*/ |
46
|
1 |
|
public function findMovieById(int $tmdb_id, string $locale = 'en'): array |
47
|
|
|
{ |
48
|
1 |
|
$movie = $this->request("/movie/{$tmdb_id}", 'GET', [ |
49
|
|
|
'query' => [ |
50
|
1 |
|
'api_key' => $this->apiKey, |
51
|
1 |
|
'language' => $locale, |
52
|
|
|
], |
53
|
|
|
]); |
54
|
|
|
|
55
|
1 |
|
if (isset($movie['status_code']) && $movie['status_code'] == 34) { |
56
|
|
|
throw new TmdbMovieNotFoundException(); |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
return $movie; |
60
|
|
|
} |
61
|
|
|
|
62
|
2 |
|
private function request(string $url, string $method = 'GET', array $params = []): array |
63
|
|
|
{ |
64
|
2 |
|
$url = self::ApiUrl . $url; |
65
|
|
|
|
66
|
|
|
try { |
67
|
2 |
|
$response = $this->client->request($method, $url, $params); |
68
|
2 |
|
$response = json_decode($response->getBody()->getContents(), true); |
69
|
2 |
|
getenv('APP_ENV') === 'dev' && $this->logger->debug('Guzzle request:', [ |
70
|
|
|
'url' => $url, |
71
|
|
|
'method' => $method, |
72
|
|
|
'params' => $params, |
73
|
2 |
|
'response' => $response, |
74
|
|
|
]); |
75
|
|
|
} catch (GuzzleException $exception) { |
76
|
|
|
$this->logger->error('Guzzle request failed.', [ |
77
|
|
|
'url' => $url, |
78
|
|
|
'method' => $method, |
79
|
|
|
'params' => $params, |
80
|
|
|
'exceptionMessage' => $exception->getMessage(), |
81
|
|
|
'exceptionCode' => $exception->getCode(), |
82
|
|
|
]); |
83
|
|
|
|
84
|
|
|
$response = []; |
85
|
|
|
|
86
|
|
|
if ($exception->getCode() == 404) { |
87
|
|
|
$response = [ |
88
|
|
|
'status_code' => 34 |
89
|
|
|
]; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
2 |
|
return $response; |
94
|
|
|
} |
95
|
|
|
} |