1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Skautis\Wsdl; |
5
|
|
|
|
6
|
|
|
use Skautis\EventDispatcher\EventDispatcherInterface; |
7
|
|
|
use Skautis\Config; |
8
|
|
|
use Skautis\User; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Třída pro správu webových služeb SkautISu |
12
|
|
|
*/ |
13
|
|
|
class WsdlManager |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var WebServiceFactoryInterface |
18
|
|
|
*/ |
19
|
|
|
protected $webServiceFactory; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var Config |
23
|
|
|
*/ |
24
|
|
|
protected $config; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $webServiceListeners = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Pole aktivních webových služeb |
33
|
|
|
* |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
protected $webServices = []; |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param WebServiceFactoryInterface $webServiceFactory továrna pro vytváření objektů webových služeb |
41
|
|
|
* @param Config $config |
42
|
|
|
*/ |
43
|
4 |
|
public function __construct(WebServiceFactoryInterface $webServiceFactory, Config $config) |
44
|
|
|
{ |
45
|
4 |
|
$this->webServiceFactory = $webServiceFactory; |
46
|
4 |
|
$this->config = $config; |
47
|
4 |
|
} |
48
|
|
|
|
49
|
2 |
|
public function getConfig(): Config |
50
|
|
|
{ |
51
|
2 |
|
return $this->config; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Získá objekt webové služby |
56
|
|
|
* |
57
|
|
|
* @param string $name celé jméno webové služby |
58
|
|
|
* @param string|null $loginId skautIS login token |
59
|
|
|
*/ |
60
|
3 |
|
public function getWebService(string $name, ?string $loginId = null): WebServiceInterface |
61
|
|
|
{ |
62
|
3 |
|
$key = $loginId . '_' . $name; |
63
|
|
|
|
64
|
3 |
|
if (!isset($this->webServices[$key])) { |
65
|
2 |
|
$options = $this->config->getSoapOptions(); |
66
|
2 |
|
$options[User::ID_LOGIN] = $loginId; |
67
|
2 |
|
$this->webServices[$key] = $this->createWebService($name, $options); |
68
|
|
|
} |
69
|
|
|
|
70
|
3 |
|
return $this->webServices[$key]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Vytváří objekt webové služby |
75
|
|
|
* |
76
|
|
|
* @param string $name jméno webové služby |
77
|
|
|
* @param array $options volby pro SoapClient |
78
|
|
|
*/ |
79
|
2 |
|
public function createWebService(string $name, array $options = []): WebServiceInterface |
80
|
|
|
{ |
81
|
2 |
|
$webService = $this->webServiceFactory->createWebService($this->getWebServiceUrl($name), $options); |
82
|
|
|
|
83
|
2 |
|
if ($webService instanceof EventDispatcherInterface) { |
84
|
|
|
// Zaregistruj listenery na vytvořeném objektu webové služby, pokud je to podporováno |
85
|
2 |
|
foreach ($this->webServiceListeners as $listener) { |
86
|
|
|
$webService->subscribe($listener['eventName'], $listener['callback']); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
2 |
|
return $webService; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Vrací URL webové služby podle jejího jména |
95
|
|
|
*/ |
96
|
2 |
|
protected function getWebServiceUrl(string $name): string |
97
|
|
|
{ |
98
|
2 |
|
if (!WebServiceName::isValidServiceName($name)) { |
99
|
|
|
throw new WsdlException("Web service '$name' not found."); |
100
|
|
|
} |
101
|
|
|
|
102
|
2 |
|
return $this->config->getBaseUrl() . 'JunakWebservice/' . rawurlencode($name) . '.asmx?WSDL'; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function isMaintenance(): bool |
106
|
|
|
{ |
107
|
|
|
$headers = get_headers($this->getWebServiceUrl("UserManagement")); |
108
|
|
|
return !$headers || !in_array('HTTP/1.1 200 OK', $headers); |
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Přidá listener na spravovaných vytvářených webových služeb. |
113
|
|
|
*/ |
114
|
|
|
public function addWebServiceListener(string $eventName, callable $callback): void |
115
|
|
|
{ |
116
|
|
|
$this->webServiceListeners[] = [ |
117
|
|
|
'eventName' => $eventName, |
118
|
|
|
'callback' => $callback, |
119
|
|
|
]; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.