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
|
|
|
class Browser |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private $host; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $prefix; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Client |
30
|
|
|
*/ |
31
|
|
|
private $client; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param Client $client |
35
|
|
|
* @param string $host |
36
|
|
|
* @param string $prefix |
37
|
|
|
*/ |
38
|
15 |
|
public function __construct(Client $client, $host, $prefix) |
39
|
|
|
{ |
40
|
15 |
|
$this->client = $client; |
41
|
15 |
|
$this->host = $host; |
42
|
15 |
|
$this->prefix = $prefix; |
43
|
15 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $resource |
47
|
|
|
* @param array $options |
48
|
|
|
* |
49
|
|
|
* @return array |
50
|
|
|
*/ |
51
|
3 |
|
public function get($resource, array $options = []) |
52
|
|
|
{ |
53
|
3 |
|
return $this->request('GET', $resource, $options); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $resource |
58
|
|
|
* @param array $options |
59
|
|
|
* |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
3 |
|
public function post($resource, array $options = []) |
63
|
|
|
{ |
64
|
3 |
|
return $this->request('POST', $resource, $options); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $resource |
69
|
|
|
* @param array $options |
70
|
|
|
* |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
3 |
|
public function put($resource, array $options = []) |
74
|
|
|
{ |
75
|
3 |
|
return $this->request('PUT', $resource, $options); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $resource |
80
|
|
|
* @param array $options |
81
|
|
|
* |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
3 |
|
public function patch($resource, array $options = []) |
85
|
|
|
{ |
86
|
3 |
|
return $this->request('PATCH', $resource, $options); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param string $resource |
91
|
|
|
* @param array $options |
92
|
|
|
* |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
3 |
|
public function delete($resource, array $options = []) |
96
|
|
|
{ |
97
|
3 |
|
return $this->request('DELETE', $resource, $options); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param string $method |
102
|
|
|
* @param string $path |
103
|
|
|
* @param array $options |
104
|
|
|
* |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
15 |
|
private function request($method, $path = '', array $options = []) |
108
|
|
|
{ |
109
|
|
|
try { |
110
|
15 |
|
$response = $this->client->request($method, $this->host.$this->prefix.$path, $options); |
111
|
5 |
|
} catch (\Exception $e) { |
112
|
5 |
|
throw ResponseException::failed($this->host, $e); |
113
|
|
|
} |
114
|
|
|
|
115
|
10 |
|
$body = json_decode($response->getBody()->getContents(), true); |
116
|
|
|
|
117
|
10 |
|
if (json_last_error() !== JSON_ERROR_NONE || !is_array($body)) { |
118
|
5 |
|
throw ResponseException::invalidResponse($this->host); |
119
|
|
|
} |
120
|
|
|
|
121
|
5 |
|
return $body; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|