Completed
Push — master ( 3e3269...414f6c )
by Valentyn
05:10
created

TmdbSearchService::request()   B

Complexity

Conditions 5
Paths 13

Size

Total Lines 35
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 12.9233

Importance

Changes 0
Metric Value
dl 0
loc 35
ccs 7
cts 22
cp 0.3182
rs 8.439
c 0
b 0
f 0
cc 5
eloc 23
nc 13
nop 3
crap 12.9233
1
<?php
2
declare(strict_types=1);
3
4
namespace App\Movies\Service;
5
6
use App\Movies\Exception\TmdbMovieNotFoundException;
7
use App\Movies\Exception\TmdbRequestLimitException;
8
use GuzzleHttp\ClientInterface;
9
use GuzzleHttp\Exception\GuzzleException;
10
use Psr\Log\LoggerInterface;
11
12
class TmdbSearchService
13
{
14
    private $apiKey;
15
    private $client;
16
    private $logger;
17
    private const ApiUrl = 'https://api.themoviedb.org/3';
18
19 7
    public function __construct(LoggerInterface $logger, ClientInterface $client)
20
    {
21 7
        $this->apiKey = \getenv('MOVIE_DB_API_KEY'); // is it ok to use \getenv() here?
22 7
        $this->client = $client;
23 7
        $this->logger = $logger;
24 7
    }
25
26
    /**
27
     * @param string $query
28
     * @param string $locale
29
     * @param array $data
30
     * @return array
31
     * @throws TmdbMovieNotFoundException
32
     * @throws TmdbRequestLimitException
33
     */
34 1
    public function findMoviesByQuery(string $query, string $locale = 'en', $data = []): array
35
    {
36 1
        $data = array_merge([
37 1
            'api_key' => $this->apiKey,
38 1
            'language' => $locale,
39 1
            'query' => $query
40 1
        ], $data);
41
42 1
        $movies = $this->request('/search/movie', 'GET', [
43 1
            'query' => $data,
44
        ]);
45
46 1
        return $movies;
47
    }
48
49
    /**
50
     * @param int $tmdb_id
51
     * @param string $locale
52
     * @return array
53
     * @throws TmdbMovieNotFoundException
54
     * @throws TmdbRequestLimitException
55
     */
56 1
    public function findMovieById(int $tmdb_id, string $locale = 'en'): array
57
    {
58 1
        $movie = $this->request("/movie/{$tmdb_id}", 'GET', [
59
            'query' => [
60 1
                'api_key' => $this->apiKey,
61 1
                'language' => $locale,
62
            ],
63
        ]);
64
65 1
        return $movie;
66
    }
67
68
    /**
69
     * @param int $tmdb_id
70
     * @return array
71
     * @throws TmdbMovieNotFoundException
72
     * @throws TmdbRequestLimitException
73
     */
74
    public function findMovieTranslationsById(int $tmdb_id): array
75
    {
76
        $movie = $this->request("/movie/{$tmdb_id}/translations", 'GET', [
77
            'query' => [
78
                'api_key' => $this->apiKey,
79
            ],
80
        ]);
81
82
        return $movie;
83
    }
84
85 2
    private function request(string $url, string $method = 'GET', array $params = []): array
86
    {
87 2
        $url = self::ApiUrl . $url;
88
89
        try {
90 2
            $response = $this->client->request($method, $url, $params);
91 2
            $response = json_decode($response->getBody()->getContents(), true);
92 2
            getenv('APP_ENV') === 'dev' && $this->logger->debug('Guzzle request:', [
93
                'url' => $url,
94
                'method' => $method,
95
                'params' => $params,
96 2
                'response' => $response,
97
            ]);
98
        } catch (GuzzleException $exception) {
99
            $this->logger->error('Guzzle request failed.', [
100
                'url' => $url,
101
                'method' => $method,
102
                'params' => $params,
103
                'exceptionMessage' => $exception->getMessage(),
104
                'exceptionCode' => $exception->getCode(),
105
            ]);
106
107
            $response = [];
108
109
            if ($exception->getCode() == 404) {
110
                throw new TmdbMovieNotFoundException();
111
            }
112
113
            if ($exception->getCode() == 429) {
114
                throw new TmdbRequestLimitException();
115
            }
116
        }
117
118 2
        return $response;
119
    }
120
}