Completed
Push — master ( 4d3228...6108f5 )
by Daniel
01:58
created

EventTrap::close()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * @package narrator
4
 */
5
6
7
namespace Mleko\Narrator\Listener;
8
9
10
class EventTrap implements \Mleko\Narrator\Listener
11
{
12
13
    /** @var object[] */
14
    private $trappedEvents;
15
16
    /** @var boolean */
17
    private $oneTime;
18
19
    /**
20
     * EventTrap constructor.
21
     * @param bool $oneTime
22
     */
23 2
    public function __construct($oneTime = true)
24
    {
25 2
        $this->oneTime = $oneTime;
26 2
        $this->trappedEvents = [];
27 2
    }
28
29
30
    /**
31
     * @param object $event
32
     * @param \Mleko\Narrator\Meta $meta
33
     */
34 2
    public function handle($event, \Mleko\Narrator\Meta $meta)
35
    {
36 2
        if ($this->oneTime) {
37 1
            $this->close($meta);
38 1
            if (!empty($this->trappedEvents)) {
39
                return;
40
            }
41 1
        }
42 2
        $this->trappedEvents[] = $event;
43 2
    }
44
45
    /**
46
     * @return object[]
47
     */
48 2
    public function getTrappedEvents()
49
    {
50 2
        return $this->trappedEvents;
51
    }
52
53
    /**
54
     * @return null|object
55
     */
56 1
    public function getFirstEvent()
57
    {
58 1
        return isset($this->trappedEvents[0]) ? $this->trappedEvents[0] : null;
59
    }
60
61
    /**
62
     * @param \Mleko\Narrator\Meta $meta
63
     * @return bool
64
     */
65 1
    public function close(\Mleko\Narrator\Meta $meta)
66
    {
67 1
        return $meta->getEventSource()->unsubscribe($meta->getMatchedName(), $this);
68
    }
69
70
}
71