Completed
Push — batch-iterator ( dd6368...bfc1fc )
by Davide
01:59
created

StreamFeedIterator::nextUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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
    private $pagesLeft;
26
27 10
    private function __construct(
28
        EventStoreInterface $eventStore,
29
        $streamName,
30
        LinkRelation $startingRelation,
31
        LinkRelation $navigationRelation,
32
        callable $arraySortingFunction,
33
        $limit
34
    )
35
    {
36 10
        $this->eventStore = $eventStore;
37 10
        $this->streamName = $streamName;
38 10
        $this->startingRelation = $startingRelation;
39 10
        $this->navigationRelation = $navigationRelation;
40 10
        $this->arraySortingFunction = $arraySortingFunction;
41 10
        $this->pagesLeft = $limit;
42 10
    }
43
44 4
    public static function forward(EventStoreInterface $eventStore, $streamName, $limit = PHP_INT_MAX)
45
    {
46 4
        return new self(
47 4
            $eventStore,
48 4
            $streamName,
49 4
            LinkRelation::LAST(),
50 4
            LinkRelation::PREVIOUS(),
51 4
            'array_reverse',
52
            $limit
53 4
        );
54
    }
55
56 6
    public static function backward(EventStoreInterface $eventStore, $streamName, $limit = PHP_INT_MAX)
57
    {
58 6
        return new self(
59 6
            $eventStore,
60 6
            $streamName,
61 6
            LinkRelation::FIRST(),
62 6
            LinkRelation::NEXT(),
63
            function (array $array) { return $array; },
64
            $limit
65 6
        );
66
    }
67
68 7
    public function current()
69
    {
70 7
        return $this->innerIterator->current();
71
    }
72
73 7
    public function next()
74
    {
75 7
        $this->rewinded = false;
76 7
        $this->innerIterator->next();
77
78 7
        if (!$this->innerIterator->valid() && !$this->limitReached()) {
79 7
            $this->feed = $this
80
                ->eventStore
81 7
                ->navigateStreamFeed(
82 7
                    $this->feed,
83 7
                    $this->navigationRelation
84 7
                )
85
            ;
86
87 7
            $this->createInnerIterator();
88 7
        }
89 7
    }
90
91 7
    public function key()
92
    {
93 7
        return $this->innerIterator->current()->getEntry()->getTitle();
94
    }
95
96 7
    public function valid()
97
    {
98 7
        return $this->innerIterator->valid();
99
    }
100
101 8
    public function rewind()
102
    {
103 8
        if ($this->rewinded) {
104 1
            return;
105
        }
106
107 8
        $this->feed = $this->eventStore->openStreamFeed($this->streamName);
108
109 8
        if ($this->feed->hasLink($this->startingRelation)) {
110 7
            $this->feed = $this
111
                ->eventStore
112 7
                ->navigateStreamFeed(
113 7
                    $this->feed,
114 7
                    $this->startingRelation
115 7
                )
116
            ;
117 7
        }
118
119 8
        $this->createInnerIterator();
120
121 8
        $this->rewinded = true;
122 8
    }
123
124 1
    public function nextUrl()
125
    {
126 1
        return $this->feed->getLinkUrl($this->navigationRelation);
127
    }
128
129 8
    private function createInnerIterator()
130
    {
131 8
        if (null !== $this->feed) {
132 8
            $entries = $this->feed->getEntries();
133 8
        } else {
134 2
            $entries = [];
135
        }
136
137 8
        if (empty($entries)) {
138 5
            $this->innerIterator = new ArrayIterator([]);
139
140 5
            return;
141
        }
142
143 8
        $entries = call_user_func(
144 8
            $this->arraySortingFunction,
145
            $entries
146 8
        );
147
148 8
        $urls = array_map(
149
            function ($entry) {
150 8
                return $entry->getEventUrl();
151 8
            },
152
            $entries
153 8
        );
154
155 8
        $this->innerIterator = new ArrayIterator(
156 8
            array_map(
157 8
                function ($entry, $event) {
158 8
                    return new EntryWithEvent(
159 8
                        $entry,
160
                        $event
161 8
                    );
162 8
                },
163 8
                $entries,
164 8
                $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...
165 8
            )
166 8
        );
167 8
    }
168
169 7
    private function limitReached()
170
    {
171 7
        return --$this->pagesLeft <= 0;
172
    }
173
}
174