1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MedianetDev\PConnector\Concerns; |
4
|
|
|
|
5
|
|
|
trait Utils |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Load the response from http client to PConnector. |
9
|
|
|
*/ |
10
|
|
|
private function loadResponse($data) |
11
|
|
|
{ |
12
|
|
|
$this->status = $data['status']; |
|
|
|
|
13
|
|
|
$this->request = $data['request']; |
|
|
|
|
14
|
|
|
$this->errorMessage = $this->status ? null : $data['error_message']; |
|
|
|
|
15
|
|
|
|
16
|
|
|
$this->response = $data['response']; |
|
|
|
|
17
|
|
|
switch ($this->decodeResponse) { |
|
|
|
|
18
|
|
|
case 'object': |
19
|
|
|
$this->response['body'] = json_decode($data['response']['body']); |
20
|
|
|
break; |
21
|
|
|
case 'array': |
22
|
|
|
$this->response['body'] = json_decode($data['response']['body'], true); |
23
|
|
|
break; |
24
|
|
|
default: |
25
|
|
|
break; |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Log the request & response data to the log files. |
31
|
|
|
* |
32
|
|
|
* @return void |
33
|
|
|
*/ |
34
|
|
|
public function log() |
35
|
|
|
{ |
36
|
|
|
app('log')->debug( |
37
|
|
|
'[PConnector: '.$this->profile.'] '. |
|
|
|
|
38
|
|
|
"\n------------------- gateway request --------------------". |
39
|
|
|
"\n#Url: ".$this->request['url']. |
40
|
|
|
"\n#Method: ".$this->request['method']. |
41
|
|
|
"\n#Data: ".json_encode($this->request['payload']). |
42
|
|
|
"\n------------------- gateway response -------------------". |
43
|
|
|
"\n#Status code: ".$this->response['status_code']. |
44
|
|
|
"\n#Headers: ".json_encode($this->response['headers']). |
45
|
|
|
"\n#Body: ".(is_string($this->response['body']) ? $this->response['body'] : json_encode($this->response['body'])). |
46
|
|
|
"\n#Error: ".($this->errorMessage ? $this->errorMessage : 'none'). |
47
|
|
|
"\n--------------------------------------------------------" |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Log the request & response data to the log files if response code is equal to. |
53
|
|
|
* |
54
|
|
|
* @param int $responseCode |
55
|
|
|
* |
56
|
|
|
* @return void |
57
|
|
|
*/ |
58
|
|
|
public function logIfResponseCodeNot($responseCode) |
59
|
|
|
{ |
60
|
|
|
if ($this->responseCodeNot($responseCode)) { |
61
|
|
|
$this->log(); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Dump the \MedianetDev\PConnector\PConnector using laravel dump function. |
67
|
|
|
* |
68
|
|
|
* @return \MedianetDev\PConnector\PConnector |
69
|
|
|
*/ |
70
|
|
|
public function dump() |
71
|
|
|
{ |
72
|
|
|
dump($this); |
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Dump and die the \MedianetDev\PConnector\PConnector using laravel dd function. |
79
|
|
|
*/ |
80
|
|
|
public function dd() |
81
|
|
|
{ |
82
|
|
|
dd($this); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get attribute from the response. |
87
|
|
|
* |
88
|
|
|
* @param string $attribute The attribute path, for nested attributes use the "." separator [EX: "profile.name"] |
89
|
|
|
* @param mixed $default The fallback value if the attribute is not on the response object |
90
|
|
|
* |
91
|
|
|
* @return mixed |
92
|
|
|
*/ |
93
|
|
|
public function getAttribute($attribute, $default = null) |
94
|
|
|
{ |
95
|
|
|
if (0 === $this->status) { |
96
|
|
|
return $default; |
97
|
|
|
} |
98
|
|
|
if ('object' !== gettype($this->response['body'])) { |
99
|
|
|
throw new \BadMethodCallException('You can use the get() function only if you are parsing the response as object, your response body type is: '.gettype($this->response['body']).'. You can set in the config with the "decode_response" key.'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return _get($this->response['body'], explode('.', $attribute), $default); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Check if the response code is equal to. |
107
|
|
|
* |
108
|
|
|
* @return bool |
109
|
|
|
*/ |
110
|
|
|
public function responseCodeIs(int $code) |
111
|
|
|
{ |
112
|
|
|
return $this->response['status_code'] === $code; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Check if the response code is not equal to. |
117
|
|
|
* |
118
|
|
|
* @return bool |
119
|
|
|
*/ |
120
|
|
|
public function responseCodeNot(int $code) |
121
|
|
|
{ |
122
|
|
|
return ! $this->responseCodeNot($code); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Check if the response code is in the given array. |
127
|
|
|
* |
128
|
|
|
* @return bool |
129
|
|
|
*/ |
130
|
|
|
public function responseCodeIn(array $code) |
131
|
|
|
{ |
132
|
|
|
return in_array($this->response['status_code'], $code); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Check if the response code is not in the given array. |
137
|
|
|
* |
138
|
|
|
* @return bool |
139
|
|
|
*/ |
140
|
|
|
public function responseCodeNotIn(array $code) |
141
|
|
|
{ |
142
|
|
|
return ! $this->responseCodeIn($code); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Check if the response code is in [200, 201, 202, 204] range. |
147
|
|
|
* |
148
|
|
|
* @return bool |
149
|
|
|
*/ |
150
|
|
|
public function responseOK() |
151
|
|
|
{ |
152
|
|
|
return $this->responseCodeIn([200, 201, 202, 204]); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Check if the response code is not in [200, 201, 202, 204] range. |
157
|
|
|
* |
158
|
|
|
* @return bool |
159
|
|
|
*/ |
160
|
|
|
public function responseNOK() |
161
|
|
|
{ |
162
|
|
|
return ! $this->responseOK(); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function __get($name) |
166
|
|
|
{ |
167
|
|
|
return $this->status ? $this->response['body']->{$name} : null; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|