StreamFeedIterator   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 7
Bugs 2 Features 2
Metric Value
wmc 14
c 7
b 2
f 2
lcom 1
cbo 4
dl 0
loc 152
ccs 66
cts 66
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A forward() 0 10 1
A current() 0 4 1
A key() 0 4 1
A valid() 0 4 1
A backward() 0 12 1
A next() 0 17 2
A rewind() 0 22 3
B createInnerIterator() 0 39 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