1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Skaut\Skautis; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Trida slouzici pro debugovani SOAP pozadvku na servery Skautisu |
8
|
|
|
*/ |
9
|
|
|
class SkautisQuery implements \Serializable |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var string Nazev funkce volane pomoci SOAP requestu |
14
|
|
|
*/ |
15
|
|
|
public $fname; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Parametry SOAP requestu na server |
19
|
|
|
* |
20
|
|
|
* @var array<int, mixed> |
21
|
|
|
*/ |
22
|
|
|
public $args; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array<int, array<string, mixed>> Zasobnik volanych funkci |
26
|
|
|
*/ |
27
|
|
|
public $trace; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var float Doba trvani pozadvku |
31
|
|
|
*/ |
32
|
|
|
public $time; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var mixed |
36
|
|
|
*/ |
37
|
|
|
public $result; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* V pripade ze SOAP pozadavek selze |
41
|
|
|
* |
42
|
|
|
* Nelze povolit uzivateli primy pristup kvuli serializaci. Ne vsechny exceptions jdou serializovat. |
43
|
|
|
* |
44
|
|
|
* @var \Exception|null |
45
|
|
|
*/ |
46
|
|
|
protected $exception = null; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Po unserializaci Query s exception je zde jeji trida |
50
|
|
|
* |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
protected $exceptionClass = ''; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Po unserializaci je zde text exxception |
57
|
|
|
* |
58
|
|
|
* Pouziva __toString() methodu |
59
|
|
|
* |
60
|
|
|
* @var string |
61
|
|
|
*/ |
62
|
|
|
protected $exceptionString = ''; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* |
66
|
|
|
* |
67
|
|
|
* @param string $fname Nazev volane funkce |
68
|
|
|
* @param array<int, mixed> $args Argumenty pozadavku |
69
|
|
|
* @param array<int, array<string, mixed>> $trace Zasobnik volanych funkci |
70
|
|
|
*/ |
71
|
6 |
|
public function __construct( |
72
|
|
|
string $fname, |
73
|
|
|
array $args = [], |
74
|
|
|
array $trace = [] |
75
|
|
|
) { |
76
|
6 |
|
$this->fname = $fname; |
77
|
6 |
|
$this->args = $args; |
78
|
6 |
|
$this->trace = $trace; |
79
|
6 |
|
$this->time = -microtime(true); |
|
|
|
|
80
|
6 |
|
} |
81
|
|
|
|
82
|
2 |
|
public function serialize(): string |
83
|
|
|
{ |
84
|
|
|
$data = [ |
85
|
2 |
|
'fname' => $this->fname, |
86
|
2 |
|
'args' => $this->args, |
87
|
2 |
|
'trace' => $this->trace, |
88
|
2 |
|
'time' => $this->time, |
89
|
2 |
|
'result' => $this->result, |
90
|
2 |
|
'exception_class' => $this->exception === null ? '' : get_class($this->exception), |
91
|
2 |
|
'exception_string' => $this->exception === null ? '' : (string)$this->exception, |
92
|
|
|
]; |
93
|
2 |
|
return serialize($data); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string $data |
98
|
|
|
*/ |
99
|
2 |
|
public function unserialize($data): void |
100
|
|
|
{ |
101
|
2 |
|
$data = unserialize($data); |
102
|
2 |
|
$this->fname = (string) $data['fname']; |
103
|
2 |
|
$this->args = (array) $data['args']; |
104
|
2 |
|
$this->trace = (array) $data['trace']; |
105
|
2 |
|
$this->time = (float) $data['time']; |
106
|
2 |
|
$this->result = $data['result']; |
107
|
2 |
|
$this->exceptionClass = (string) $data['exception_class']; |
108
|
2 |
|
$this->exceptionString = (string) $data['exception_string']; |
109
|
2 |
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Oznac pozadavek za dokonceny a uloz vysledek |
113
|
|
|
* |
114
|
|
|
* @param mixed $result Odpoved ze serveru |
115
|
|
|
* @param \Exception $e Výjimka v pripade problemu |
116
|
|
|
*/ |
117
|
6 |
|
public function done($result = null, \Exception $e = null): self |
118
|
|
|
{ |
119
|
6 |
|
$this->time += microtime(true); |
120
|
6 |
|
$this->result = $result; |
121
|
6 |
|
$this->exception = $e; |
122
|
|
|
|
123
|
6 |
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Vrati tridu exception |
128
|
|
|
* |
129
|
|
|
* Pouziva se tato metoda protoze SoapFault exception vyhozena SoapClientem nejde serializovat |
130
|
|
|
*/ |
131
|
5 |
|
public function getExceptionClass(): string |
132
|
|
|
{ |
133
|
5 |
|
if ($this->exception === null) { |
134
|
2 |
|
return $this->exceptionClass; |
135
|
|
|
} |
136
|
|
|
|
137
|
3 |
|
return get_class($this->exception); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Vrati textovou podobu exception |
142
|
|
|
*/ |
143
|
2 |
|
public function getExceptionString(): string |
144
|
|
|
{ |
145
|
2 |
|
if ($this->exception === null) { |
146
|
|
|
return $this->exceptionString; |
147
|
|
|
} |
148
|
|
|
|
149
|
2 |
|
return (string)$this->exception; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Kontrola jestli se pozadavek zdaril |
154
|
|
|
*/ |
155
|
6 |
|
public function hasFailed(): bool |
156
|
|
|
{ |
157
|
6 |
|
return $this->exception !== null || strlen($this->exceptionClass) > 0; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.