Completed
Push — master ( 334adc...a3ab94 )
by Rémi
03:43
created

TransactionalEmitter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
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\GeneratorInterface;
8
use League\Event\ListenerProviderInterface;
9
use RemiSan\TransactionManager\Exception\TransactionException;
10
use RemiSan\TransactionManager\Transactional;
11
12
final class TransactionalEmitter implements EmitterInterface, Transactional
13
{
14
    /** @var EmitterInterface */
15
    private $emitter;
16
17
    /** @var array */
18
    private $events = [];
19
20
    /** @var bool */
21
    private $running;
22
23
    /**
24
     * Constructor.
25
     *
26
     * @param EmitterInterface $emitter
27
     */
28
    public function __construct(EmitterInterface $emitter)
29
    {
30
        $this->emitter = $emitter;
31
        $this->events = [];
32
        $this->running = false;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function emit($event)
39
    {
40
        if (!$this->running) {
41
            throw new TransactionException('Cannot emit outside a transaction');
42
        }
43
44
        $this->addEvent($event);
0 ignored issues
show
Documentation introduced by
$event is of type string|object<League\Event\EventInterface>, but the function expects a object<League\Event\Event>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
45
46
        return $event;
0 ignored issues
show
Bug Compatibility introduced by
The expression return $event; of type string|League\Event\EventInterface is incompatible with the return type declared by the interface League\Event\EmitterInterface::emit of type League\Event\EventInterface as it can also be of type string which is not included in this return type.
Loading history...
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function beginTransaction()
53
    {
54
        $this->events = [];
55
        $this->running = true;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function commit()
62
    {
63
        foreach ($this->events as $event) {
64
            $this->emitter->emit($event);
65
        }
66
67
        $this->events = [];
68
        $this->running = false;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function rollback()
75
    {
76
        $this->events = [];
77
        $this->running = false;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function removeListener($event, $listener)
84
    {
85
        $this->emitter->removeListener($event, $listener);
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function useListenerProvider(ListenerProviderInterface $provider)
92
    {
93
        $this->emitter->useListenerProvider($provider);
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function removeAllListeners($event)
100
    {
101
        $this->emitter->removeAllListeners($event);
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    public function hasListeners($event)
108
    {
109
        $this->emitter->hasListeners($event);
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115
    public function getListeners($event)
116
    {
117
        $this->emitter->getListeners($event);
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123
    public function emitBatch(array $events)
124
    {
125
        $results = [];
126
127
        foreach ($events as $event) {
128
            $results[] = $this->emit($event);
129
        }
130
131
        return $results;
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137
    public function emitGeneratedEvents(GeneratorInterface $generator)
138
    {
139
        $this->emitter->emitGeneratedEvents($generator);
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145
    public function addListener($event, $listener, $priority = self::P_NORMAL)
146
    {
147
        $this->emitter->addListener($event, $listener, $priority);
148
    }
149
150
    /**
151
     * {@inheritdoc}
152
     */
153
    public function addOneTimeListener($event, $listener, $priority = self::P_NORMAL)
154
    {
155
        $this->emitter->addOneTimeListener($event, $listener, $priority);
156
    }
157
158
    /**
159
     * @param Event $event
160
     */
161
    private function addEvent(Event $event)
162
    {
163
        $this->events[] = $event;
164
    }
165
}
166