1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Phraseanet SDK. |
5
|
|
|
* |
6
|
|
|
* (c) Alchemy <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace PhraseanetSDK\Http; |
13
|
|
|
|
14
|
|
|
use Guzzle\Common\Exception\GuzzleException; |
15
|
|
|
use Guzzle\Http\Client as Guzzle; |
16
|
|
|
use Guzzle\Http\ClientInterface; |
17
|
|
|
use Guzzle\Http\Exception\BadResponseException as GuzzleBadResponse; |
18
|
|
|
use Guzzle\Http\Exception\CurlException; |
19
|
|
|
use Guzzle\Http\Message\EntityEnclosingRequestInterface; |
20
|
|
|
use Guzzle\Http\Message\RequestInterface; |
21
|
|
|
use PhraseanetSDK\ApplicationInterface; |
22
|
|
|
use PhraseanetSDK\Exception\BadResponseException; |
23
|
|
|
use PhraseanetSDK\Exception\InvalidArgumentException; |
24
|
|
|
use PhraseanetSDK\Exception\RuntimeException; |
25
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
26
|
|
|
|
27
|
|
|
class GuzzleAdapter implements GuzzleAdapterInterface |
28
|
|
|
{ |
29
|
|
|
/** @var ClientInterface */ |
30
|
|
|
private $guzzle; |
31
|
|
|
private $extended = false; |
32
|
|
|
|
33
|
89 |
|
public function __construct(ClientInterface $guzzle) |
34
|
|
|
{ |
35
|
89 |
|
$this->guzzle = $guzzle; |
36
|
89 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
* |
41
|
|
|
* @return ClientInterface |
42
|
|
|
*/ |
43
|
2 |
|
public function getGuzzle() |
44
|
|
|
{ |
45
|
2 |
|
return $this->guzzle; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Returns the client base URL |
50
|
|
|
* |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
2 |
|
public function getBaseUrl() |
54
|
|
|
{ |
55
|
2 |
|
return $this->guzzle->getBaseUrl(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Sets the user agent |
60
|
|
|
* |
61
|
|
|
* @param string $userAgent |
62
|
|
|
*/ |
63
|
1 |
|
public function setUserAgent($userAgent) |
64
|
|
|
{ |
65
|
1 |
|
$this->guzzle->setUserAgent($userAgent); |
66
|
1 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Sets extended mode |
70
|
|
|
* |
71
|
|
|
* Extended mode fetch more data (status, meta, subdefs) in one request |
72
|
|
|
* for a record |
73
|
|
|
* |
74
|
|
|
* @param boolean $extended |
75
|
|
|
*/ |
76
|
1 |
|
public function setExtended($extended) |
77
|
|
|
{ |
78
|
|
|
$this->extended = (boolean)$extended; |
79
|
1 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return boolean |
83
|
|
|
*/ |
84
|
|
|
public function isExtended() |
85
|
|
|
{ |
86
|
|
|
return $this->extended; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Performs an HTTP request, returns the body response |
91
|
|
|
* |
92
|
|
|
* @param string $method The method |
93
|
|
|
* @param string $path The path to query |
94
|
|
|
* @param array $query An array of query parameters |
95
|
|
|
* @param array $postFields An array of post fields |
96
|
|
|
* @param array $files An array of post files |
97
|
|
|
* @param array $headers An array of request headers |
98
|
|
|
* |
99
|
|
|
* @return string The response body |
100
|
|
|
* |
101
|
|
|
* @throws BadResponseException |
102
|
|
|
* @throws RuntimeException |
103
|
|
|
*/ |
104
|
82 |
|
public function call( |
105
|
|
|
$method, |
106
|
|
|
$path, |
107
|
|
|
array $query = array(), |
108
|
|
|
array $postFields = array(), |
109
|
|
|
array $files = array(), |
110
|
|
|
array $headers = array() |
111
|
|
|
) { |
112
|
|
|
try { |
113
|
|
|
$acceptHeader = array( |
114
|
82 |
|
'Accept' => $this->extended ? 'application/vnd.phraseanet.record-extended+json' : 'application/json' |
115
|
82 |
|
); |
116
|
|
|
|
117
|
82 |
|
$request = $this->guzzle->createRequest($method, $path, array_merge($acceptHeader, $headers)); |
118
|
82 |
|
$this->addRequestParameters($request, $query, $postFields, $files); |
119
|
81 |
|
$response = $request->send(); |
120
|
82 |
|
} catch (CurlException $e) { |
121
|
2 |
|
throw new RuntimeException($e->getMessage(), $e->getErrorNo(), $e); |
122
|
17 |
|
} catch (GuzzleBadResponse $e) { |
123
|
15 |
|
throw BadResponseException::fromGuzzleResponse($e); |
124
|
2 |
|
} catch (GuzzleException $e) { |
125
|
1 |
|
throw new RuntimeException($e->getMessage(), $e->getCode(), $e); |
126
|
|
|
} |
127
|
|
|
|
128
|
63 |
|
return $response->getBody(true); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Creates a new instance of GuzzleAdapter |
133
|
|
|
* |
134
|
|
|
* @param string $endpoint |
135
|
|
|
* @param EventSubscriberInterface[] $plugins |
136
|
|
|
* @param int $endpointVersion |
|
|
|
|
137
|
|
|
* @return static |
138
|
|
|
*/ |
139
|
6 |
|
public static function create( |
140
|
|
|
$endpoint, |
141
|
|
|
array $plugins = array() |
142
|
|
|
) { |
143
|
6 |
|
if (!is_string($endpoint)) { |
144
|
|
|
throw new InvalidArgumentException('API url endpoint must be a valid url'); |
145
|
|
|
} |
146
|
|
|
|
147
|
6 |
|
$versionMountPoint = ApplicationInterface::API_MOUNT_POINT; |
148
|
|
|
|
149
|
|
|
// test if url already end with API_MOUNT_POINT |
150
|
6 |
|
$mountPoint = substr(trim($endpoint, '/'), -strlen($versionMountPoint)); |
151
|
|
|
|
152
|
6 |
|
if ($versionMountPoint !== $mountPoint) { |
153
|
6 |
|
$endpoint = sprintf('%s%s/', trim($endpoint, '/'), $versionMountPoint); |
154
|
6 |
|
} |
155
|
|
|
|
156
|
6 |
|
$guzzle = new Guzzle($endpoint); |
157
|
6 |
|
$guzzle->setUserAgent(sprintf( |
158
|
6 |
|
'%s version %s', |
159
|
6 |
|
ApplicationInterface::USER_AGENT, |
160
|
|
|
ApplicationInterface::VERSION |
161
|
6 |
|
)); |
162
|
|
|
|
163
|
6 |
|
foreach ($plugins as $plugin) { |
164
|
3 |
|
$guzzle->addSubscriber($plugin); |
165
|
6 |
|
} |
166
|
|
|
|
167
|
6 |
|
return new static($guzzle); |
168
|
|
|
} |
169
|
|
|
|
170
|
82 |
|
private function addRequestParameters(RequestInterface $request, $query, $postFields, $files) |
171
|
|
|
{ |
172
|
82 |
|
foreach ($query as $name => $value) { |
173
|
19 |
|
$request->getQuery()->add($name, $value); |
174
|
82 |
|
} |
175
|
|
|
|
176
|
82 |
|
if ($request instanceof EntityEnclosingRequestInterface) { |
177
|
13 |
|
if ($request->getHeader('Content-Type') == 'application/json') { |
178
|
|
|
$request->getHeaders()->offsetUnset('Content-Type'); |
179
|
|
|
$request->setBody(json_encode($postFields)); |
180
|
|
|
|
181
|
|
|
return; |
182
|
|
|
} |
183
|
|
|
|
184
|
13 |
|
foreach ($postFields as $name => $value) { |
185
|
10 |
|
$request->getPostFields()->add($name, $value); |
186
|
14 |
|
} |
187
|
61 |
|
foreach ($files as $name => $filename) { |
188
|
3 |
|
$request->addPostFile($name, $filename); |
189
|
13 |
|
} |
190
|
82 |
|
} elseif (0 < count($postFields)) { |
191
|
1 |
|
throw new InvalidArgumentException('Can not add post fields to GET request'); |
192
|
|
|
} |
193
|
81 |
|
} |
194
|
|
|
} |
195
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$ireland
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was changed, but the annotation was not.