1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* TERYT-API |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2017 pudelek.org.pl |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view source file |
8
|
|
|
* that is bundled with this package in the file LICENSE |
9
|
|
|
* |
10
|
|
|
* Author Marcin Pudełek <[email protected]> |
11
|
|
|
* |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
declare (strict_types=1); |
15
|
|
|
|
16
|
|
|
namespace mrcnpdlk\Teryt; |
17
|
|
|
|
18
|
|
|
use mrcnpdlk\Teryt\Exception\Connection; |
19
|
|
|
use mrcnpdlk\Teryt\Exception\Response; |
20
|
|
|
use Psr\Log\LoggerInterface; |
21
|
|
|
use Psr\Log\NullLogger; |
22
|
|
|
use Psr\SimpleCache\CacheInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class Client |
26
|
|
|
* |
27
|
|
|
* @package mrcnpdlk\Teryt |
28
|
|
|
*/ |
29
|
|
|
class Client |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Client instance |
33
|
|
|
* |
34
|
|
|
* @var \mrcnpdlk\Teryt\Client |
35
|
|
|
*/ |
36
|
|
|
protected static $classInstance; |
37
|
|
|
/** |
38
|
|
|
* SoapClient handler |
39
|
|
|
* |
40
|
|
|
* @var \mrcnpdlk\Teryt\TerytSoapClient |
41
|
|
|
*/ |
42
|
|
|
private $soapClient; |
43
|
|
|
/** |
44
|
|
|
* Cache handler |
45
|
|
|
* |
46
|
|
|
* @var CacheInterface |
47
|
|
|
*/ |
48
|
|
|
private $oCache; |
49
|
|
|
/** |
50
|
|
|
* Logger handler |
51
|
|
|
* |
52
|
|
|
* @var LoggerInterface |
53
|
|
|
*/ |
54
|
|
|
private $oLogger; |
55
|
|
|
/** |
56
|
|
|
* Teryt auth configuration |
57
|
|
|
* |
58
|
|
|
* @var array |
59
|
|
|
*/ |
60
|
|
|
private $tTerytConfig = []; |
61
|
|
|
/** |
62
|
|
|
* Default Teryt auth configuration |
63
|
|
|
* |
64
|
|
|
* @var array |
65
|
|
|
*/ |
66
|
|
|
private $tDefTerytConfig |
67
|
|
|
= [ |
68
|
|
|
'url' => 'https://uslugaterytws1test.stat.gov.pl/wsdl/terytws1.wsdl', |
69
|
|
|
'username' => 'TestPubliczny', |
70
|
|
|
'password' => '1234abcd', |
71
|
|
|
]; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Client constructor. |
75
|
|
|
*/ |
76
|
|
|
protected function __construct() |
77
|
|
|
{ |
78
|
|
|
$this->setTerytConfig(); |
79
|
|
|
$this->setLoggerInstance(); |
80
|
|
|
$this->setCacheInstance(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Set Teryt configuration parameters |
85
|
|
|
* |
86
|
|
|
* @param array $tConfig |
87
|
|
|
* |
88
|
|
|
* @return $this |
89
|
|
|
* @throws \mrcnpdlk\Teryt\Exception\Connection |
90
|
|
|
*/ |
91
|
|
|
public function setTerytConfig(array $tConfig = []) |
92
|
|
|
{ |
93
|
|
|
if (empty($tConfig)) { |
94
|
|
|
$tConfig = $this->tDefTerytConfig; |
95
|
|
|
} |
96
|
|
|
$this->tTerytConfig['url'] = $tConfig['url'] ?? 'https://uslugaterytws1.stat.gov.pl/wsdl/terytws1.wsdl'; |
97
|
|
|
$this->tTerytConfig['username'] = $tConfig['username'] ?? null; |
98
|
|
|
$this->tTerytConfig['password'] = $tConfig['password'] ?? null; |
99
|
|
|
|
100
|
|
|
if (!$this->tTerytConfig['username'] || !$this->tTerytConfig['password']) { |
101
|
|
|
throw new Connection(sprintf('Username and password for TERYT WS1 is required')); |
102
|
|
|
} |
103
|
|
|
$this->reinitSoap(); |
104
|
|
|
|
105
|
|
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Reinit Soap Client |
110
|
|
|
* |
111
|
|
|
* @return $this |
112
|
|
|
* @throws Connection |
113
|
|
|
* @throws Exception |
114
|
|
|
*/ |
115
|
|
|
private function reinitSoap() |
116
|
|
|
{ |
117
|
|
|
try { |
118
|
|
|
$this->soapClient = new TerytSoapClient($this->tTerytConfig['url'], [ |
119
|
|
|
'soap_version' => SOAP_1_1, |
120
|
|
|
'exceptions' => true, |
121
|
|
|
'cache_wsdl' => WSDL_CACHE_BOTH, |
122
|
|
|
]); |
123
|
|
|
$this->soapClient->addUserToken($this->tTerytConfig['username'], $this->tTerytConfig['password']); |
124
|
|
|
} catch (\Exception $e) { |
125
|
|
|
throw Helper::handleException($e); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Set Logger handler (PSR-3) |
133
|
|
|
* |
134
|
|
|
* @param LoggerInterface|null $oLogger |
135
|
|
|
* |
136
|
|
|
* @return $this |
137
|
|
|
*/ |
138
|
|
|
public function setLoggerInstance(LoggerInterface $oLogger = null) |
139
|
|
|
{ |
140
|
|
|
$this->oLogger = $oLogger ?: new NullLogger(); |
141
|
|
|
|
142
|
|
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Set Cache handler (PSR-16) |
147
|
|
|
* |
148
|
|
|
* @param CacheInterface|null $oCache |
149
|
|
|
* |
150
|
|
|
* @return \mrcnpdlk\Teryt\Client |
151
|
|
|
* @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-16-simple-cache.md PSR-16 |
152
|
|
|
*/ |
153
|
|
|
public function setCacheInstance(CacheInterface $oCache = null) |
154
|
|
|
{ |
155
|
|
|
$this->oCache = $oCache; |
156
|
|
|
|
157
|
|
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Get Client class instance |
162
|
|
|
* |
163
|
|
|
* @return \mrcnpdlk\Teryt\Client Instancja klasy |
164
|
|
|
* @throws \mrcnpdlk\Teryt\Exception |
165
|
|
|
*/ |
166
|
|
|
public static function getInstance() |
167
|
|
|
{ |
168
|
|
|
if (!static::$classInstance) { |
169
|
|
|
static::$classInstance = new static; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return static::$classInstance; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Making request to Teryt WS1 API |
177
|
|
|
* |
178
|
|
|
* @param string $method Methid name |
179
|
|
|
* @param array $args Parameters |
180
|
|
|
* |
181
|
|
|
* @return mixed |
182
|
|
|
* @throws \mrcnpdlk\Teryt\Exception |
183
|
|
|
* @throws \mrcnpdlk\Teryt\Exception\Connection |
184
|
|
|
* @todo dodac logowanie IN/OUT oraz rozpoznawanie w cache ustawien seriwsu czy prod/test |
185
|
|
|
*/ |
186
|
|
|
public function request(string $method, array $args = []) |
187
|
|
|
{ |
188
|
|
|
try { |
189
|
|
|
if (!array_key_exists('DataStanu', $args)) { |
190
|
|
|
$args['DataStanu'] = (new \DateTime())->format('Y-m-d'); |
191
|
|
|
} |
192
|
|
|
$hashKey = md5(json_encode([__METHOD__, $method, $args])); |
193
|
|
|
$self = $this; |
194
|
|
|
$this->oLogger->debug($method, $args); |
195
|
|
|
|
196
|
|
|
return $this->useCache( |
197
|
|
|
function () use ($self, $method, $args) { |
198
|
|
|
$res = $self->getSoap()->__soapCall($method, [$args]); |
199
|
|
|
$resultKey = $method . 'Result'; |
200
|
|
|
if (!property_exists($res, $resultKey)) { |
201
|
|
|
throw new Response(sprintf('%s doesnt exist in response', $resultKey)); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
return $res->{$resultKey}; |
205
|
|
|
}, |
206
|
|
|
$hashKey); |
207
|
|
|
|
208
|
|
|
} catch (\Exception $e) { |
209
|
|
|
throw Helper::handleException($e); |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Caching things |
215
|
|
|
* |
216
|
|
|
* @param \Closure $closure Function calling wheen cache is empty or not valid |
217
|
|
|
* @param mixed $hashKey Cache key of item |
218
|
|
|
* @param int|null $ttl Time to live for item |
219
|
|
|
* |
220
|
|
|
* @return mixed |
221
|
|
|
*/ |
222
|
|
|
private function useCache(\Closure $closure, $hashKey, int $ttl = null) |
223
|
|
|
{ |
224
|
|
|
if (is_array($hashKey) || is_object($hashKey)) { |
225
|
|
|
$hashKey = md5(json_encode($hashKey)); |
226
|
|
|
} else { |
227
|
|
|
$hashKey = strval($hashKey); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
if ($this->oCache) { |
231
|
|
|
if ($this->oCache->has($hashKey)) { |
232
|
|
|
$answer = $this->oCache->get($hashKey); |
233
|
|
|
} else { |
234
|
|
|
$answer = $closure(); |
235
|
|
|
$this->oCache->set($hashKey, $answer, $ttl); |
236
|
|
|
} |
237
|
|
|
} else { |
238
|
|
|
$answer = $closure(); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
return $answer; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Get SoapClient |
246
|
|
|
* |
247
|
|
|
* @return \mrcnpdlk\Teryt\TerytSoapClient |
248
|
|
|
*/ |
249
|
|
|
private function getSoap() |
250
|
|
|
{ |
251
|
|
|
try { |
252
|
|
|
if (!$this->soapClient) { |
253
|
|
|
$this->reinitSoap(); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
} catch (\Exception $e) { |
257
|
|
|
Helper::handleException($e); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
return $this->soapClient; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Get logger instance |
265
|
|
|
* |
266
|
|
|
* @return LoggerInterface |
267
|
|
|
*/ |
268
|
|
|
public function getLogger() |
269
|
|
|
{ |
270
|
|
|
return $this->oLogger; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
public function __debugInfo() |
274
|
|
|
{ |
275
|
|
|
return ['Top secret']; |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
|