1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Skautis\Wsdl; |
4
|
|
|
|
5
|
|
|
use Skautis\EventDispatcher\EventDispatcherTrait; |
6
|
|
|
use Skautis\InvalidArgumentException; |
7
|
|
|
use Skautis\SkautisQuery; |
8
|
|
|
use SoapFault; |
9
|
|
|
use stdClass; |
10
|
|
|
use SoapClient; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @author Hána František <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class WebService implements WebServiceInterface |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
use EventDispatcherTrait; |
19
|
|
|
|
20
|
|
|
const EVENT_SUCCESS = 1; |
21
|
|
|
const EVENT_FAILURE = 2; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* základní údaje volané při každém požadavku |
25
|
|
|
* ID_Application, ID_Login |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $init; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var SoapClient |
32
|
|
|
*/ |
33
|
|
|
protected $soapClient; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param mixed $wsdl Odkaz na WSDL soubor |
37
|
|
|
* @param array $soapOpts Nastaveni SOAP requestu |
38
|
|
|
* Ma pouzivat kompresi na prenasena data? |
39
|
|
|
* @throws InvalidArgumentException pokud je odkaz na WSDL soubor prázdný |
40
|
|
|
*/ |
41
|
3 |
|
public function __construct($wsdl, array $soapOpts) |
42
|
|
|
{ |
43
|
3 |
|
$this->init = $soapOpts; |
44
|
3 |
|
if (empty($wsdl)) { |
45
|
1 |
|
throw new InvalidArgumentException("WSDL address cannot be empty."); |
46
|
|
|
} |
47
|
|
|
|
48
|
2 |
|
$this->soapClient = new SoapClient($wsdl, $soapOpts); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @inheritdoc |
53
|
|
|
*/ |
54
|
|
|
public function call($functionName, array $arguments = []) |
55
|
|
|
{ |
56
|
|
|
return $this->soapCall($functionName, $arguments); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @inheritdoc |
62
|
|
|
*/ |
63
|
|
|
public function __call($functionName, $arguments) |
64
|
|
|
{ |
65
|
|
|
return $this->call($functionName, $arguments); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Metoda provadejici SOAP pozadavek na servery Skautisu |
70
|
|
|
* |
71
|
|
|
* @see http://php.net/manual/en/soapclient.soapcall.php |
72
|
|
|
* |
73
|
|
|
* @param string $function_name Nazev akce k provedeni na WebService |
74
|
|
|
* @param array $arguments ([0]=args [1]=cover) |
75
|
|
|
* @param array $options Nastaveni |
76
|
|
|
* @param mixed $input_headers Hlavicky pouzite pri odesilani |
77
|
|
|
* @param array $output_headers Hlavicky ktere prijdou s odpovedi |
78
|
|
|
* @return mixed |
79
|
|
|
*/ |
80
|
|
|
protected function soapCall($function_name, $arguments, array $options = [], $input_headers = null, array &$output_headers = []) |
81
|
|
|
{ |
82
|
|
|
$fname = ucfirst($function_name); |
83
|
|
|
$args = $this->prepareArgs($fname, $arguments); |
84
|
|
|
|
85
|
|
|
if ($this->hasListeners()) { |
86
|
|
|
$query = new SkautisQuery($fname, $args, debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
try { |
90
|
|
|
$soapResponse = $this->soapClient->__soapCall($fname, $args, $options, $input_headers, $output_headers); |
91
|
|
|
|
92
|
|
|
$soapResponse = $this->parseOutput($fname, $soapResponse); |
93
|
|
|
|
94
|
|
|
if ($this->hasListeners()) { |
95
|
|
|
$this->dispatch(self::EVENT_SUCCESS, $query->done($soapResponse)); |
96
|
|
|
} |
97
|
|
|
return $soapResponse; |
98
|
|
|
} catch (SoapFault $e) { |
99
|
|
|
if ($this->hasListeners()) { |
100
|
|
|
$this->dispatch(self::EVENT_FAILURE, $query->done(null, $e)); |
101
|
|
|
} |
102
|
|
|
if (preg_match('/Uživatel byl odhlášen/', $e->getMessage())) { |
103
|
|
|
throw new AuthenticationException($e->getMessage(), $e->getCode(), $e); |
104
|
|
|
} |
105
|
|
|
if (preg_match('/Nemáte oprávnění/', $e->getMessage())) { |
106
|
|
|
throw new PermissionException($e->getMessage(), $e->getCode(), $e); |
107
|
|
|
} |
108
|
|
|
throw new WsdlException($e->getMessage(), $e->getCode(), $e); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Z defaultnich parametru a parametru callu vytvori argumenty pro SoapClient::__soapCall |
114
|
|
|
* |
115
|
|
|
* @param string $function_name Jmeno funkce volane pres SOAP |
116
|
|
|
* @param array $arguments Argumenty k mergnuti s defaultnimy |
117
|
|
|
* |
118
|
|
|
* @return array Argumenty pro SoapClient::__soapCall |
119
|
|
|
*/ |
120
|
|
|
protected function prepareArgs($function_name, array $arguments) |
121
|
|
|
{ |
122
|
|
|
if (!isset($arguments[0]) || !is_array($arguments[0])) { |
123
|
|
|
$arguments[0] = []; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$args = array_merge($this->init, $arguments[0]); //k argumentum připoji vlastni informace o aplikaci a uzivateli |
127
|
|
|
|
128
|
|
|
if (!isset($arguments[1]) || $arguments[1] === null) { |
129
|
|
|
$function_name = strtolower(substr($function_name, 0, 1)) . substr($function_name, 1); //nahrazuje lcfirst |
130
|
|
|
$args = [[$function_name . "Input" => $args]]; |
131
|
|
|
return $args; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
//pokud je zadan druhy parametr tak lze prejmenovat obal dat |
135
|
|
|
$matches = preg_split('~/~', $arguments[1]); //rozdeli to na stringy podle / |
136
|
|
|
$matches = array_reverse($matches); //pole se budou vytvaret zevnitr ven |
137
|
|
|
|
138
|
|
|
$matches[] = 0; //zakladni obal 0=>... |
139
|
|
|
|
140
|
|
|
foreach ($matches as $value) { |
141
|
|
|
$args = [$value => $args]; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $args; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Parsuje output ze SoapClient do jednotného formátu |
149
|
|
|
* |
150
|
|
|
* @param string $fname Jméno funkce volané přes SOAP |
151
|
|
|
* @param mixed $ret Odpoveď ze SoapClient::__soapCall |
152
|
|
|
* |
153
|
|
|
* @return array |
154
|
|
|
*/ |
155
|
|
|
protected function parseOutput($fname, $ret) |
156
|
|
|
{ |
157
|
|
|
//pokud obsahuje Output tak vždy vrací pole i s jedním prvkem. |
158
|
|
|
if (!isset($ret->{$fname . "Result"})) { |
159
|
|
|
return $ret; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
if (!isset($ret->{$fname . "Result"}->{$fname . "Output"})) { |
163
|
|
|
return $ret->{$fname . "Result"}; //neobsahuje $fname.Output |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
if ($ret->{$fname . "Result"}->{$fname . "Output"} instanceof stdClass) { //vraci pouze jednu hodnotu misto pole? |
167
|
|
|
return [$ret->{$fname . "Result"}->{$fname . "Output"}]; //vraci pole se stdClass |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return $ret->{$fname . "Result"}->{$fname . "Output"}; //vraci pole se stdClass |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|