Completed
Push — master ( 9d729c...78ea8b )
by Valentyn
03:06
created

TmdbSearchService::request()   A

Complexity

Conditions 5
Paths 13

Size

Total Lines 35

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