|
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
|
|
|
if ($this->status) { |
|
17
|
|
|
$this->response = $data['response']; |
|
|
|
|
|
|
18
|
|
|
switch ($this->decodeResponse) { |
|
|
|
|
|
|
19
|
|
|
case 'object': |
|
20
|
|
|
$this->response['body'] = json_decode($data['response']['body']); |
|
21
|
|
|
break; |
|
22
|
|
|
case 'array': |
|
23
|
|
|
$this->response['body'] = json_decode($data['response']['body'], true); |
|
24
|
|
|
break; |
|
25
|
|
|
|
|
26
|
|
|
default: |
|
27
|
|
|
break; |
|
28
|
|
|
} |
|
29
|
|
|
} |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Log the request & response data to the log files. |
|
34
|
|
|
* |
|
35
|
|
|
* @return void |
|
36
|
|
|
*/ |
|
37
|
|
|
public function log() |
|
38
|
|
|
{ |
|
39
|
|
|
app('log')->debug( |
|
40
|
|
|
'[PConnector: '.$this->profile.'] '. |
|
|
|
|
|
|
41
|
|
|
"\n------------------- gateway request --------------------". |
|
42
|
|
|
"\n#Url: ".$this->request['url']. |
|
43
|
|
|
"\n#Method: ".$this->request['method']. |
|
44
|
|
|
"\n#Data: ".json_encode($this->request['payload']). |
|
45
|
|
|
"\n------------------- gateway response -------------------". |
|
46
|
|
|
"\n#Status code: ".$this->response['status_code']. |
|
47
|
|
|
"\n#Headers: ".json_encode($this->response['headers']). |
|
48
|
|
|
"\n#Body: ".(is_string($this->response['body']) ? $this->response['body'] : json_encode($this->response['body'])). |
|
49
|
|
|
"\n--------------------------------------------------------" |
|
50
|
|
|
); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Log the request & response data to the log files if response code is equal to. |
|
55
|
|
|
* |
|
56
|
|
|
* @param int $responseCode |
|
57
|
|
|
* |
|
58
|
|
|
* @return void |
|
59
|
|
|
*/ |
|
60
|
|
|
public function logIfResponseCodeNot($responseCode) |
|
61
|
|
|
{ |
|
62
|
|
|
if ($this->responseCodeNot($responseCode)) { |
|
63
|
|
|
app('log')->debug( |
|
64
|
|
|
'[PConnector: '.$this->profile.'] '. |
|
|
|
|
|
|
65
|
|
|
"\n------------------- gateway request --------------------". |
|
66
|
|
|
"\n#Url: ".$this->request['url']. |
|
67
|
|
|
"\n#Method: ".$this->request['method']. |
|
68
|
|
|
"\n#Data: ".json_encode($this->request['payload']). |
|
69
|
|
|
"\n------------------- gateway response -------------------". |
|
70
|
|
|
"\n#Status code: ".$this->response['status_code']. |
|
71
|
|
|
"\n#Headers: ".json_encode($this->response['headers']). |
|
72
|
|
|
"\n#Body: ".(is_string($this->response['body']) ? $this->response['body'] : json_encode($this->response['body'])). |
|
73
|
|
|
"\n--------------------------------------------------------" |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Dump the \MedianetDev\PConnector\PConnector using laravel dump function. |
|
80
|
|
|
* |
|
81
|
|
|
* @return \MedianetDev\PConnector\PConnector |
|
82
|
|
|
*/ |
|
83
|
|
|
public function dump() |
|
84
|
|
|
{ |
|
85
|
|
|
dump($this); |
|
86
|
|
|
|
|
87
|
|
|
return $this; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Get attribute from the response. |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $attribute The attribute path, for nested attributes use the "." separator [EX: "profile.name"] |
|
94
|
|
|
* @param mixed $default The fallback value if the attribute is not on the response object |
|
95
|
|
|
* |
|
96
|
|
|
* @return mixed |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getAttribute($attribute, $default = null) |
|
99
|
|
|
{ |
|
100
|
|
|
if ('object' !== gettype($this->response['body'])) { |
|
101
|
|
|
throw new \BadMethodCallException( |
|
102
|
|
|
'You can use the get() function only if you are parsing the response as object, your response body type is: '. |
|
103
|
|
|
gettype($this->response['body']). |
|
104
|
|
|
'. You can set in the config with the "decode_response" key.' |
|
105
|
|
|
); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return _get($this->response['body'], explode('.', $attribute), $default); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Check if the response code is not equal to. |
|
113
|
|
|
* |
|
114
|
|
|
* @return bool |
|
115
|
|
|
*/ |
|
116
|
|
|
public function responseCodeNot(int $code) |
|
117
|
|
|
{ |
|
118
|
|
|
return $this->response['status_code'] !== $code; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Check if the response code is not in. |
|
123
|
|
|
* |
|
124
|
|
|
* @return bool |
|
125
|
|
|
*/ |
|
126
|
|
|
public function responseCodeNotIn(array $code) |
|
127
|
|
|
{ |
|
128
|
|
|
return ! in_array($this->response['status_code'], $code); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
public function __get($name) |
|
132
|
|
|
{ |
|
133
|
|
|
return $this->response['body']->{$name}; |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|