ImdbService   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 54
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A search() 0 19 3
A infos() 0 23 4
1
<?php
2
3
namespace KI\PonthubBundle\Service;
4
5
use KI\CoreBundle\Service\CurlService;
6
7
// Échange des informations avec l'API Imdb pour récupérer des informations
8
// sur les films et les séries (utilisé par Ponthub)
9
// Testé par PonthubControllerTest
10
class ImdbService
11
{
12
    protected $curlService;
13
    protected $baseUrl = 'http://www.omdbapi.com/';
14
15
    public function __construct(CurlService $curlService)
16
    {
17
        $this->curlService = $curlService;
18
    }
19
20
    public function search($name)
21
    {
22
        $url = $this->baseUrl.'?s='.urlencode($name);
23
        $response = json_decode($this->curlService->curl($url), true);
24
25
        $return = [];
26
        if (isset($response['Search'])) {
27
            foreach ($response['Search'] as $result) {
28
                $return[] = [
29
                    'name' => $result['Title'],
30
                    'year' => $result['Year'],
31
                    'type' => $result['Type'],
32
                    'id'   => $result['imdbID']
33
                ];
34
            }
35
        }
36
37
        return $return;
38
    }
39
40
    public function infos($id)
41
    {
42
        $url = $this->baseUrl.'?i='.urlencode($id);
43
        $response = json_decode($this->curlService->curl($url), true);
44
45
        if (!isset($response['Title']))
46
            return null;
47
48
        if (preg_match('#\d–\d#', $response['Year']))
49
            $response['Year'] = explode('–', $response['Year'])[0];
50
51
        return [
52
            'title'       => $response['Title'],
53
            'year'        => (int)$response['Year'],
54
            'duration'    => 60*str_replace(' min', '', $response['Runtime']),
55
            'genres'      => explode(', ', $response['Genre']),
56
            'director'    => $response['Director'],
57
            'actors'      => explode(', ', $response['Actors']),
58
            'description' => $response['Plot'],
59
            'image'       => $response['Poster'],
60
            'rating'      => isset($response['Metascore']) ? $response['Metascore'] : $response['imdbRating']*10
61
        ];
62
    }
63
}
64