MonitorsDomainEvents   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
c 2
b 0
f 0
lcom 1
cbo 3
dl 0
loc 58
rs 10

2 Methods

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