Completed
Push — master ( 6dbf1b...fb879b )
by Iqbal
02:51
created

EventBus::dispatchNow()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 1
1
<?php
2
/*
3
 * This file is part of the Borobudur-Cqrs package.
4
 *
5
 * (c) Hexacodelabs <http://hexacodelabs.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Borobudur\Cqrs\Bus;
12
13
use Borobudur\Bus\Bus;
14
use Borobudur\Bus\Exception\LogicException;
15
use Borobudur\Bus\Message\MessageInterface;
16
use Borobudur\Cqrs\Exception\InvalidArgumentException;
17
use Borobudur\Cqrs\Message\AbstractDomainEvent;
18
use Borobudur\EventDispatcher\EventDispatcher;
19
use Closure;
20
21
/**
22
 * @author      Iqbal Maulana <[email protected]>
23
 * @created     8/18/15
24
 */
25
class EventBus extends Bus
26
{
27
    const BUS_NAME = 'bus.event';
28
29
    /**
30
     * @var EventDispatcher
31
     */
32
    protected $dispatcher;
33
34
    /**
35
     * @var array
36
     */
37
    protected $responses = array();
38
39
    /**
40
     * Constructor.
41
     *
42
     * @param EventDispatcher|null $dispatcher
43
     */
44
    public function __construct(EventDispatcher $dispatcher = null)
45
    {
46
        $this->dispatcher = $dispatcher ?: new EventDispatcher();
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function getName()
53
    {
54
        return EventBus::BUS_NAME;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function registers(array $events)
61
    {
62
        foreach ($events as $event => $handler) {
63
            if (is_array($handler)) {
64
                foreach ($handler as $h) {
65
                    $this->register($event, $h);
66
                }
67
            } else {
68
                $this->register($event, $handler);
69
            }
70
        }
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function register($event, $handler)
77
    {
78
        $name = ltrim($event, '\\');
79
        $this->messages[$name][] = $handler;
80
        $this->dispatcher->addListener($name, $this->getClosureHandler($handler, $name));
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function dispatchNow(MessageInterface $event)
87
    {
88
        if (!$event instanceof AbstractDomainEvent) {
89
            throw new InvalidArgumentException(sprintf(
90
                'Domain event "%s" should implement \Borobudur\Cqrs\Message\AbstractDomainEvent',
91
                get_class($event)
92
            ));
93
        }
94
95
        $this->responses = array();
96
        $this->dispatcher->fireEvent(ltrim(get_class($event), '\\'), $event);
97
98
        return $this->responses;
99
    }
100
101
    /**
102
     * Get event handler.
103
     *
104
     * @param mixed  $handler
105
     * @param string $class
106
     *
107
     * @return Closure
108
     */
109
    protected function getClosureHandler($handler, $class)
110
    {
111
        return function (MessageInterface $message) use ($handler, $class) {
112
            $class = ltrim(get_class($message), '\\');
113
            if (!isset($this->messages[$class])) {
114
                throw new LogicException(sprintf('Message "%s" does not have handler.', $class));
115
            }
116
117
            if (null !== ($response = $this->invoke($message, $handler, $class))) {
118
                $this->responses[] = $response;
119
            }
120
121
            return $response;
122
        };
123
    }
124
}
125