Completed
Push — master ( bbb956...f4fb75 )
by Julián
01:28 queued 11s
created

EventBehaviour   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 0
dl 0
loc 45
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A recordEvent() 0 4 1
A getRecordedEvents() 0 4 1
A clearRecordedEvents() 0 4 1
A collectRecordedEvents() 0 8 1
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\EventArrayCollection;
18
use Gears\Event\EventCollection;
19
20
/**
21
 * Recorded events trait.
22
 */
23
trait EventBehaviour
24
{
25
    /**
26
     * @var Event[]
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
        $this->recordedEvents[] = $event;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    final public function getRecordedEvents(): EventCollection
44
    {
45
        return new EventArrayCollection($this->recordedEvents);
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    final public function clearRecordedEvents(): void
52
    {
53
        $this->recordedEvents = [];
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    final public function collectRecordedEvents(): EventCollection
60
    {
61
        $events = new EventArrayCollection($this->recordedEvents);
62
63
        $this->recordedEvents = [];
64
65
        return $events;
66
    }
67
}
68