EventStream   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 0
dl 0
loc 155
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A iterator() 0 7 2
A add() 0 7 2
A asArray() 0 4 1
A count() 0 4 1
A isEmpty() 0 4 1
A first() 0 4 2
A last() 0 4 2
A current() 0 4 1
A next() 0 4 1
A key() 0 4 1
A valid() 0 4 1
A rewind() 0 4 1
1
<?php
2
3
/**
4
 * This file is part of slick/cqrs-tools
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\CQRSTools\Domain\Event;
11
12
use ArrayIterator;
13
use Slick\CQRSTools\Event;
14
15
/**
16
 * EventStream
17
 *
18
 * @package Slick\CQRSTools\Domain\Event
19
*/
20
final class EventStream implements Stream
21
{
22
    /**
23
     * @var Event[]
24
     */
25
    private $events = [];
26
27
    /**
28
     * @var ArrayIterator
29
     */
30
    private $iterator;
31
32
    /**
33
     * Creates an Event Stream
34
     *
35
     * @param array $events
36
     * @throws \ReflectionException
37
     */
38
    public function __construct(array $events = [])
39
    {
40
        foreach ($events as $event) {
41
            $this->add($event);
42
        }
43
    }
44
45
    /**
46
     * iterator
47
     *
48
     * @return ArrayIterator
49
     */
50
    private function iterator(): ArrayIterator
51
    {
52
        if (null === $this->iterator) {
53
            $this->iterator = new ArrayIterator($this->events);
54
        }
55
        return $this->iterator;
56
    }
57
58
    /**
59
     * Adds an event to the stream
60
     *
61
     * @param Event|StoredEvent $event
62
     *
63
     * @return EventStream
64
     * @throws \ReflectionException
65
     */
66
    public function add(Event $event): EventStream
67
    {
68
        $this->iterator = null;
69
        $event = $event instanceof StoredEvent ? $event->event() : $event;
70
        array_push($this->events, $event);
71
        return $this;
72
    }
73
74
    /**
75
     * Returns the event list as an array
76
     *
77
     * @return Event[]
78
     */
79
    public function asArray(): array
80
    {
81
        return $this->events;
82
    }
83
84
    /**
85
     * Count the events in this stream
86
     *
87
     * @return int
88
     */
89
    public function count()
90
    {
91
        return count($this->events);
92
    }
93
94
    /**
95
     * Returns true if this stream has no events
96
     *
97
     * @return bool
98
     */
99
    public function isEmpty(): bool
100
    {
101
        return empty($this->events);
102
    }
103
104
    /**
105
     * Returns the first event in stream or null if stream is empty
106
     *
107
     * @return null|Event
108
     */
109
    public function first(): ?Event
110
    {
111
        return reset($this->events) ?: null;
112
    }
113
114
    /**
115
     * Returns the first event in stream or null if stream is empty
116
     *
117
     * @return null|Event
118
     */
119
    public function last(): ?Event
120
    {
121
        return end($this->events) ?: null;
122
    }
123
124
    /**
125
     * Return the current element
126
     *
127
     * @return mixed Can return any type.
128
     */
129
    public function current()
130
    {
131
        return $this->iterator()->current();
132
    }
133
134
    /**
135
     * Move forward to next element
136
     *
137
     * @return void Any returned value is ignored.
138
     */
139
    public function next()
140
    {
141
        $this->iterator()->next();
142
    }
143
144
    /**
145
     * Return the key of the current element
146
     *
147
     * @return mixed scalar on success, or null on failure.
148
     */
149
    public function key()
150
    {
151
        return $this->iterator()->key();
152
    }
153
154
    /**
155
     * Checks if current position is valid
156
     *
157
     * @return boolean The return value will be casted to boolean and then evaluated.
158
     * Returns true on success or false on failure.
159
     */
160
    public function valid()
161
    {
162
        return $this->iterator()->valid();
163
    }
164
165
    /**
166
     * Rewind the Iterator to the first element
167
     *
168
     * @return void Any returned value is ignored.
169
     */
170
    public function rewind()
171
    {
172
        $this->iterator()->rewind();
173
    }
174
}
175