DomainEventsPublicationMiddleware   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 27
c 0
b 0
f 0
wmc 2
lcom 1
cbo 3
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A execute() 0 17 1
1
<?php
2
3
/*
4
 * This file is part of the Shared Kernel library.
5
 *
6
 * Copyright (c) 2016-present LIN3S <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace LIN3S\SharedKernel\Infrastructure\Application\Tactician\Middlewares;
15
16
use League\Tactician\Middleware;
17
use LIN3S\SharedKernel\Application\EventBus;
18
use LIN3S\SharedKernel\Domain\Event\CollectInMemoryDomainEventsSubscriber;
19
use LIN3S\SharedKernel\Domain\Event\DomainEventPublisher;
20
use LIN3S\SharedKernel\Domain\Model\PublishableDomainEvent;
21
22
/**
23
 * @author Beñat Espiña <[email protected]>
24
 */
25
class DomainEventsPublicationMiddleware implements Middleware
26
{
27
    private $eventBus;
28
29
    public function __construct(EventBus $eventBus)
30
    {
31
        $this->eventBus = $eventBus;
32
    }
33
34
    public function execute($command, callable $next)
35
    {
36
        $returnValue = $next($command);
37
38
        $collectDomainEventsSubscriber = DomainEventPublisher::instance()->subscriberOfClassName(
39
            CollectInMemoryDomainEventsSubscriber::class
40
        );
41
        $publishableEvents = $collectDomainEventsSubscriber->events();
42
43
        $domainEvents = array_map(function (PublishableDomainEvent $publishableDomainEvent) {
44
            return $publishableDomainEvent->event();
45
        }, $publishableEvents);
46
47
        $this->eventBus->publish(...$domainEvents);
48
49
        return $returnValue;
50
    }
51
}
52