Completed
Push — master ( c98198...f64bec )
by Peter
03:24
created

Browser   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 186
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 22
Bugs 3 Features 5
Metric Value
wmc 17
c 22
b 3
f 5
lcom 2
cbo 6
dl 0
loc 186
ccs 44
cts 44
cp 1
rs 10

10 Methods

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