Completed
Push — master ( 1302da...3a2ccf )
by Valentyn
03:07
created

TmdbSearchService::request()   B

Complexity

Conditions 4
Paths 9

Size

Total Lines 33
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 8.394

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 7
cts 20
cp 0.35
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 22
nc 9
nop 3
crap 8.394
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
}