EventBehaviour::recordEvent()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 2
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
/*
4
 * aggregate (https://github.com/phpgears/aggregate).
5
 * Aggregate base.
6
 *
7
 * @license MIT
8
 * @link https://github.com/phpgears/aggregate
9
 * @author Julián Gutiérrez <[email protected]>
10
 */
11
12
declare(strict_types=1);
13
14
namespace Gears\Aggregate;
15
16
use Gears\Event\Event;
17
use Gears\Event\EventCollection;
18
use Gears\Event\EventIteratorCollection;
19
20
/**
21
 * Recorded events trait.
22
 */
23
trait EventBehaviour
24
{
25
    /**
26
     * @var \ArrayObject<string, Event>|null
27
     */
28
    private $recordedEvents;
29
30
    /**
31
     * Record event.
32
     *
33
     * @param Event $event
34
     */
35
    final protected function recordEvent(Event $event): void
36
    {
37
        if ($this->recordedEvents === null) {
38
            $this->recordedEvents = new \ArrayObject();
39
        }
40
41
        $this->recordedEvents->append($event);
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    final public function getRecordedEvents(): EventCollection
48
    {
49
        return new EventIteratorCollection(
50
            $this->recordedEvents !== null ? $this->recordedEvents->getIterator() : new \EmptyIterator()
51
        );
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    final public function clearRecordedEvents(): void
58
    {
59
        $this->recordedEvents = null;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    final public function collectRecordedEvents(): EventCollection
66
    {
67
        $events = new EventIteratorCollection(
68
            $this->recordedEvents !== null ? $this->recordedEvents->getIterator() : new \EmptyIterator()
69
        );
70
71
        $this->recordedEvents = null;
72
73
        return $events;
74
    }
75
}
76