Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
25.00% |
1 / 4 |
CRAP | |
55.56% |
15 / 27 |
Response | |
0.00% |
0 / 1 |
|
25.00% |
1 / 4 |
24.64 | |
55.56% |
15 / 27 |
__construct | |
0.00% |
0 / 1 |
3.14 | |
75.00% |
9 / 12 |
|||
__get | |
100.00% |
1 / 1 |
2 | |
100.00% |
3 / 3 |
|||
__set | |
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 3 |
|||
__call | |
0.00% |
0 / 1 |
12.41 | |
33.33% |
3 / 9 |
1 | <?php |
2 | |
3 | namespace Qmp\Laravel\MicroService\Client\Tools; |
4 | |
5 | use Illuminate\Support\Facades\Log; |
6 | use Psr\Http\Message\ResponseInterface; |
7 | use Qmp\Laravel\MicroService\Client\Exceptions\ClientException; |
8 | |
9 | class Response |
10 | { |
11 | /** |
12 | * @var array elements response |
13 | */ |
14 | private $response = [ |
15 | 'code' => null, |
16 | 'reason' => null, |
17 | 'body' => null, |
18 | 'content' => [] |
19 | ]; |
20 | |
21 | /** |
22 | * Response constructor. |
23 | * @param ResponseInterface $response |
24 | * @throws ClientException |
25 | */ |
26 | public function __construct(ResponseInterface $response) |
27 | { |
28 | try { |
29 | $this->response = [ |
30 | 'code' => $response->getStatusCode(), |
31 | 'reason' => $response->getReasonPhrase(), |
32 | 'body' => $response->getBody()->getContents(), |
33 | 'headers' => $response->getHeaders() |
34 | ]; |
35 | $this->response['content'] = (!empty($this->response['body'])) |
36 | ? \GuzzleHttp\json_decode($this->response['body'], true) |
37 | : []; |
38 | } catch (\InvalidArgumentException $e) { |
39 | //var_dump($this->body, $e->getMessage());die; |
40 | Log::debug('error on micro service call:' . var_export(['error' => $e->getMessage(), 'body' => $this->response['body']], true)); |
41 | //echo $this->body;die; |
42 | throw new ClientException($e->getMessage(), 30); |
43 | } |
44 | } |
45 | |
46 | /** |
47 | * @param string $property |
48 | * @return mixed|null |
49 | */ |
50 | public function __get($property) |
51 | { |
52 | if (isset($this->response[$property])) { |
53 | return $this->response[$property]; |
54 | } |
55 | |
56 | return null; |
57 | } |
58 | |
59 | /** |
60 | * @param string $property |
61 | * @param $value |
62 | */ |
63 | public function __set($property, $value) |
64 | { |
65 | if (isset($this->response[$property])) { |
66 | $this->response[$property] = $value; |
67 | } |
68 | } |
69 | |
70 | /** |
71 | * @param string $method |
72 | * @param array $arguments |
73 | * @return mixed|bool |
74 | * @throws ClientException |
75 | */ |
76 | public function __call($method, array $arguments) |
77 | { |
78 | $field = reset($arguments); |
79 | |
80 | if (!in_array($field, array_keys($this->response['content']))) { |
81 | throw new ClientException('Field ' . $field . ' not exist', 31); |
82 | } |
83 | |
84 | switch ($method) { |
85 | case 'get': |
86 | return $this->response['content'][$field]; |
87 | case 'has': |
88 | return $this->response['content'][$field] !== null; |
89 | |
90 | default: |
91 | throw new ClientException('Method ' . $method .' is not allowed', 32); |
92 | } |
93 | } |
94 | } |