Omdb::getByTitle()   B
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 36
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 36
ccs 0
cts 31
cp 0
rs 8.439
c 0
b 0
f 0
cc 5
eloc 23
nc 3
nop 1
crap 30
1
<?php
2
/**
3
 * Created by Marcin.
4
 * Date: 09.12.2017
5
 * Time: 22:46
6
 */
7
8
namespace mrcnpdlk\Xmdb;
9
10
11
use Campo\UserAgent;
12
use Curl\Curl;
13
use HttpLib\Http;
14
use mrcnpdlk\Xmdb\Model\Omdb\Title;
15
use mrcnpdlk\Xmdb\Model\Ratio;
16
17
class Omdb
18
{
19
    /**
20
     * @var \mrcnpdlk\Xmdb\Client
21
     */
22
    private $oClient;
23
    /**
24
     * @var \Psr\Log\LoggerInterface
25
     */
26
    private $oLog;
27
    /**
28
     * @var \mrcnpdlk\Psr16Cache\Adapter
29
     */
30
    private $oAdapter;
31
    /**
32
     * @var string
33
     */
34
    private $sToken;
35
    /**
36
     * @var string
37
     */
38
    private $url;
39
40
    /**
41
     * Omdb constructor.
42
     *
43
     * @param \mrcnpdlk\Xmdb\Client $oClient
44
     *
45
     * @throws \mrcnpdlk\Xmdb\Exception
46
     */
47
    public function __construct(Client $oClient)
48
    {
49
        $this->oClient  = $oClient;
50
        $this->oLog     = $oClient->getLogger();
51
        $this->oAdapter = $oClient->getAdapter();
52
        $this->sToken   = $oClient->getOmdbToken();
53
        $this->url      = 'http://www.omdbapi.com/?r=json&apikey=' . $oClient->getOmdbToken();
54
    }
55
56
    /**
57
     * @param string                          $imdbId
58
     *
59
     * @param \mrcnpdlk\Xmdb\Model\Ratio|null $oRatio
60
     *
61
     * @return Title
62
     * @throws \mrcnpdlk\Xmdb\Exception
63
     */
64
    public function getByImdbId(string $imdbId, Ratio $oRatio = null)
65
    {
66
        try {
67
            $oResp = $this->oClient->getAdapter()->useCache(
68
                function () use ($imdbId) {
69
                    $oCurl = new Curl();
70
                    $oCurl->setUserAgent(UserAgent::random());
71
                    $oCurl->setHeader('Accept-Language', $this->oClient->getLang());
72
                    $params = [
73
                        'i'    => $imdbId,
74
                        'plot' => 'full',
75
                        'r'    => 'json',
76
                    ];
77
                    $oCurl->get($this->url . '&' . http_build_query($params));
78
79
                    if ($oCurl->error) {
80
                        throw new \RuntimeException('Curl Error! ' . ($oCurl->httpStatusCode ? Http::message($oCurl->httpStatusCode) : 'Unknown code'),
81
                            $oCurl->error_code);
82
                    }
83
                    if ($oCurl->response->Response !== 'True') {
84
                        throw new \RuntimeException($oCurl->response->Error);
85
                    }
86
87
                    return $oCurl->response;
88
                },
89
                [__METHOD__, $imdbId, $this->oClient->getLang()],
90
                3600 * 2)
91
            ;
92
93
            $oTitle = Title::create($oResp);
94
95
            if ($oRatio) {
96
                $oRatio->calculateRatio([$oTitle]);
0 ignored issues
show
Documentation introduced by
array($oTitle) is of type array<integer,object<mrc...\Model\\Omdb\\Title>"}>, but the function expects a array<integer,object<mrc...Xmdb\Model\Imdb\Title>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
97
            }
98
99
            return $oTitle;
100
101
        } catch (\Exception $e) {
102
            throw new Exception($e->getMessage(), 1, $e);
103
        }
104
    }
105
106
    /**
107
     * @param string $title
108
     *
109
     * @return Title
110
     * @throws \mrcnpdlk\Xmdb\Exception
111
     */
112
    public function getByTitle(string $title)
113
    {
114
        try {
115
            $oResp = $this->oClient->getAdapter()->useCache(
116
                function () use ($title) {
117
                    $oCurl = new Curl();
118
                    $oCurl->setUserAgent(UserAgent::random());
119
                    $oCurl->setHeader('Accept-Language', $this->oClient->getLang());
120
                    $params = [
121
                        't'    => $title,
122
                        'plot' => 'full',
123
                        'r'    => 'json',
124
                    ];
125
                    $oCurl->get($this->url . '&' . http_build_query($params));
126
127
                    if ($oCurl->error) {
128
                        throw new \RuntimeException('Curl Error! ' . ($oCurl->httpStatusCode ? Http::message($oCurl->httpStatusCode) : 'Unknown code'),
129
                            $oCurl->error_code);
130
                    }
131
                    if ($oCurl->response->Response !== 'True') {
132
                        throw new \RuntimeException($oCurl->response->Error);
133
                    }
134
135
                    return $oCurl->response;
136
                },
137
                [__METHOD__, $title, $this->oClient->getLang()],
138
                3600 * 2)
139
            ;
140
141
142
            return Title::create($oResp);
143
144
        } catch (\Exception $e) {
145
            throw new Exception($e->getMessage(), 1, $e);
146
        }
147
    }
148
149
}
150