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