1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HMLB\DDDBundle\MessageBus\Middleware; |
4
|
|
|
|
5
|
|
|
use HMLB\DDD\Exception\Exception; |
6
|
|
|
use HMLB\DDDBundle\EventListener\CollectsEventsFromEntities; |
7
|
|
|
use Psr\Log\LoggerInterface; |
8
|
|
|
use SimpleBus\Message\Bus\Middleware\MessageBusMiddleware; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* This subscribers throws an exception if DomainEvents have been collected and will not be notified. |
12
|
|
|
* |
13
|
|
|
* It should have High priority to check for Events after all Notifiers like "NotifiesDomainEvents" have been called. |
14
|
|
|
* |
15
|
|
|
* @author Hugues Maignol <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class MonitorsDomainEvents implements MessageBusMiddleware |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var CollectsEventsFromEntities |
21
|
|
|
*/ |
22
|
|
|
private $collectsEventsFromEntities; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var LoggerInterface |
26
|
|
|
*/ |
27
|
|
|
private $logger; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param LoggerInterface $logger |
31
|
|
|
* @param CollectsEventsFromEntities $collectsEventsFromEntities |
32
|
|
|
*/ |
33
|
|
|
public function __construct( |
34
|
|
|
LoggerInterface $logger, |
35
|
|
|
CollectsEventsFromEntities $collectsEventsFromEntities |
36
|
|
|
) { |
37
|
|
|
$this->logger = $logger; |
38
|
|
|
$this->collectsEventsFromEntities = $collectsEventsFromEntities; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param object $message |
43
|
|
|
* @param callable $next |
44
|
|
|
* |
45
|
|
|
* @throws Exception |
46
|
|
|
*/ |
47
|
|
|
public function handle($message, callable $next) |
48
|
|
|
{ |
49
|
|
|
$next($message); |
50
|
|
|
$events = $this->collectsEventsFromEntities->recordedMessages(); |
51
|
|
|
$count = count($events); |
52
|
|
|
$this->logger->debug('MonitorsDomainEvents saw '.$count.' events'); |
53
|
|
|
|
54
|
|
|
if ($count) { |
55
|
|
|
foreach ($events as $event) { |
56
|
|
|
$this->logger->debug('MonitorsDomainEvents saw '.get_class($event)); |
57
|
|
|
} |
58
|
|
|
if ($count == 1) { |
59
|
|
|
$message = sprintf( |
60
|
|
|
'%s Domain Event has been collected but will not be notified. |
61
|
|
|
You should not raise domain event during the handling of a Event message but only for a Command.', |
62
|
|
|
get_class(array_pop($events)) |
63
|
|
|
); |
64
|
|
|
} else { |
65
|
|
|
$message = sprintf( |
66
|
|
|
'%s Domain Event(s) have been collected but will not be notified. |
67
|
|
|
You should not raise domain event during the handling of a Event message but only for a Command.', |
68
|
|
|
$count |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
throw new Exception($message); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|