|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* Copyright (c) 2012-2014 Janos Szurovecz |
|
4
|
|
|
* |
|
5
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
|
6
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
|
7
|
|
|
* the Software without restriction, including without limitation the rights to |
|
8
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
|
9
|
|
|
* of the Software, and to permit persons to whom the Software is furnished to do |
|
10
|
|
|
* so, subject to the following conditions: |
|
11
|
|
|
* |
|
12
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
13
|
|
|
* copies or substantial portions of the Software. |
|
14
|
|
|
* |
|
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
16
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
18
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
19
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
20
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
21
|
|
|
* SOFTWARE. |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
namespace predaddy\messagehandling\interceptors; |
|
25
|
|
|
|
|
26
|
|
|
use precore\lang\Object; |
|
27
|
|
|
use predaddy\messagehandling\DispatchInterceptor; |
|
28
|
|
|
use predaddy\messagehandling\InterceptorChain; |
|
29
|
|
|
use predaddy\messagehandling\NonBlockable; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* It can block and buffer messages in blocking mode. Its state can be configured with start and stop methods. |
|
33
|
|
|
* The collected messages can be flushed or cleared according to the needs. |
|
34
|
|
|
* |
|
35
|
|
|
* <p> The control can be done by a manager object, which is also an interceptor. |
|
36
|
|
|
* It should be used if two buses should work together. |
|
37
|
|
|
* For instance in a CQRS environment all events can be buffered until the command is processed. |
|
38
|
|
|
* |
|
39
|
|
|
* <p> If the message implements {@link NonBlockable} interface, it will never be buffered. |
|
40
|
|
|
* |
|
41
|
|
|
* @author Janos Szurovecz <[email protected]> |
|
42
|
|
|
*/ |
|
43
|
|
|
final class BlockerInterceptor extends Object implements DispatchInterceptor |
|
44
|
|
|
{ |
|
45
|
|
|
private $blocking = false; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var InterceptorChain[] |
|
49
|
|
|
*/ |
|
50
|
|
|
private $chains = []; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var BlockerInterceptorManager |
|
54
|
|
|
*/ |
|
55
|
|
|
private $manager; |
|
56
|
|
|
|
|
57
|
|
|
public function __construct() |
|
58
|
|
|
{ |
|
59
|
|
|
$this->manager = new BlockerInterceptorManager($this); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function invoke($message, InterceptorChain $chain) |
|
63
|
|
|
{ |
|
64
|
|
|
if ($this->blocking && !($message instanceof NonBlockable)) { |
|
65
|
|
|
$this->chains[] = $chain; |
|
66
|
|
|
} else { |
|
67
|
|
|
$chain->proceed(); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function startBlocking() |
|
72
|
|
|
{ |
|
73
|
|
|
$this->blocking = true; |
|
74
|
|
|
self::getLogger()->debug('Message blocking has been started'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function stopBlocking() |
|
78
|
|
|
{ |
|
79
|
|
|
$this->blocking = false; |
|
80
|
|
|
self::getLogger()->debug('Message blocking has been stopped'); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function flush() |
|
84
|
|
|
{ |
|
85
|
|
|
$chains = $this->chains; |
|
86
|
|
|
$this->chains = []; |
|
87
|
|
|
foreach ($chains as $chain) { |
|
88
|
|
|
$chain->proceed(); |
|
89
|
|
|
} |
|
90
|
|
|
self::getLogger()->debug('Blocked chains have been flushed, {} item(s) was sent', [count($chains)]); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function clear() |
|
94
|
|
|
{ |
|
95
|
|
|
$this->chains = []; |
|
96
|
|
|
self::getLogger()->debug('Blocked chains have been cleared'); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @return BlockerInterceptorManager |
|
101
|
|
|
*/ |
|
102
|
|
|
public function manager() |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->manager; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|