Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
16 / 16 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| OmdbApiRomain | |
100.00% |
16 / 16 |
|
100.00% |
2 / 2 |
2 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| fetch | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Services; |
| 4 | |
| 5 | // composer require symfony/http-client |
| 6 | use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 7 | |
| 8 | class OmdbApiRomain |
| 9 | { |
| 10 | // Clé dans .env |
| 11 | private $apiKey; |
| 12 | /** |
| 13 | * Service client HTTP |
| 14 | * |
| 15 | * @var HttpClientInterface |
| 16 | */ |
| 17 | private $client; |
| 18 | |
| 19 | |
| 20 | public function __construct(HttpClientInterface $client, $apiKey) |
| 21 | { |
| 22 | $this->client = $client; |
| 23 | $this->apiKey = $apiKey; |
| 24 | } |
| 25 | |
| 26 | |
| 27 | public function fetch(string $title): array |
| 28 | { |
| 29 | $response = $this->client->request( |
| 30 | 'GET', |
| 31 | 'http://www.omdbapi.com/', [ |
| 32 | // these values are automatically encoded before including them in the URL |
| 33 | 'query' => [ |
| 34 | 't' => $title, |
| 35 | 'apiKey' => $this->apiKey, |
| 36 | ], |
| 37 | ] |
| 38 | ); |
| 39 | |
| 40 | |
| 41 | $statusCode = $response->getStatusCode(); |
| 42 | // $statusCode = 200 |
| 43 | $contentType = $response->getHeaders()['content-type'][0]; |
| 44 | // $contentType = 'application/json' |
| 45 | $content = $response->getContent(); |
| 46 | // $content = '{"id":521583, "name":"symfony-docs", ...}' |
| 47 | $content = $response->toArray(); |
| 48 | // $content = ['id' => 521583, 'name' => 'symfony-docs', ...] |
| 49 | |
| 50 | return $content; |
| 51 | } |
| 52 | } |