EventTrap::getFirstEvent()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 2
eloc 2
nc 2
nop 0
crap 2
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 3
    public function __construct($oneTime = true)
24
    {
25 3
        $this->oneTime = $oneTime;
26 3
        $this->trappedEvents = [];
27 3
    }
28
29
30
    /**
31
     * @param object $event
32
     * @param \Mleko\Narrator\Meta $meta
33
     */
34 3
    public function handle($event, \Mleko\Narrator\Meta $meta)
35
    {
36 3
        if ($this->oneTime) {
37 2
            $this->close($meta);
38 2
            if (!empty($this->trappedEvents)) {
39 1
                return;
40
            }
41 2
        }
42 3
        $this->trappedEvents[] = $event;
43 3
    }
44
45
    /**
46
     * @return object[]
47
     */
48 3
    public function getTrappedEvents()
49
    {
50 3
        return $this->trappedEvents;
51
    }
52
53
    /**
54
     * @return null|object
55
     */
56 2
    public function getFirstEvent()
57
    {
58 2
        return isset($this->trappedEvents[0]) ? $this->trappedEvents[0] : null;
59
    }
60
61
    /**
62
     * @param \Mleko\Narrator\Meta $meta
63
     * @return bool
64
     */
65 2
    private function close(\Mleko\Narrator\Meta $meta)
66
    {
67 2
        return $meta->getEventSource()->unsubscribe($meta->getMatchedName(), $this);
68
    }
69
70
}
71