|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Rs\VersionEye\Http; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Psr7\LazyOpenStream; |
|
6
|
|
|
use GuzzleHttp\Psr7\MultipartStream; |
|
7
|
|
|
use Http\Client\Common\HttpMethodsClient; |
|
8
|
|
|
use Http\Client\Exception\HttpException as PlugException; |
|
9
|
|
|
use Http\Client\HttpClient as PlugClient; |
|
10
|
|
|
use Http\Discovery\StreamFactoryDiscovery; |
|
11
|
|
|
use Psr\Http\Message\StreamInterface; |
|
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
|
|
|
|
|
44
|
|
|
try { |
|
45
|
|
|
$body = $this->createBody($params, $files); |
|
46
|
|
|
$response = $this->adapter->send($method, $this->url . $path, [], $body); |
|
47
|
|
|
|
|
48
|
|
|
return json_decode($response->getBody(), true); |
|
49
|
|
|
} catch (PlugException $e) { |
|
50
|
|
|
throw $this->buildRequestError($e); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* splits arguments into parameters and files (if any). |
|
56
|
|
|
* |
|
57
|
|
|
* @param array $params |
|
58
|
|
|
* |
|
59
|
|
|
* @return array |
|
60
|
|
|
*/ |
|
61
|
|
|
private function splitParams(array $params) |
|
62
|
|
|
{ |
|
63
|
|
|
$parameters = []; |
|
64
|
|
|
$files = []; |
|
65
|
|
|
|
|
66
|
|
|
foreach ($params as $name => $value) { |
|
67
|
|
|
if (is_readable($value)) { //file |
|
68
|
|
|
$files[$name] = $value; |
|
69
|
|
|
} else { |
|
70
|
|
|
$parameters[$name] = $value; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return [$parameters, $files]; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* builds the error exception. |
|
79
|
|
|
* |
|
80
|
|
|
* @param PlugException $e |
|
81
|
|
|
* |
|
82
|
|
|
* @return CommunicationException |
|
83
|
|
|
*/ |
|
84
|
|
|
private function buildRequestError(PlugException $e) |
|
85
|
|
|
{ |
|
86
|
|
|
$data = $e->getResponse() ? json_decode($e->getResponse()->getBody(), true) : ['error' => $e->getMessage()]; |
|
87
|
|
|
$message = isset($data['error']) ? $data['error'] : 'Server Error'; |
|
88
|
|
|
$status = $e->getResponse() ? $e->getResponse()->getStatusCode() : 500; |
|
89
|
|
|
|
|
90
|
|
|
return new CommunicationException(sprintf('%s : %s', $status, $message)); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param array $params |
|
95
|
|
|
* @param array $files |
|
96
|
|
|
* |
|
97
|
|
|
* @return StreamInterface|null |
|
98
|
|
|
*/ |
|
99
|
|
|
private function createBody(array $params, array $files) |
|
100
|
|
|
{ |
|
101
|
|
|
$streams = []; |
|
102
|
|
|
|
|
103
|
|
|
foreach ($params as $k => $v) { |
|
104
|
|
|
$streams[] = ['name' => $k, 'contents' => $v]; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
foreach ($files as $k => $file) { |
|
108
|
|
|
$streams[] = ['name' => $k, 'contents' => new LazyOpenStream($file, 'r'), 'filename' => $file]; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return count($streams) ? StreamFactoryDiscovery::find()->createStream(new MultipartStream($streams)) : null; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
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.