Completed
Push — master ( 9e8331...bb86fc )
by Constantin
03:22
created

RawEventStream::fetchCommits()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 18
c 0
b 0
f 0
ccs 9
cts 9
cp 1
rs 8.8571
cc 5
eloc 9
nc 8
nop 0
crap 5
1
<?php
2
3
4
namespace Gica\Cqrs\EventStore\InMemory;
5
6
7
use Gica\Cqrs\EventStore\ByClassNamesEventStream;
8
use Gica\Iterator\IteratorTransformer\IteratorExpander;
9
10
class RawEventStream implements ByClassNamesEventStream
11
{
12
13
    private $groupedEventsArray = [];
14
15
    /** @var int|null */
16
    private $limit;
17
18
    /** @var  int|null */
19
    private $skip;
20
21 3
    public function __construct($groupedEventsArray)
22
    {
23 3
        $this->groupedEventsArray = $groupedEventsArray;
24 3
    }
25
26 2
    public function getIterator()
27
    {
28 2
        $groupedEvents = $this->fetchCommits();
29
30 2
        $deGrouper = new IteratorExpander(function ($group) {
31 2
            foreach ($group as $event) {
32 2
                yield $event;
33
            }
34 2
        });
35
36 2
        $events = iterator_to_array($deGrouper($groupedEvents));
37
38 2
        return new \ArrayIterator($events);
39
    }
40
41
    /**
42
     * @return array|\ArrayIterator
43
     */
44 2
    public function fetchCommits()
45
    {
46 2
        $groupedEvents = $this->groupedEventsArray;
47
48 2
        if ($this->groupedEventsArray instanceof \Iterator || $this->groupedEventsArray instanceof \IteratorAggregate) {
49 1
            $groupedEvents = iterator_to_array($this->groupedEventsArray);
50
        }
51
52 2
        if ($this->skip) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->skip of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
53 1
            $groupedEvents = array_slice($groupedEvents, $this->skip);
54
        }
55
56 2
        if ($this->limit) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->limit of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
57 1
            $groupedEvents = array_slice($groupedEvents, 0, $this->limit);
58
        }
59
60 2
        return $groupedEvents;
61
    }
62
63 1
    public function limitCommits(int $limit)
64
    {
65 1
        $this->limit = $limit;
66 1
    }
67
68 1
    public function skipCommits(int $numberOfCommits)
69
    {
70 1
        $this->skip = $numberOfCommits;
71 1
    }
72
73 1
    public function countCommits(): int
74
    {
75 1
        return count($this->groupedEventsArray);
76
    }
77
}