1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rs\VersionEye\Http; |
4
|
|
|
|
5
|
|
|
use Http\Client\Common\HttpMethodsClient; |
6
|
|
|
use Http\Client\Exception\HttpException as PlugException; |
7
|
|
|
use Http\Client\HttpClient as PlugClient; |
8
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
9
|
|
|
use Http\Discovery\StreamFactoryDiscovery; |
10
|
|
|
use Http\Message\MultipartStream\MultipartStreamBuilder; |
11
|
|
|
use Psr\Http\Message\RequestInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* HttpPlugHttpAdapterClient. |
15
|
|
|
* |
16
|
|
|
* @author Robert Schönthal <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class HttpPlugHttpAdapterClient implements HttpClient |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var HttpMethodsClient |
22
|
|
|
*/ |
23
|
|
|
private $adapter; |
24
|
|
|
|
25
|
|
|
private $url; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param PlugClient $adapter |
29
|
|
|
* @param string $url |
30
|
|
|
*/ |
31
|
|
|
public function __construct(PlugClient $adapter, $url) |
32
|
|
|
{ |
33
|
|
|
$this->adapter = $adapter; |
|
|
|
|
34
|
|
|
$this->url = $url; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
|
|
public function request($method, $path, array $params = []) |
41
|
|
|
{ |
42
|
|
|
list($params, $files) = $this->splitParams($params); |
43
|
|
|
$url = $this->url . $path; |
44
|
|
|
|
45
|
|
|
try { |
46
|
|
|
$request = $this->createBody($params, $files, $method, $url); |
47
|
|
|
$response = $request ? $this->adapter->sendRequest($request) : $this->adapter->send($method, $url); |
48
|
|
|
|
49
|
|
|
return json_decode($response->getBody(), true); |
50
|
|
|
} catch (PlugException $e) { |
51
|
|
|
throw $this->buildRequestError($e); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* splits arguments into parameters and files (if any). |
57
|
|
|
* |
58
|
|
|
* @param array $params |
59
|
|
|
* |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
|
|
private function splitParams(array $params) |
63
|
|
|
{ |
64
|
|
|
$parameters = []; |
65
|
|
|
$files = []; |
66
|
|
|
|
67
|
|
|
foreach ($params as $name => $value) { |
68
|
|
|
if (is_readable($value)) { //file |
69
|
|
|
$files[$name] = $value; |
70
|
|
|
} else { |
71
|
|
|
$parameters[$name] = $value; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return [$parameters, $files]; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* builds the error exception. |
80
|
|
|
* |
81
|
|
|
* @param PlugException $e |
82
|
|
|
* |
83
|
|
|
* @return CommunicationException |
84
|
|
|
*/ |
85
|
|
|
private function buildRequestError(PlugException $e) |
86
|
|
|
{ |
87
|
|
|
$data = $e->getResponse() ? json_decode($e->getResponse()->getBody(), true) : ['error' => $e->getMessage()]; |
88
|
|
|
$message = isset($data['error']) ? $data['error'] : 'Server Error'; |
89
|
|
|
$status = $e->getResponse() ? $e->getResponse()->getStatusCode() : 500; |
90
|
|
|
|
91
|
|
|
return new CommunicationException(sprintf('%s : %s', $status, $message)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param array $params |
96
|
|
|
* @param array $files |
97
|
|
|
* @param string $method |
98
|
|
|
* @param string $url |
99
|
|
|
* |
100
|
|
|
* @return null|RequestInterface |
101
|
|
|
*/ |
102
|
|
|
private function createBody(array $params, array $files, $method, $url) |
103
|
|
|
{ |
104
|
|
|
if (!count($params) && !count($files)) { |
105
|
|
|
return; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$streamFactory = StreamFactoryDiscovery::find(); |
109
|
|
|
$builder = new MultipartStreamBuilder($streamFactory); |
110
|
|
|
|
111
|
|
|
foreach ($params as $k => $v) { |
112
|
|
|
$builder->addResource($k, $v); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
foreach ($files as $k => $file) { |
116
|
|
|
$builder->addResource($k, fopen($file, 'r'), ['filename' => $file]); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return MessageFactoryDiscovery::find()->createRequest( |
120
|
|
|
$method, |
121
|
|
|
$url, |
122
|
|
|
['Content-Type' => 'multipart/form-data; boundary=' . $builder->getBoundary()], |
123
|
|
|
$builder->build() |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.