Completed
Push — master ( 9bf5de...63a510 )
by Peter
22:57
created

QueueEventBus   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A publish() 0 4 1
A pullAndPublish() 0 6 2
A publishFromQueue() 0 12 3
A getRegisteredEventListeners() 0 4 1
1
<?php
2
/**
3
 * GpsLab component.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2016, Peter Gribanov
7
 * @license   http://opensource.org/licenses/MIT
8
 */
9
10
namespace GpsLab\Domain\Event\Bus;
11
12
use GpsLab\Domain\Event\Aggregator\AggregateEventsInterface;
13
use GpsLab\Domain\Event\EventInterface;
14
use GpsLab\Domain\Event\Listener\ListenerCollection;
15
use GpsLab\Domain\Event\Listener\ListenerInterface;
16
use GpsLab\Domain\Event\Queue\EventQueueInterface;
17
18
class QueueEventBus implements EventBusInterface
19
{
20
    /**
21
     * @var EventQueueInterface
22
     */
23
    private $queue;
24
25
    /**
26
     * @var EventBusInterface
27
     */
28
    private $publisher_bus;
29
30
    /**
31
     * @param EventQueueInterface $queue
32
     * @param EventBusInterface $publisher_bus
33
     */
34
    public function __construct(EventQueueInterface $queue, EventBusInterface $publisher_bus)
35
    {
36
        $this->queue = $queue;
37
        $this->publisher_bus = $publisher_bus;
38
    }
39
40
    /**
41
     * Publishes the event $event to every EventListener that wants to.
42
     *
43
     * @param EventInterface $event
44
     */
45
    public function publish(EventInterface $event)
46
    {
47
        $this->queue->push($event);
48
    }
49
50
    /**
51
     * @param AggregateEventsInterface $aggregator
52
     */
53
    public function pullAndPublish(AggregateEventsInterface $aggregator)
54
    {
55
        foreach ($aggregator->pullEvents() as $event) {
56
            $this->publish($event);
57
        }
58
    }
59
60
    /**
61
     * Publishes the events from event queue to the publisher bus.
62
     */
63
    public function publishFromQueue()
64
    {
65
        while (true) {
66
            $event = $this->queue->pop();
67
68
            if (!($event instanceof EventInterface)) { // it's a end of queue
69
                break;
70
            }
71
72
            $this->publisher_bus->publish($event);
73
        }
74
    }
75
76
    /**
77
     * @return ListenerInterface[]|ListenerCollection
78
     */
79
    public function getRegisteredEventListeners()
80
    {
81
        return $this->publisher_bus->getRegisteredEventListeners();
82
    }
83
}
84