Completed
Push — analysis-8nZkrg ( 95fd09 )
by Peter
08:33 queued 07:01
created

Browser::__construct()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 14
cts 14
cp 1
rs 8.9713
c 0
b 0
f 0
cc 2
eloc 21
nc 2
nop 9
crap 2

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 * AnimeDb package.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2011, Peter Gribanov
7
 * @license   http://opensource.org/licenses/GPL-3.0 GPL v3
8
 */
9
10
namespace AnimeDb\Bundle\AniDbBrowserBundle\Service;
11
12
use AnimeDb\Bundle\AniDbBrowserBundle\Util\ResponseRepair;
13
use Symfony\Component\DomCrawler\Crawler;
14
use Guzzle\Http\Client;
15
16
/**
17
 * Browser.
18
 *
19
 * @see http://anidb.net/
20
 * @see http://wiki.anidb.net/w/HTTP_API_Definition
21
 */
22
class Browser
23
{
24
    /**
25
     * @var string
26
     */
27
    private $host;
28
29
    /**
30
     * @var string
31
     */
32
    private $api_prefix;
33
34
    /**
35
     * @var string
36
     */
37
    private $app_code;
38
39
    /**
40
     * @var string
41
     */
42
    private $image_prefix;
43
44
    /**
45
     * @var Client
46
     */
47
    private $client;
48
49
    /**
50
     * @var CacheResponse
51
     */
52
    private $cache;
53
54
    /**
55
     * @var ResponseRepair
56
     */
57
    private $response_repair;
58
59
    /**
60
     * @param Client $client
61
     * @param ResponseRepair $response_repair
62
     * @param string $host
63
     * @param string $api_prefix
64
     * @param string $api_client
65
     * @param string $api_clientver
66
     * @param string $api_protover
67
     * @param string $app_code
68
     * @param string $image_prefix
69
     */
70 9
    public function __construct(
71
        Client $client,
72
        ResponseRepair $response_repair,
73
        $host,
74
        $api_prefix,
75
        $api_client,
76
        $api_clientver,
77
        $api_protover,
78
        $app_code,
79
        $image_prefix
80
    ) {
81 9
        $this->client = $client;
82 9
        $api_prefix .= strpos($api_prefix, '?') !== false ? '&' : '?';
83 9
        $api_prefix .= http_build_query([
84 9
            'client' => $api_client,
85 9
            'clientver' => $api_clientver,
86 9
            'protover' => $api_protover,
87 9
        ]);
88 9
        $this->host = $host;
89 9
        $this->api_prefix = $api_prefix;
90 9
        $this->app_code = $app_code;
91 9
        $this->image_prefix = $image_prefix;
92 9
        $this->response_repair = $response_repair;
93 9
    }
94
95
    /**
96
     * @return string
97
     */
98 1
    public function getHost()
99
    {
100 1
        return $this->host;
101
    }
102
103
    /**
104
     * @return string
105
     */
106 2
    public function getApiHost()
107
    {
108 2
        return $this->client->getBaseUrl();
109
    }
110
111
    /**
112
     * @param int $timeout
113
     *
114
     * @return Browser
115
     */
116 1
    public function setTimeout($timeout)
117
    {
118 1
        $this->client->setDefaultOption('timeout', $timeout);
119
120 1
        return $this;
121
    }
122
123
    /**
124
     * @param string $proxy
125
     *
126
     * @return Browser
127
     */
128 1
    public function setProxy($proxy)
129
    {
130 1
        $this->client->setDefaultOption('proxy', $proxy);
131
132 1
        return $this;
133
    }
134
135
    /**
136
     * @param CacheResponse $cache
137
     */
138 2
    public function setResponseCache(CacheResponse $cache)
139
    {
140 2
        $this->cache = $cache;
141 2
    }
142
143
    /**
144
     * @deprecated get() is deprecated since AniDbBrowser 2.0. Use getCrawler() instead
145
     *
146
     * @param string $request
147
     * @param array $params
148
     * @param bool $force
149
     *
150
     * @return Crawler
151
     */
152
    public function get($request, array $params = [], $force = false)
153
    {
154
        return $this->getCrawler($request, $params, $force);
155
    }
156
157
    /**
158
     * @param string $request
159
     * @param array $params
160
     * @param bool $force
161
     *
162
     * @return string
163
     */
164 4
    public function getContent($request, array $params = [], $force = false)
165
    {
166 4
        $path = $this->api_prefix.'&request='.$request.($params ? '&'.http_build_query($params) : '');
167
168
        // try get response from cache
169 4
        if ($force || !($this->cache instanceof CacheResponse) || !($response = $this->cache->get($path))) {
170 3
            $response = $this->client->get($path)->setHeader('User-Agent', $this->app_code)->send();
171 3
            if ($response->isError()) {
172 1
                throw new \RuntimeException("Failed execute request '{$request}' to the server '".$this->getApiHost()."'");
173
            }
174 2
            $response = gzdecode($response->getBody(true));
175 2
            $response = $this->response_repair->repair($response); // repair
176
177
            // cache response
178 2
            if ($this->cache instanceof CacheResponse) {
179 1
                $this->cache->set($request, $path, $response);
180 1
            }
181 2
        }
182
183 3
        return $response;
184
    }
185
186
    /**
187
     * @param string $request
188
     * @param array $params
189
     * @param bool $force
190
     *
191
     * @return Crawler
192
     */
193 4
    public function getCrawler($request, array $params = [], $force = false)
194
    {
195 4
        return new Crawler($this->getContent($request, $params, $force));
196
    }
197
198
    /**
199
     * @param string $image
200
     *
201
     * @return string
202
     */
203 1
    public function getImageUrl($image)
204
    {
205 1
        return $this->image_prefix.$image;
206
    }
207
}
208