Completed
Pull Request — master (#1)
by Raymond
02:20
created

current()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %
Metric Value
dl 4
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace RayRutjes\GetEventStore\Client\Http\Feed;
4
5
use RayRutjes\GetEventStore\EventRecord;
6
7 View Code Duplication
final class EventStreamViaPersistentSubscriptionIterator implements \Iterator
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
8
{
9
    /**
10
     * @var EventStreamViaPersistentSubscriptionFeedIterator
11
     */
12
    private $feedIterator;
13
14
    /**
15
     * @var \ArrayIterator
16
     */
17
    private $eventsIterator;
18
19
    /**
20
     * @var int
21
     */
22
    private $currentKey;
23
24
    /**
25
     * @param EventStreamViaPersistentSubscriptionFeedIterator $feedIterator
26
     */
27
    public function __construct(EventStreamViaPersistentSubscriptionFeedIterator $feedIterator)
28
    {
29
        $this->feedIterator = $feedIterator;
30
    }
31
32
    /**
33
     * @return EventRecord
34
     */
35
    public function current(): EventRecord
36
    {
37
        return $this->eventsIterator->current();
38
    }
39
40
    public function next()
41
    {
42
        $this->eventsIterator->next();
43
        $this->currentKey++;
44
        if ($this->eventsIterator->valid()) {
45
            return;
46
        }
47
48
        $this->feedIterator->next();
49
        if ($this->feedIterator->valid()) {
50
            $this->eventsIterator = $this->newEventsIterator();
51
        } else {
52
            $this->feedIterator = null;
53
        }
54
    }
55
56
    /**
57
     * @return \ArrayIterator
58
     */
59
    private function newEventsIterator()
60
    {
61
        $events = $this->feedIterator->current()->getEvents();
62
63
        $events = array_reverse($events);
64
65
        return new \ArrayIterator($events);
66
    }
67
68
    /**
69
     * @return int
70
     */
71
    public function key()
72
    {
73
        return $this->currentKey;
74
    }
75
76
    /**
77
     * @return bool
78
     */
79
    public function valid(): bool
80
    {
81
        return $this->eventsIterator->valid();
82
    }
83
84
    public function rewind()
85
    {
86
        $this->currentKey = 0;
87
        $this->eventsIterator = $this->newEventsIterator();
88
    }
89
}
90