Completed
Push — master ( 23e886...768f8d )
by Julián
01:24
created

AggregateEventArrayStream::next()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * event-sourcing (https://github.com/phpgears/event-sourcing).
5
 * Event Sourcing base.
6
 *
7
 * @license MIT
8
 * @link https://github.com/phpgears/event-sourcing
9
 * @author Julián Gutiérrez <[email protected]>
10
 */
11
12
declare(strict_types=1);
13
14
namespace Gears\EventSourcing\Event;
15
16
use Gears\EventSourcing\Event\Exception\InvalidAggregateEventException;
17
18
final class AggregateEventArrayStream implements AggregateEventStream
19
{
20
    /**
21
     * @var AggregateEvent[]
22
     */
23
    private $events = [];
24
25
    /**
26
     * AggregateEventArrayStream constructor.
27
     *
28
     * @param (AggregateEvent|mixed)[] $events
29
     *
30
     * @throws InvalidAggregateEventException
31
     */
32
    public function __construct(array $events)
33
    {
34
        foreach ($events as $event) {
35
            if (!$event instanceof AggregateEvent) {
36
                throw new InvalidAggregateEventException(\sprintf(
37
                    'Aggregate event stream only accepts %s, %s given',
38
                    AggregateEvent::class,
39
                    \is_object($event) ? \get_class($event) : \gettype($event)
40
                ));
41
            }
42
43
            $this->events[] = $event;
44
        }
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     *
50
     * @return AggregateEvent
51
     */
52
    public function current(): AggregateEvent
53
    {
54
        return \current($this->events);
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function next(): void
61
    {
62
        \next($this->events);
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     *
68
     * @return string|int|null
69
     */
70
    public function key()
71
    {
72
        return \key($this->events);
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function valid(): bool
79
    {
80
        return \key($this->events) !== null;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function rewind(): void
87
    {
88
        \reset($this->events);
89
    }
90
}
91