1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Skaut\Skautis\Wsdl; |
5
|
|
|
|
6
|
|
|
use Skaut\Skautis\Config; |
7
|
|
|
use Skaut\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
|
|
|
* Pole aktivních webových služeb |
27
|
|
|
* |
28
|
|
|
* @var array<string, WebServiceInterface> |
29
|
|
|
*/ |
30
|
|
|
protected $webServices = []; |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param WebServiceFactoryInterface $webServiceFactory továrna pro vytváření objektů webových služeb |
35
|
|
|
* @param Config $config |
36
|
|
|
*/ |
37
|
8 |
|
public function __construct(WebServiceFactoryInterface $webServiceFactory, Config $config) |
38
|
|
|
{ |
39
|
8 |
|
$this->webServiceFactory = $webServiceFactory; |
40
|
8 |
|
$this->config = $config; |
41
|
8 |
|
} |
42
|
|
|
|
43
|
2 |
|
public function getConfig(): Config |
44
|
|
|
{ |
45
|
2 |
|
return $this->config; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Získá objekt webové služby |
50
|
|
|
* |
51
|
|
|
* @param string $name celé jméno webové služby |
52
|
|
|
* @param string|null $loginId skautIS login token |
53
|
|
|
*/ |
54
|
3 |
|
public function getWebService(string $name, ?string $loginId = null): WebServiceInterface |
55
|
|
|
{ |
56
|
3 |
|
$key = $loginId . '_' . $name; |
57
|
|
|
|
58
|
3 |
|
if (!isset($this->webServices[$key])) { |
59
|
2 |
|
$options = $this->config->getSoapOptions(); |
60
|
2 |
|
$options[User::ID_LOGIN] = $loginId; |
61
|
2 |
|
$this->webServices[$key] = $this->createWebService($name, $options); |
62
|
|
|
} |
63
|
|
|
|
64
|
3 |
|
return $this->webServices[$key]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Vytváří objekt webové služby |
69
|
|
|
* |
70
|
|
|
* @param string $name jméno webové služby |
71
|
|
|
* @param array<string, mixed> $options volby pro SoapClient |
72
|
|
|
*/ |
73
|
2 |
|
public function createWebService(string $name, array $options = []): WebServiceInterface |
74
|
|
|
{ |
75
|
2 |
|
return $this->webServiceFactory->createWebService($this->getWebServiceUrl($name), $options); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Vrací URL webové služby podle jejího jména |
80
|
|
|
*/ |
81
|
6 |
|
protected function getWebServiceUrl(string $name): string |
82
|
|
|
{ |
83
|
6 |
|
if (!WebServiceName::isValidServiceName($name)) { |
84
|
|
|
throw new WsdlException("Web service '$name' not found."); |
85
|
|
|
} |
86
|
|
|
|
87
|
6 |
|
return $this->config->getBaseUrl() . 'JunakWebservice/' . rawurlencode($name) . '.asmx?WSDL'; |
88
|
|
|
} |
89
|
|
|
|
90
|
4 |
|
public function isMaintenance(): bool |
91
|
|
|
{ |
92
|
|
|
// Transformuje PHP error/warning do Exception |
93
|
|
|
// Funkce get_headers totiž používá warning když má problém aby vysvětlil co se děje |
94
|
|
|
// Pokud například DNS selže tak to hodí PHP warning, který nejde chytat jako exception |
95
|
|
|
set_error_handler(function($errno, $errstr, $errfile, $errline) { |
96
|
1 |
|
throw new MaintenanceErrorException($errstr, $errno, $errfile, $errline); |
97
|
4 |
|
}); |
98
|
|
|
|
99
|
|
|
try { |
100
|
4 |
|
$headers = get_headers($this->getWebServiceUrl('UserManagement')); |
101
|
|
|
|
102
|
3 |
|
return !$headers || !in_array('HTTP/1.1 200 OK', $headers, true); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
finally { |
105
|
4 |
|
restore_error_handler(); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
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.