Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
OmdbApi
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 2
6
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 fetch
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace App\Services;
4
5// composer require symfony/http-client
6
7use App\Models\OmdbApiModel;
8use Symfony\Component\Serializer\SerializerInterface;
9use Symfony\Contracts\HttpClient\HttpClientInterface;
10
11
12class OmdbApi 
13{
14    /**
15     * Clé pour accéder à l'API
16     *
17     * @var string
18     */
19    private $apiKey;
20
21    /**
22     * Service client HTTP
23     *
24     * @var HttpClientInterface
25     */
26    private $client;
27
28    /**
29     * Service de désérialisation
30     *
31     * @var SerializerInterface
32     */
33    private $serializer;
34
35    public function __construct(HttpClientInterface $client,SerializerInterface $serializerInterface, $apiKey)
36    {
37        $this->client = $client;
38        $this->apiKey = $apiKey;
39        $this->serializer = $serializerInterface;
40    }
41
42    public function fetch(string $title): OmdbApiModel
43    {
44        $response = $this->client->request(
45            'GET',
46            'http://www.omdbapi.com/', [
47                // these values are automatically encoded before including them in the URL
48                'query' => [
49                    't' => $title,
50                    'apiKey' => $this->apiKey,
51                ],
52            ]
53        );
54
55        // $statusCode = $response->getStatusCode();
56        // $statusCode = 200
57        // $contentType = $response->getHeaders()['content-type'][0];
58        // $contentType = 'application/json'
59        $content = $response->getContent();
60        // $content = '{"id":521583, "name":"symfony-docs", ...}'
61        // $content = $response->toArray();
62        // $content = ['id' => 521583, 'name' => 'symfony-docs', ...]
63        
64        /** @var OmdbApiModel $omdbApiModel */
65        $omdbApiModel = $this->serializer->deserialize($content, OmdbApiModel::class, 'json');
66
67        // dd($omdbApiModel);
68
69        return $omdbApiModel;
70    }
71}
72
73