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