1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the PayBreak/basket package. |
4
|
|
|
* |
5
|
|
|
* (c) PayBreak <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace PayBreak\Sdk\Gateways; |
12
|
|
|
|
13
|
|
|
use PayBreak\Sdk\ApiClient\ApiClientFactoryInterface; |
14
|
|
|
use Psr\Log\LoggerInterface; |
15
|
|
|
use WNowicki\Generic\Logger\PsrLoggerTrait; |
16
|
|
|
use WNowicki\Generic\Exception; |
17
|
|
|
use PayBreak\ApiClient\ErrorResponseException; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Abstract Gateway |
21
|
|
|
* |
22
|
|
|
* @author WN |
23
|
|
|
* @package PayBreak\Sdk\Gateways |
24
|
|
|
*/ |
25
|
|
|
abstract class AbstractGateway |
26
|
|
|
{ |
27
|
|
|
use PsrLoggerTrait; |
28
|
|
|
|
29
|
|
|
private $apiFactory; |
30
|
|
|
private $logger; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @author WN |
34
|
|
|
* @param ApiClientFactoryInterface $factory |
35
|
|
|
* @param LoggerInterface $logger |
36
|
|
|
*/ |
37
|
40 |
|
public function __construct(ApiClientFactoryInterface $factory, LoggerInterface $logger = null) |
38
|
|
|
{ |
39
|
40 |
|
$this->apiFactory = $factory; |
40
|
40 |
|
$this->logger = $logger; |
41
|
40 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @author WN |
45
|
|
|
* @return ApiClientFactoryInterface |
46
|
|
|
*/ |
47
|
32 |
|
protected function getApiFactory() |
48
|
|
|
{ |
49
|
32 |
|
return $this->apiFactory; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @author EB |
54
|
|
|
* @param $type |
55
|
|
|
* @param $documentPath |
56
|
|
|
* @param $token |
57
|
|
|
* @param $documentName |
58
|
|
|
* @param array $query |
59
|
|
|
* @param array $body |
60
|
|
|
* @return array |
61
|
|
|
* @throws Exception |
62
|
|
|
*/ |
63
|
27 |
|
private function makeRequestForDocument($type, $documentPath, $token, $documentName, array $query, array $body = []) |
64
|
|
|
{ |
65
|
|
|
try { |
66
|
27 |
|
return $this->makeRequest($type, $documentPath, $token, $query, $body); |
67
|
|
|
|
68
|
4 |
|
} catch (ErrorResponseException $e) { |
69
|
2 |
|
throw new Exception($e->getMessage()); |
70
|
|
|
|
71
|
2 |
|
} catch (\Exception $e) { |
72
|
2 |
|
$this->logError( |
73
|
2 |
|
$documentName . 'Gateway::' . $type .' '. $documentName . '[' . $e->getCode() . ']: ' . $e->getMessage() |
74
|
2 |
|
); |
75
|
2 |
|
throw new Exception('Problem with '.$type. ': ' . $documentName . ' data form Provider API'); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @author EB |
81
|
|
|
* @param $type |
82
|
|
|
* @param $documentPath |
83
|
|
|
* @param $token |
84
|
|
|
* @param array $query |
85
|
|
|
* @param array $body |
86
|
|
|
* @return array |
87
|
|
|
*/ |
88
|
27 |
|
private function makeRequest($type, $documentPath, $token, array $query, array $body = []) |
89
|
|
|
{ |
90
|
27 |
|
$api = $this->getApiFactory()->makeApiClient($token); |
91
|
|
|
|
92
|
|
|
switch($type) { |
93
|
27 |
|
case 'post': |
94
|
5 |
|
return $api->post($documentPath, $body, $query); |
95
|
22 |
|
case 'delete': |
96
|
1 |
|
return $api->delete($documentPath, $query); |
97
|
21 |
|
case 'patch': |
98
|
|
|
return $api->patch($documentPath, $body, $query); |
99
|
21 |
|
default: |
100
|
21 |
|
return $api->get($documentPath, $query); |
101
|
21 |
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @author WN |
106
|
|
|
* @param $documentPath |
107
|
|
|
* @param $token |
108
|
|
|
* @param $documentName |
109
|
|
|
* @param array $query |
110
|
|
|
* @return array |
111
|
|
|
* @throws Exception |
112
|
|
|
*/ |
113
|
21 |
|
protected function fetchDocument($documentPath, $token, $documentName, array $query = []) |
114
|
|
|
{ |
115
|
21 |
|
return $this->makeRequestForDocument('get', $documentPath, $token, $documentName, $query); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @author EB |
120
|
|
|
* @param $documentPath |
121
|
|
|
* @param array $body |
122
|
|
|
* @param $token |
123
|
|
|
* @param $documentName |
124
|
|
|
* @return array |
125
|
|
|
* @throws Exception |
126
|
|
|
*/ |
127
|
5 |
|
protected function postDocument($documentPath, array $body = [], $token, $documentName) |
128
|
|
|
{ |
129
|
5 |
|
return $this->makeRequestForDocument('post', $documentPath, $token, $documentName, [], $body); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @author EB |
134
|
|
|
* @param $documentPath |
135
|
|
|
* @param $token |
136
|
|
|
* @param $documentName |
137
|
|
|
* @param array $body |
138
|
|
|
* @return array |
139
|
|
|
* @throws Exception |
140
|
|
|
*/ |
141
|
|
|
protected function patchDocument($documentPath, $token, $documentName, array $body = []) |
142
|
|
|
{ |
143
|
|
|
return $this->makeRequestForDocument('patch', $documentPath, $token, $documentName, [], $body); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @author LH |
148
|
|
|
* @param $token |
149
|
|
|
* @param $url |
150
|
|
|
* @param $entity |
151
|
|
|
* @return array |
152
|
|
|
* @throws Exception |
153
|
|
|
*/ |
154
|
2 |
|
public function fetchCollection($token, $url, $entity) |
155
|
|
|
{ |
156
|
2 |
|
$api = $this->getApiFactory()->makeApiClient($token); |
157
|
|
|
|
158
|
|
|
try { |
159
|
2 |
|
$data = $api->get($url); |
160
|
2 |
|
$rtn = []; |
161
|
|
|
|
162
|
2 |
|
foreach ($data as $item) { |
163
|
1 |
|
$rtn[] = $entity::make($item); |
164
|
2 |
|
} |
165
|
|
|
|
166
|
2 |
|
return $rtn; |
167
|
|
|
|
168
|
|
|
} catch (ErrorResponseException $e) { |
169
|
|
|
|
170
|
|
|
throw new Exception($e->getMessage()); |
171
|
|
|
|
172
|
|
|
} catch (\Exception $e) { |
173
|
|
|
|
174
|
|
|
$this->logError('Couldn\'t fetch collection of [' . $entity . ']: [' . $e->getCode() . ']: ' . $e->getMessage()); |
175
|
|
|
throw new Exception('Problem fetching collection of [' . $entity . '] form Provider API'); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @author EB |
181
|
|
|
* @param $documentPath |
182
|
|
|
* @param $token |
183
|
|
|
* @param $documentName |
184
|
|
|
* @return array |
185
|
|
|
* @throws Exception |
186
|
|
|
*/ |
187
|
1 |
|
protected function deleteDocument($documentPath, $token, $documentName) |
188
|
|
|
{ |
189
|
1 |
|
return $this->makeRequestForDocument('delete', $documentPath, $token, $documentName, $query = []); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @author WN |
194
|
|
|
* @return \Psr\Log\LoggerInterface|null |
195
|
|
|
*/ |
196
|
4 |
|
protected function getLogger() |
197
|
|
|
{ |
198
|
4 |
|
return $this->logger; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|