Completed
Push — master ( bfde3c...df2f00 )
by Beñat
01:59
created

DomainEventsPublicationMiddleware::streamVersion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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