AggregateEventIteratorStream::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 1
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 AggregateEventIteratorStream implements AggregateEventStream
19
{
20
    /**
21
     * @var \Iterator<AggregateEvent>
22
     */
23
    private $iterator;
24
25
    /**
26
     * AggregateEventIteratorStream constructor.
27
     *
28
     * @param \Iterator<mixed> $iterator
29
     */
30
    public function __construct(\Iterator $iterator)
31
    {
32
        // Prevents forward-only iterators such as \Generator
33
        $iterator->rewind();
34
35
        $this->iterator = $iterator;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     *
41
     * @return AggregateEvent
42
     */
43
    public function current(): AggregateEvent
44
    {
45
        /** @var mixed $event */
46
        $event = $this->iterator->current();
47
48
        if (!$event instanceof AggregateEvent) {
49
            throw new InvalidAggregateEventException(\sprintf(
50
                'Aggregate event stream only accepts "%s", "%s" given',
51
                AggregateEvent::class,
52
                \is_object($event) ? \get_class($event) : \gettype($event)
53
            ));
54
        }
55
56
        return $event;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function next(): void
63
    {
64
        $this->iterator->next();
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     *
70
     * @return string|int|null
71
     */
72
    public function key()
73
    {
74
        return $this->iterator->key();
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function valid(): bool
81
    {
82
        return $this->iterator->valid();
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function rewind(): void
89
    {
90
        $this->iterator->rewind();
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function count(): int
97
    {
98
        if ($this->iterator instanceof \EmptyIterator) {
99
            return 0;
100
        }
101
102
        if ($this->iterator instanceof \Countable) {
103
            return $this->iterator->count();
104
        }
105
106
        $currentKey = $this->iterator->key();
107
        $this->iterator->rewind();
108
109
        $count = 0;
110
        while ($this->iterator->valid()) {
111
            $count++;
112
113
            $this->iterator->next();
114
        }
115
116
        $this->iterator->rewind();
117
        while ($this->iterator->key() !== $currentKey) {
118
            $this->iterator->next();
119
        }
120
121
        return $count;
122
    }
123
}
124