TransactionalEmitter::emitBatch()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace RemiSan\TransactionManager\Event;
4
5
use League\Event\EmitterInterface;
6
use League\Event\Event;
7
use League\Event\EventInterface;
8
use League\Event\GeneratorInterface;
9
use League\Event\ListenerProviderInterface;
10
use RemiSan\TransactionManager\Exception\TransactionException;
11
use RemiSan\TransactionManager\Transactional;
12
13
final class TransactionalEmitter implements EmitterInterface, Transactional
14
{
15
    /** @var EmitterInterface */
16
    private $emitter;
17
18
    /** @var array */
19
    private $events = [];
20
21
    /** @var bool */
22
    private $running;
23
24
    /**
25
     * Constructor.
26
     *
27
     * @param EmitterInterface $emitter
28
     */
29 45
    public function __construct(EmitterInterface $emitter)
30
    {
31 45
        $this->emitter = $emitter;
32 45
        $this->events = [];
33 45
        $this->running = false;
34 45
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 21
    public function emit($event)
40
    {
41 21
        if (!$this->running) {
42 3
            throw new TransactionException('Cannot emit outside a transaction');
43
        }
44
45 18
        return $this->addEvent($event);
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 6
    public function emitBatch(array $events)
52
    {
53 6
        $results = [];
54
55 6
        foreach ($events as $event) {
56 6
            $results[] = $this->emit($event);
57 6
        }
58
59 6
        return $results;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 3
    public function emitGeneratedEvents(GeneratorInterface $generator)
66
    {
67 3
        $events = $generator->releaseEvents();
68
69 3
        return $this->emitBatch($events);
70
    }
71
72
    /**
73
     * @param $event
74
     *
75
     * @return EventInterface
76
     */
77 18
    private function addEvent($event)
78
    {
79 18
        if (is_string($event)) {
80 3
            $event = Event::named($event);
81 3
        }
82
83 18
        if (!$event instanceof EventInterface) {
84 3
            throw new \InvalidArgumentException(
85 3
                'Events should be provides as Event instances or string, received type: ' . gettype($event)
86 3
            );
87
        }
88
89 15
        $this->events[] = $event;
90
91 15
        return $event;
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97 18
    public function beginTransaction()
98
    {
99 18
        $this->running = true;
100 18
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105 12
    public function commit()
106
    {
107 12
        foreach ($this->events as $event) {
108 12
            $this->emitter->emit($event);
109 12
        }
110
111 12
        $this->events = [];
112 12
        $this->running = false;
113 12
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118 3
    public function rollback()
119
    {
120 3
        $this->events = [];
121 3
        $this->running = false;
122 3
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127 3
    public function removeListener($event, $listener)
128
    {
129 3
        $this->emitter->removeListener($event, $listener);
130
131 3
        return $this;
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137 3
    public function useListenerProvider(ListenerProviderInterface $provider)
138
    {
139 3
        $this->emitter->useListenerProvider($provider);
140
141 3
        return $this;
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147 3
    public function removeAllListeners($event)
148
    {
149 3
        $this->emitter->removeAllListeners($event);
150
151 3
        return $this;
152
    }
153
154
    /**
155
     * {@inheritdoc}
156
     */
157 3
    public function hasListeners($event)
158
    {
159 3
        return $this->emitter->hasListeners($event);
160
    }
161
162
    /**
163
     * {@inheritdoc}
164
     */
165 3
    public function getListeners($event)
166
    {
167 3
        return $this->emitter->getListeners($event);
168
    }
169
170
    /**
171
     * {@inheritdoc}
172
     */
173 3
    public function addListener($event, $listener, $priority = self::P_NORMAL)
174
    {
175 3
        $this->emitter->addListener($event, $listener, $priority);
176
177 3
        return $this;
178
    }
179
180
    /**
181
     * {@inheritdoc}
182
     */
183 3
    public function addOneTimeListener($event, $listener, $priority = self::P_NORMAL)
184
    {
185 3
        $this->emitter->addOneTimeListener($event, $listener, $priority);
186
187 3
        return $this;
188
    }
189
}
190