Completed
Push — master ( 259b4f...1970e3 )
by Marcin
03:08
created

Omdb::getByTitle()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 36
Code Lines 23

Duplication

Lines 36
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 36
loc 36
ccs 0
cts 31
cp 0
rs 8.439
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
16
class Omdb
17
{
18
    /**
19
     * @var \mrcnpdlk\Xmdb\Client
20
     */
21
    private $oClient;
22
    /**
23
     * @var \Psr\Log\LoggerInterface
24
     */
25
    private $oLog;
26
    /**
27
     * @var \mrcnpdlk\Psr16Cache\Adapter
28
     */
29
    private $oAdapter;
30
    /**
31
     * @var string
32
     */
33
    private $sToken;
34
    /**
35
     * @var string
36
     */
37
    private $url;
38
39
    /**
40
     * Omdb constructor.
41
     *
42
     * @param \mrcnpdlk\Xmdb\Client $oClient
43
     *
44
     * @throws \mrcnpdlk\Xmdb\Exception
45
     */
46
    public function __construct(Client $oClient)
47
    {
48
        $this->oClient  = $oClient;
49
        $this->oLog     = $oClient->getLogger();
50
        $this->oAdapter = $oClient->getAdapter();
51
        $this->sToken   = $oClient->getOmdbToken();
52
        $this->url      = 'http://www.omdbapi.com/?r=json&apikey=' . $oClient->getOmdbToken();
53
    }
54
55
    /**
56
     * @param string $imdbId
57
     *
58
     * @return Title
59
     * @throws \mrcnpdlk\Xmdb\Exception
60
     */
61 View Code Duplication
    public function getByImdbId(string $imdbId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
    {
63
        try {
64
            $oResp = $this->oClient->getAdapter()->useCache(
65
                function () use ($imdbId) {
66
                    $oCurl = new Curl();
67
                    $oCurl->setUserAgent(UserAgent::random());
68
                    $oCurl->setHeader('Accept-Language', $this->oClient->getLang());
69
                    $params = [
70
                        'i'    => $imdbId,
71
                        'plot' => 'full',
72
                        'r'    => 'json',
73
                    ];
74
                    $oCurl->get($this->url . '&' . http_build_query($params));
75
76
                    if ($oCurl->error) {
77
                        throw new \RuntimeException('Curl Error! ' . ($oCurl->httpStatusCode ? Http::message($oCurl->httpStatusCode) : 'Unknown code'),
78
                            $oCurl->error_code);
79
                    }
80
                    if ($oCurl->response->Response !== 'True') {
81
                        throw new \RuntimeException($oCurl->response->Error);
82
                    }
83
84
                    return $oCurl->response;
85
                },
86
                [__METHOD__, $imdbId, $this->oClient->getLang()],
87
                3600 * 2)
88
            ;
89
90
91
            return Title::create($oResp);
92
93
        } catch (\Exception $e) {
94
            throw new Exception($e->getMessage(), 1, $e);
95
        }
96
    }
97
98
    /**
99
     * @param string $title
100
     *
101
     * @return Title
102
     * @throws \mrcnpdlk\Xmdb\Exception
103
     */
104 View Code Duplication
    public function getByTitle(string $title)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105
    {
106
        try {
107
            $oResp = $this->oClient->getAdapter()->useCache(
108
                function () use ($title) {
109
                    $oCurl = new Curl();
110
                    $oCurl->setUserAgent(UserAgent::random());
111
                    $oCurl->setHeader('Accept-Language', $this->oClient->getLang());
112
                    $params = [
113
                        't'    => $title,
114
                        'plot' => 'full',
115
                        'r'    => 'json',
116
                    ];
117
                    $oCurl->get($this->url . '&' . http_build_query($params));
118
119
                    if ($oCurl->error) {
120
                        throw new \RuntimeException('Curl Error! ' . ($oCurl->httpStatusCode ? Http::message($oCurl->httpStatusCode) : 'Unknown code'),
121
                            $oCurl->error_code);
122
                    }
123
                    if ($oCurl->response->Response !== 'True') {
124
                        throw new \RuntimeException($oCurl->response->Error);
125
                    }
126
127
                    return $oCurl->response;
128
                },
129
                [__METHOD__, $title, $this->oClient->getLang()],
130
                3600 * 2)
131
            ;
132
133
134
            return Title::create($oResp);
135
136
        } catch (\Exception $e) {
137
            throw new Exception($e->getMessage(), 1, $e);
138
        }
139
    }
140
}
141