|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Raidros\Storer; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
|
6
|
|
|
|
|
7
|
|
|
class Executor |
|
8
|
|
|
{ |
|
9
|
|
|
protected $api; |
|
10
|
|
|
protected $endpoint; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Setup a api endpoint execution. |
|
14
|
|
|
* |
|
15
|
|
|
* @param Raidros\Storer\Api $api |
|
16
|
|
|
*/ |
|
17
|
15 |
|
public function __construct(Api $api) |
|
18
|
|
|
{ |
|
19
|
15 |
|
$this->api = $api; |
|
20
|
15 |
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* call a api endpoint. |
|
24
|
|
|
* |
|
25
|
|
|
* @param string $pointName |
|
26
|
|
|
* @param array $data |
|
27
|
|
|
* @param array $headers |
|
28
|
|
|
* |
|
29
|
|
|
* @return Raidros\Storer\Response |
|
30
|
|
|
*/ |
|
31
|
15 |
|
public function run($pointName, array $data = [], array $headers = []) |
|
32
|
|
|
{ |
|
33
|
15 |
|
$this->endpoint = $this->api->getEndpoints()->findOrFail($pointName); |
|
34
|
|
|
|
|
35
|
15 |
|
$client = new Client($this->api->getHandler()); |
|
36
|
15 |
|
$method = $this->endpoint->getMethod(); |
|
37
|
15 |
|
$uri = $this->endpoint->getUri($data); |
|
38
|
15 |
|
$body = $this->makeBody($headers, $data); |
|
39
|
15 |
|
$response = $client->request($method, $uri, $body); |
|
40
|
|
|
|
|
41
|
15 |
|
return new Response($response, $this->endpoint->getTransformer()); |
|
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* make the request body. |
|
46
|
|
|
* |
|
47
|
|
|
* @param array $pointHeaders |
|
48
|
|
|
* @param array $data |
|
49
|
|
|
* |
|
50
|
|
|
* @return array |
|
51
|
|
|
*/ |
|
52
|
15 |
|
protected function makeBody($pointHeaders, $data) |
|
53
|
|
|
{ |
|
54
|
15 |
|
unset($data['uriData']); |
|
55
|
15 |
|
$apiHeaders = array_map(function ($value) { |
|
56
|
12 |
|
return $value->get(); |
|
57
|
15 |
|
}, $this->api->getHeaders()->get()); |
|
58
|
|
|
|
|
59
|
15 |
|
$dataChain = $this->endpoint->getMethod() == 'GET' ? 'query' : 'json'; |
|
60
|
15 |
|
$body = array_merge($apiHeaders, $pointHeaders); |
|
61
|
15 |
|
$body[$dataChain] = $data; |
|
62
|
|
|
|
|
63
|
15 |
|
$request = new Request($body, $this->endpoint->getTransformer('request'), $dataChain); |
|
|
|
|
|
|
64
|
|
|
|
|
65
|
15 |
|
return $request->getBody($dataChain); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.