Completed
Push — 2.0 ( a647f0...05c7c7 )
by Peter
04:17
created

Browser::setProxy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * AnimeDb package.
5
 *
6
 * @author    Peter Gribanov <[email protected]>
7
 * @copyright Copyright (c) 2011, Peter Gribanov
8
 * @license   http://opensource.org/licenses/GPL-3.0 GPL v3
9
 */
10
11
namespace AnimeDb\Bundle\ShikimoriBrowserBundle\Service;
12
13
use AnimeDb\Bundle\ShikimoriBrowserBundle\Service\Exception\ResponseException;
14
use GuzzleHttp\Client;
15
16
/**
17
 * Browser.
18
 *
19
 * @see http://shikimori.org/
20
 * @see http://shikimori.org/api/doc
21
 */
22
class Browser
23
{
24
    /**
25
     * @var string
26
     */
27
    private $host;
28
29
    /**
30
     * @var string
31
     */
32
    private $api_host;
33
34
    /**
35
     * @var string
36
     */
37
    private $api_prefix;
38
39
    /**
40
     * @var Client
41
     */
42
    private $client;
43
44
    /**
45
     * @param Client $client
46
     * @param string $host
47
     * @param string $api_host
48
     * @param string $api_prefix
49
     */
50 5
    public function __construct(Client $client, $host, $api_host, $api_prefix)
51
    {
52 5
        $this->client = $client;
53 5
        $this->host = $host;
54 5
        $this->api_host = $api_host;
55 5
        $this->api_prefix = $api_prefix;
56 5
    }
57
58
    /**
59
     * @return string
60
     */
61 1
    public function getHost()
62
    {
63 1
        return $this->host;
64
    }
65
66
    /**
67
     * @return string
68
     */
69 1
    public function getApiHost()
70
    {
71 1
        return $this->api_host;
72
    }
73
74
    /**
75
     * Get data from path.
76
     *
77
     * @param string $path
78
     *
79
     * @return array
80
     */
81 3
    public function get($path)
82
    {
83 3
        $response = $this->client->request('GET', $this->api_host.$this->api_prefix.$path);
84
85 3
        if ($response->isError()) {
0 ignored issues
show
Bug introduced by
The method isError() does not seem to exist on object<Psr\Http\Message\ResponseInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
86 1
            throw ResponseException::failed($this->api_host);
87
        }
88
89 2
        $body = json_decode($response->getBody()->getContents(), true);
90
91 2
        if (json_last_error() !== JSON_ERROR_NONE || !is_array($body)) {
92 1
            throw ResponseException::invalidResponse($this->api_host);
93
        }
94
95 1
        return $body;
96
    }
97
}
98