Passed
Push — main ( 120722...2226e2 )
by Thierry
10:15
created

CallbackManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * Callbacks.php
5
 *
6
 * Jaxon request callback manager
7
 *
8
 * @package jaxon-core
0 ignored issues
show
Coding Style introduced by
Package name "jaxon-core" is not valid; consider "Jaxoncore" instead
Loading history...
9
 * @author Thierry Feuzeu <[email protected]>
10
 * @copyright 2017 Thierry Feuzeu <[email protected]>
11
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
12
 * @link https://github.com/jaxon-php/jaxon-core
13
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
14
15
namespace Jaxon\Request\Handler;
16
17
use Exception;
18
use Jaxon\Exception\RequestException;
19
use Jaxon\Request\Target;
20
use Jaxon\Response\Manager\ResponseManager;
21
use Jaxon\Response\ResponseInterface;
22
23
use function array_merge;
24
use function array_values;
25
use function count;
26
use function call_user_func_array;
27
use function is_a;
28
29
class CallbackManager
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class CallbackManager
Loading history...
30
{
31
    /**
32
     * The response manager.
33
     *
34
     * @var ResponseManager
35
     */
36
    private $xResponseManager;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
37
38
    /**
39
     * The callbacks to run after booting the library
40
     *
41
     * @var callable[]
42
     */
43
    protected $aBootCallbacks = [];
44
45
    /**
46
     * The callbacks to run before processing the request
47
     *
48
     * @var callable[]
49
     */
50
    protected $aBeforeCallbacks = [];
51
52
    /**
53
     * The callbacks to run after processing the request
54
     *
55
     * @var callable[]
56
     */
57
    protected $aAfterCallbacks = [];
58
59
    /**
60
     * The callbacks to run in case of invalid request
61
     *
62
     * @var callable[]
63
     */
64
    protected $aInvalidCallbacks = [];
65
66
    /**
67
     * The callbacks to run in case of error
68
     *
69
     * @var callable[]
70
     */
71
    protected $aErrorCallbacks = [];
72
73
    /**
74
     * The callbacks to run in case of exception
75
     *
76
     * @var callable[][]
77
     */
78
    protected $aExceptionCallbacks = [];
79
80
    /**
81
     * The callbacks to run when a class is instanced
82
     *
83
     * @var callable[]
84
     */
85
    protected $aInitCallbacks = [];
86
87
    /**
88
     * @param ResponseManager $xResponseManager
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
89
     */
90
    public function __construct(ResponseManager $xResponseManager)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
91
    {
92
        $this->xResponseManager = $xResponseManager;
93
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
94
95
    /**
96
     * Get the library booting callbacks, and reset the array.
97
     *
98
     * @return callable[]
99
     */
100
    public function popBootCallbacks(): array
101
    {
102
        if(empty($this->aBootCallbacks))
103
        {
104
            return [];
105
        }
106
        $aCallbacks = $this->aBootCallbacks;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
107
        $this->aBootCallbacks = [];
108
        return $aCallbacks;
109
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
110
111
    /**
112
     * Get the exception callbacks.
113
     *
114
     * @param Exception $xException      The exception class
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 6 found
Loading history...
115
     *
116
     * @return callable[]
117
     */
118
    private function getExceptionCallbacks(Exception $xException): array
119
    {
120
        $aExceptionCallbacks = [];
121
        foreach($this->aExceptionCallbacks as $sExClass => $aCallbacks)
122
        {
123
            if(is_a($xException, $sExClass))
124
            {
125
                $aExceptionCallbacks = array_merge($aExceptionCallbacks, $aCallbacks);
126
            }
127
        }
128
        return array_values($aExceptionCallbacks);
129
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
130
131
    /**
132
     * Add a library booting callback.
133
     *
134
     * @param callable $xCallable    The callback function
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
135
     *
136
     * @return CallbackManager
137
     */
138
    public function boot(callable $xCallable): CallbackManager
139
    {
140
        $this->aBootCallbacks[] = $xCallable;
141
        return $this;
142
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
143
144
    /**
145
     * Add a pre-request processing callback.
146
     *
147
     * @param callable $xCallable    The callback function
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
148
     *
149
     * @return CallbackManager
150
     */
151
    public function before(callable $xCallable): CallbackManager
152
    {
153
        $this->aBeforeCallbacks[] = $xCallable;
154
        return $this;
155
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
156
157
    /**
158
     * Add a post-request processing callback.
159
     *
160
     * @param callable $xCallable    The callback function
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
161
     *
162
     * @return CallbackManager
163
     */
164
    public function after(callable $xCallable): CallbackManager
165
    {
166
        $this->aAfterCallbacks[] = $xCallable;
167
        return $this;
168
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
169
170
    /**
171
     * Add a invalid request callback.
172
     *
173
     * @param callable $xCallable    The callback function
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
174
     *
175
     * @return CallbackManager
176
     */
177
    public function invalid(callable $xCallable): CallbackManager
178
    {
179
        $this->aInvalidCallbacks[] = $xCallable;
180
        return $this;
181
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
182
183
    /**
184
     * Add a processing error callback.
185
     *
186
     * @param callable $xCallable   The callback function
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
187
     * @param string $sExClass      The exception class
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter name; 6 found
Loading history...
188
     *
189
     * @return CallbackManager
190
     */
191
    public function error(callable $xCallable, string $sExClass = ''): CallbackManager
192
    {
193
        if($sExClass === '' || $sExClass === Exception::class)
194
        {
195
            $this->aErrorCallbacks[] = $xCallable;
196
            return $this;
197
        }
198
        // Callback for a given exception class
199
        if(isset($this->aExceptionCallbacks[$sExClass]))
200
        {
201
            $this->aExceptionCallbacks[$sExClass][] = $xCallable;
202
            return $this;
203
        }
204
        $this->aExceptionCallbacks[$sExClass] = [$xCallable];
205
        return $this;
206
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
207
208
    /**
209
     * Add a class initialisation callback.
210
     *
211
     * @param callable $xCallable    The callback function
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
212
     *
213
     * @return CallbackManager
214
     */
215
    public function init(callable $xCallable): CallbackManager
216
    {
217
        $this->aInitCallbacks[] = $xCallable;
218
        return $this;
219
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
220
221
    /**
222
     * @param callable $xCallback
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
223
     * @param array $aParameters
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
224
     *
225
     * @return void
226
     */
227
    private function executeCallback(callable $xCallback, array $aParameters)
228
    {
229
        $xReturn = call_user_func_array($xCallback, $aParameters);
230
        if($xReturn instanceof ResponseInterface)
231
        {
232
            $this->xResponseManager->append($xReturn);
233
        }
234
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
235
236
    /**
237
     * @param array $aCallbacks
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
238
     * @param array $aParameters
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
239
     *
240
     * @return void
241
     */
242
    private function executeCallbacks(array $aCallbacks, array $aParameters)
243
    {
244
        foreach($aCallbacks as $xCallback)
245
        {
246
            $this->executeCallback($xCallback, $aParameters);
247
        }
248
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
249
250
    /**
251
     * Execute the class initialisation callbacks.
252
     *
253
     * @param mixed $xRegisteredObject
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
254
     *
255
     * @return void
256
     */
257
    public function onInit($xRegisteredObject)
258
    {
259
        $this->executeCallbacks($this->aInitCallbacks, [$xRegisteredObject]);
260
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
261
262
    /**
263
     * These are the pre-request processing callbacks passed to the Jaxon library.
264
     *
265
     * @param Target $xTarget
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
266
     * @param bool $bEndRequest If set to true, the request processing is interrupted.
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
267
     *
268
     * @return void
269
     * @throws RequestException
270
     */
271
    public function onBefore(Target $xTarget, bool &$bEndRequest)
272
    {
273
        // Call the user defined callback
274
        foreach($this->aBeforeCallbacks as $xCallback)
275
        {
276
            $this->executeCallback($xCallback, [$xTarget, &$bEndRequest]);
277
            if($bEndRequest)
278
            {
279
                return;
280
            }
281
        }
282
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
283
284
    /**
285
     * These are the post-request processing callbacks passed to the Jaxon library.
286
     *
287
     * @param Target $xTarget
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
288
     * @param bool $bEndRequest
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
289
     *
290
     * @return void
291
     * @throws RequestException
292
     */
293
    public function onAfter(Target $xTarget, bool $bEndRequest)
294
    {
295
        $this->executeCallbacks($this->aAfterCallbacks, [$xTarget, $bEndRequest]);
296
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
297
298
    /**
299
     * These callbacks are called whenever an invalid request is processed.
300
     *
301
     * @param RequestException $xException
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
302
     *
303
     * @return void
304
     * @throws RequestException
305
     */
306
    public function onInvalid(RequestException $xException)
307
    {
308
        $this->executeCallbacks($this->aInvalidCallbacks, [$xException]);
309
        throw $xException;
310
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
311
312
    /**
313
     * These callbacks are called whenever an invalid request is processed.
314
     *
315
     * @param Exception $xException
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
316
     *
317
     * @return void
318
     * @throws Exception
319
     */
320
    public function onError(Exception $xException)
321
    {
322
        $aExceptionCallbacks = $this->getExceptionCallbacks($xException);
323
        $this->executeCallbacks($aExceptionCallbacks, [$xException]);
324
        if(count($aExceptionCallbacks) > 0)
325
        {
326
            // Do not throw the exception if a custom handler is defined
327
            return;
328
        }
329
330
        $this->executeCallbacks($this->aErrorCallbacks, [$xException]);
331
        throw $xException;
332
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
333
}
334