1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
/** |
5
|
|
|
* TERYT-API |
6
|
|
|
* |
7
|
|
|
* Copyright (c) 2019 pudelek.org.pl |
8
|
|
|
* |
9
|
|
|
* @license MIT License (MIT) |
10
|
|
|
* |
11
|
|
|
* For the full copyright and license information, please view source file |
12
|
|
|
* that is bundled with this package in the file LICENSE |
13
|
|
|
* @author Marcin Pudełek <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Created by Marcin Pudełek <[email protected]> |
18
|
|
|
* Date: 24.12.2019 |
19
|
|
|
* Time: 12:29 |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace mrcnpdlk\Teryt; |
23
|
|
|
|
24
|
|
|
use DateTime; |
25
|
|
|
use Mrcnpdlk\Lib\ConfigurationOptionsAbstract; |
26
|
|
|
use mrcnpdlk\Psr16Cache\Adapter; |
27
|
|
|
use mrcnpdlk\Teryt\Exception\Connection; |
28
|
|
|
use mrcnpdlk\Teryt\Exception\Response; |
29
|
|
|
use Psr\SimpleCache\CacheInterface; |
30
|
|
|
|
31
|
|
|
class Config extends ConfigurationOptionsAbstract |
32
|
|
|
{ |
33
|
|
|
public const SERVICE_URL_TEST = 'https://uslugaterytws1test.stat.gov.pl/wsdl/terytws1.wsdl'; |
34
|
|
|
public const SERVICE_URL = 'https://uslugaterytws1.stat.gov.pl/wsdl/terytws1.wsdl'; |
35
|
|
|
public const SERVICE_USER_TEST = 'TestPubliczny'; |
36
|
|
|
public const SERVICE_PASSWORD_TEST = '1234abcd'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $username = self::SERVICE_USER_TEST; |
42
|
|
|
/** |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
protected $password = self::SERVICE_PASSWORD_TEST; |
46
|
|
|
/** |
47
|
|
|
* @var bool |
48
|
|
|
*/ |
49
|
|
|
protected $isProduction = false; |
50
|
|
|
/** |
51
|
|
|
* @var \mrcnpdlk\Psr16Cache\Adapter |
52
|
|
|
*/ |
53
|
|
|
private $oCacheAdapter; |
54
|
|
|
/** |
55
|
|
|
* SoapClient handler |
56
|
|
|
* |
57
|
|
|
* @var \mrcnpdlk\Teryt\TerytSoapClient|null |
58
|
|
|
*/ |
59
|
|
|
private $soapClient; |
60
|
|
|
/** |
61
|
|
|
* @var \Psr\SimpleCache\CacheInterface|null |
62
|
|
|
*/ |
63
|
|
|
protected $cache; |
64
|
|
|
/** |
65
|
|
|
* @var string |
66
|
|
|
*/ |
67
|
|
|
private $serviceUrl; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Config constructor. |
71
|
|
|
* |
72
|
|
|
* @param array<string,mixed> $config |
73
|
|
|
* |
74
|
|
|
* @throws \Mrcnpdlk\Lib\ConfigurationException |
75
|
|
|
*/ |
76
|
13 |
|
public function __construct(array $config = []) |
77
|
|
|
{ |
78
|
13 |
|
parent::__construct($config); |
79
|
13 |
|
$this->oCacheAdapter = new Adapter($this->cache, $this->getLogger()); |
80
|
13 |
|
$this->serviceUrl = $this->isProduction ? self::SERVICE_URL : self::SERVICE_URL_TEST; |
81
|
13 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Making request to Teryt WS1 API |
85
|
|
|
* |
86
|
|
|
* @param string $method Method name |
87
|
|
|
* @param array<string, mixed> $args Parameters |
88
|
|
|
* @param bool $addDate Add DataStanu to request |
89
|
|
|
* |
90
|
|
|
* @throws \mrcnpdlk\Teryt\Exception |
91
|
|
|
* @throws \mrcnpdlk\Teryt\Exception\Connection |
92
|
|
|
* |
93
|
|
|
* @return mixed |
94
|
|
|
*/ |
95
|
11 |
|
public function request(string $method, array $args = [], bool $addDate = true) |
96
|
|
|
{ |
97
|
|
|
try { |
98
|
11 |
|
if (!array_key_exists('DataStanu', $args) && $addDate) { |
99
|
10 |
|
$args['DataStanu'] = (new DateTime())->format('Y-m-d'); |
100
|
|
|
} |
101
|
11 |
|
$self = $this; |
102
|
11 |
|
$this->getLogger()->debug(sprintf('REQ: %s', $method), $args); |
103
|
|
|
|
104
|
11 |
|
$resp = $this->oCacheAdapter->useCache( |
105
|
|
|
static function () use ($self, $method, $args) { |
106
|
11 |
|
$res = $self->getSoap()->__soapCall($method, [$args]); |
107
|
10 |
|
$resultKey = $method . 'Result'; |
108
|
10 |
|
if (!property_exists($res, $resultKey)) { |
109
|
|
|
throw new Response(sprintf('%s doesnt exist in response', $resultKey)); |
110
|
|
|
} |
111
|
|
|
|
112
|
10 |
|
return $res->{$resultKey}; |
113
|
11 |
|
}, |
114
|
11 |
|
[__METHOD__, $method, $args] |
115
|
|
|
); |
116
|
|
|
|
117
|
10 |
|
$this->getLogger()->debug(sprintf('RESP: %s, type is %s', $method, gettype($resp))); |
118
|
|
|
|
119
|
10 |
|
return $resp; |
120
|
1 |
|
} catch (\Exception $e) { |
121
|
1 |
|
throw Helper::handleException($e); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param \Psr\SimpleCache\CacheInterface|null $cache |
127
|
|
|
* |
128
|
|
|
* @return Config |
129
|
|
|
*/ |
130
|
|
|
public function setCache(?CacheInterface $cache): Config |
131
|
|
|
{ |
132
|
|
|
$this->cache = $cache; |
133
|
|
|
|
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param bool $isProduction |
139
|
|
|
* |
140
|
|
|
* @return Config |
141
|
|
|
*/ |
142
|
1 |
|
public function setIsProduction(bool $isProduction): Config |
143
|
|
|
{ |
144
|
1 |
|
$this->isProduction = $isProduction; |
145
|
|
|
|
146
|
1 |
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param string $password |
151
|
|
|
* |
152
|
|
|
* @return Config |
153
|
|
|
*/ |
154
|
1 |
|
public function setPassword(string $password): Config |
155
|
|
|
{ |
156
|
1 |
|
$this->password = $password; |
157
|
|
|
|
158
|
1 |
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param string $username |
163
|
|
|
* |
164
|
|
|
* @return Config |
165
|
|
|
*/ |
166
|
1 |
|
public function setUsername(string $username): Config |
167
|
|
|
{ |
168
|
1 |
|
$this->username = $username; |
169
|
|
|
|
170
|
1 |
|
return $this; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Get SoapClient |
175
|
|
|
* |
176
|
|
|
* @return \mrcnpdlk\Teryt\TerytSoapClient |
177
|
|
|
*/ |
178
|
11 |
|
private function getSoap(): TerytSoapClient |
179
|
|
|
{ |
180
|
|
|
try { |
181
|
11 |
|
if (null === $this->soapClient) { |
182
|
11 |
|
$this->reinitSoap(); |
183
|
|
|
} |
184
|
|
|
} catch (\Exception $e) { |
185
|
|
|
Helper::handleException($e); |
186
|
|
|
} |
187
|
|
|
|
188
|
11 |
|
return $this->soapClient; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Reinit Soap Client |
193
|
|
|
* |
194
|
|
|
* @throws \Exception |
195
|
|
|
* @throws Connection |
196
|
|
|
* @throws Exception |
197
|
|
|
* |
198
|
|
|
* @return $this |
199
|
|
|
*/ |
200
|
11 |
|
private function reinitSoap(): self |
201
|
|
|
{ |
202
|
|
|
try { |
203
|
11 |
|
$this->soapClient = new TerytSoapClient($this->serviceUrl, [ |
204
|
11 |
|
'soap_version' => SOAP_1_1, |
205
|
|
|
'exceptions' => true, |
206
|
11 |
|
'cache_wsdl' => WSDL_CACHE_BOTH, |
207
|
|
|
]); |
208
|
11 |
|
$this->soapClient->addUserToken($this->username, $this->password); |
209
|
|
|
} catch (\Exception $e) { |
210
|
|
|
throw Helper::handleException($e); |
211
|
|
|
} |
212
|
|
|
|
213
|
11 |
|
return $this; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|