Completed
Push — master ( 675678...95511f )
by Peter
06:26 queued 22s
created

Browser::getApiHost()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 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\ShikimoriBrowserBundle\Service;
10
11
use Guzzle\Http\Client;
12
13
/**
14
 * Browser.
15
 *
16
 * @link http://shikimori.org/
17
 * @link http://shikimori.org/api/doc
18
 */
19
class Browser
20
{
21
    /**
22
     * @var string
23
     */
24
    private $host;
25
26
    /**
27
     * @var string
28
     */
29
    private $api_prefix;
30
31
    /**
32
     * @var Client
33
     */
34
    private $client;
35
36
    /**
37
     * @param Client $client
38
     * @param string $host
39
     * @param string $api_prefix
40
     */
41 7
    public function __construct(Client $client, $host, $api_prefix)
42
    {
43 7
        $this->client = $client;
44 7
        $this->host = $host;
45 7
        $this->api_prefix = $api_prefix;
46 7
    }
47
48
    /**
49
     * @return string
50
     */
51 1
    public function getHost()
52
    {
53 1
        return $this->host;
54
    }
55
56
    /**
57
     * @return string
58
     */
59 1
    public function getApiHost()
60
    {
61 1
        return $this->client->getBaseUrl();
62
    }
63
64
    /**
65
     * @param int $timeout
66
     *
67
     * @return Browser
68
     */
69 1
    public function setTimeout($timeout)
70
    {
71 1
        $this->client->setDefaultOption('timeout', $timeout);
72
73 1
        return $this;
74
    }
75
76
    /**
77
     * @param int $proxy
78
     *
79
     * @return Browser
80
     */
81 1
    public function setProxy($proxy)
82
    {
83 1
        $this->client->setDefaultOption('proxy', $proxy);
84
85 1
        return $this;
86
    }
87
88
    /**
89
     * Get data from path.
90
     *
91
     * @param string $path
92
     *
93
     * @return array
94
     */
95 3
    public function get($path)
96
    {
97 3
        $response = $this->client->get($this->api_prefix.$path)->send();
98 3
        if ($response->isError()) {
99 1
            throw new \RuntimeException('Failed to query the server '.$this->client->getBaseUrl());
100
        }
101
102 2
        $body = json_decode($response->getBody(true), true);
103
104 2
        if (json_last_error() !== JSON_ERROR_NONE || !is_array($body)) {
105 1
            throw new \RuntimeException('Invalid response from the server '.$this->client->getBaseUrl());
106
        }
107
108 1
        return $body;
109
    }
110
}
111