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

EventStreamViaPersistentSubscriptionIterator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 83
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 2
Metric Value
wmc 9
lcom 1
cbo 2
dl 83
loc 83
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A current() 4 4 1
A next() 15 15 3
A newEventsIterator() 8 8 1
A key() 4 4 1
A valid() 4 4 1
A rewind() 5 5 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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