StreamFeedIterator::rewind()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 22
ccs 13
cts 13
cp 1
rs 9.2
cc 3
eloc 12
nc 3
nop 0
crap 3
1
<?php
2
namespace EventStore\StreamFeed;
3
4
use ArrayIterator;
5
use EventStore\EventStoreInterface;
6
7
final class StreamFeedIterator implements \Iterator
8
{
9
    private $eventStore;
10
11
    private $streamName;
12
13
    private $feed;
14
15
    private $innerIterator;
16
17
    private $startingRelation;
18
19
    private $navigationRelation;
20
21
    private $arraySortingFunction;
22
23
    private $rewinded;
24
25 8
    private function __construct(
26
        EventStoreInterface $eventStore,
27
        $streamName,
28
        LinkRelation $startingRelation,
29
        LinkRelation $navigationRelation,
30
        callable $arraySortingFunction
31
    ) {
32 8
        $this->eventStore = $eventStore;
33 8
        $this->streamName = $streamName;
34 8
        $this->startingRelation = $startingRelation;
35 8
        $this->navigationRelation = $navigationRelation;
36 8
        $this->arraySortingFunction = $arraySortingFunction;
37 8
    }
38
39 4
    public static function forward(EventStoreInterface $eventStore, $streamName)
40
    {
41 4
        return new self(
42
            $eventStore,
43
            $streamName,
44 4
            LinkRelation::LAST(),
45 4
            LinkRelation::PREVIOUS(),
46 4
            'array_reverse'
47
        );
48
    }
49
50 4
    public static function backward(EventStoreInterface $eventStore, $streamName)
51
    {
52 4
        return new self(
53
            $eventStore,
54
            $streamName,
55 4
            LinkRelation::FIRST(),
56 4
            LinkRelation::NEXT(),
57
            function (array $array) {
58 3
                return $array;
59 4
            }
60
        );
61
    }
62
63 5
    public function current()
64
    {
65 5
        return $this->innerIterator->current();
66
    }
67
68 5
    public function next()
69
    {
70 5
        $this->rewinded = false;
71 5
        $this->innerIterator->next();
72
73 5
        if (!$this->innerIterator->valid()) {
74 5
            $this->feed = $this
75 5
                ->eventStore
76 5
                ->navigateStreamFeed(
77 5
                    $this->feed,
78 5
                    $this->navigationRelation
79
                )
80
            ;
81
82 5
            $this->createInnerIterator();
83
        }
84 5
    }
85
86 5
    public function key()
87
    {
88 5
        return $this->innerIterator->current()->getEntry()->getTitle();
89
    }
90
91 5
    public function valid()
92
    {
93 5
        return $this->innerIterator->valid();
94
    }
95
96 6
    public function rewind()
97
    {
98 6
        if ($this->rewinded) {
99 1
            return;
100
        }
101
102 6
        $this->feed = $this->eventStore->openStreamFeed($this->streamName);
103
104 6
        if ($this->feed->hasLink($this->startingRelation)) {
105 5
            $this->feed = $this
106 5
                ->eventStore
107 5
                ->navigateStreamFeed(
108 5
                    $this->feed,
109 5
                    $this->startingRelation
110
                )
111
            ;
112
        }
113
114 6
        $this->createInnerIterator();
115
116 6
        $this->rewinded = true;
117 6
    }
118
119 6
    private function createInnerIterator()
120
    {
121 6
        if (null !== $this->feed) {
122 6
            $entries = $this->feed->getEntries();
123
        } else {
124 2
            $entries = [];
125
        }
126
127 6
        if (empty($entries)) {
128 5
            $this->innerIterator = new ArrayIterator([]);
129
130 5
            return;
131
        }
132
133 6
        $entries = call_user_func(
134 6
            $this->arraySortingFunction,
135
            $entries
136
        );
137
138 6
        $urls = array_map(
139
            function ($entry) {
140 6
                return $entry->getEventUrl();
141 6
            },
142
            $entries
143
        );
144
145 6
        $this->innerIterator = new ArrayIterator(
146
            array_map(
147 6
                function ($entry, $event) {
148 6
                    return new EntryWithEvent(
149
                        $entry,
150
                        $event
151
                    );
152 6
                },
153
                $entries,
154 6
                $this->eventStore->readEventBatch($urls)
0 ignored issues
show
Bug introduced by
The method readEventBatch() does not exist on EventStore\EventStoreInterface. Did you maybe mean readEvent()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
155
            )
156
        );
157 6
    }
158
}
159