1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Skautis\Wsdl; |
4
|
|
|
|
5
|
|
|
use Skautis\EventDispatcher\EventDispatcherInterface; |
6
|
|
|
use Skautis\Config; |
7
|
|
|
use Skautis\User; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Třída pro správu webových služeb SkautISu |
11
|
|
|
*/ |
12
|
|
|
class WsdlManager |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var WebServiceFactoryInterface |
17
|
|
|
*/ |
18
|
|
|
protected $webServiceFactory; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var Config |
22
|
|
|
*/ |
23
|
|
|
protected $config; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Aliasy webových služeb pro rychlý přístup |
27
|
|
|
* |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected $aliases = [ |
31
|
|
|
"user" => "UserManagement", |
32
|
|
|
"usr" => "UserManagement", |
33
|
|
|
"org" => "OrganizationUnit", |
34
|
|
|
"app" => "ApplicationManagement", |
35
|
|
|
"event" => "Events", |
36
|
|
|
"events" => "Events", |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Dostupné webové služby SkautISu |
41
|
|
|
* |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
protected $supportedWebServices = [ |
45
|
|
|
"ApplicationManagement", |
46
|
|
|
"ContentManagement", |
47
|
|
|
"Evaluation", |
48
|
|
|
"Events", |
49
|
|
|
"Exports", |
50
|
|
|
"GoogleApps", |
51
|
|
|
"Journal", |
52
|
|
|
"Material", |
53
|
|
|
"Message", |
54
|
|
|
"OrganizationUnit", |
55
|
|
|
"Power", |
56
|
|
|
"Reports", |
57
|
|
|
"Summary", |
58
|
|
|
"Task", |
59
|
|
|
"Telephony", |
60
|
|
|
"UserManagement", |
61
|
|
|
"Vivant", |
62
|
|
|
"Welcome", |
63
|
|
|
]; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var array |
67
|
|
|
*/ |
68
|
|
|
protected $webServiceListeners = []; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Pole aktivních webových služeb |
72
|
|
|
* |
73
|
|
|
* @var array |
74
|
|
|
*/ |
75
|
|
|
protected $webServices = []; |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param WebServiceFactoryInterface $webServiceFactory továrna pro vytváření objektů webových služeb |
80
|
|
|
* @param Config $config |
81
|
|
|
*/ |
82
|
3 |
|
public function __construct(WebServiceFactoryInterface $webServiceFactory, Config $config) |
83
|
|
|
{ |
84
|
3 |
|
$this->webServiceFactory = $webServiceFactory; |
85
|
3 |
|
$this->config = $config; |
86
|
3 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return Config |
90
|
|
|
*/ |
91
|
|
|
public function getConfig() |
92
|
|
|
{ |
93
|
|
|
return $this->config; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Získá objekt webové služby |
98
|
|
|
* |
99
|
|
|
* @param string $name jméno nebo alias webové služby |
100
|
|
|
* @param string|null $loginId skautIS login token |
101
|
|
|
* @return WebServiceInterface |
102
|
|
|
*/ |
103
|
1 |
|
public function getWebService($name, $loginId = null) |
104
|
|
|
{ |
105
|
1 |
|
$name = $this->getWebServiceName($name); |
106
|
1 |
|
$key = $loginId . '_' . $name . ($this->config->isTestMode() ? '_Test' : ''); |
107
|
|
|
|
108
|
1 |
|
if (!isset($this->webServices[$key])) { |
109
|
1 |
|
$options = $this->config->getSoapOptions(); |
110
|
1 |
|
$options[User::ID_LOGIN] = $loginId; |
111
|
1 |
|
$this->webServices[$key] = $this->createWebService($name, $options); |
112
|
|
|
} |
113
|
|
|
|
114
|
1 |
|
return $this->webServices[$key]; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Vytváří objekt webové služby |
119
|
|
|
* |
120
|
|
|
* @param string $name jméno webové služby |
121
|
|
|
* @param array $options volby pro SoapClient |
122
|
|
|
* @return WebService|mixed |
123
|
|
|
*/ |
124
|
1 |
|
public function createWebService($name, array $options = []) |
125
|
|
|
{ |
126
|
1 |
|
$webService = $this->webServiceFactory->createWebService($this->getWebServiceUrl($name), $options); |
127
|
|
|
|
128
|
1 |
|
if ($webService instanceof EventDispatcherInterface) { |
129
|
|
|
// Zaregistruj listenery na vytvořeném objektu webové služby, pokud je to podporováno |
130
|
|
|
foreach ($this->webServiceListeners as $listener) { |
131
|
|
|
$webService->subscribe($listener['eventName'], $listener['callback']); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
1 |
|
return $webService; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Vrací celé jméno webové služby |
140
|
|
|
* |
141
|
|
|
* @param string $name jméno nebo alias webové služby |
142
|
|
|
* @return string |
143
|
|
|
* @throws WsdlException |
144
|
|
|
*/ |
145
|
1 |
|
protected function getWebServiceName($name) |
146
|
|
|
{ |
147
|
1 |
|
if (in_array($name, $this->supportedWebServices)) { |
148
|
|
|
// služba s daným jménem existuje |
149
|
1 |
|
return $name; |
150
|
|
|
} |
151
|
|
|
if (array_key_exists($name, $this->aliases) && in_array($this->aliases[$name], $this->supportedWebServices)) { |
152
|
|
|
// je definovaný alias pro tuto službu |
153
|
|
|
return $this->aliases[$name]; |
154
|
|
|
} |
155
|
|
|
throw new WsdlException("Web service '$name' not found."); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Vrací URL webové služby podle jejího jména |
160
|
|
|
* |
161
|
|
|
* @param string $name celé jméno webové služby |
162
|
|
|
* @return string |
163
|
|
|
*/ |
164
|
1 |
|
protected function getWebServiceUrl($name) |
165
|
|
|
{ |
166
|
1 |
|
return $this->config->getBaseUrl() . "JunakWebservice/" . rawurlencode($name) . ".asmx?WSDL"; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Vrací seznam webových služeb, které podporuje |
171
|
|
|
* |
172
|
|
|
* @return array |
173
|
|
|
*/ |
174
|
1 |
|
public function getSupportedWebServices() |
175
|
|
|
{ |
176
|
1 |
|
return $this->supportedWebServices; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @return bool |
181
|
|
|
*/ |
182
|
|
|
public function isMaintenance() |
183
|
|
|
{ |
184
|
|
|
$headers = get_headers($this->getWebServiceUrl("UserManagement")); |
185
|
|
|
return !in_array('HTTP/1.1 200 OK', $headers); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Přidá listener na spravovaných vytvářených webových služeb. |
190
|
|
|
* |
191
|
|
|
* @param string $eventName |
192
|
|
|
* @param callable $callback |
193
|
|
|
*/ |
194
|
|
|
public function addWebServiceListener($eventName, callable $callback) |
195
|
|
|
{ |
196
|
|
|
$this->webServiceListeners[] = [ |
197
|
|
|
'eventName' => $eventName, |
198
|
|
|
'callback' => $callback, |
199
|
|
|
]; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|