Completed
Push — master ( 4ebf8e...9e8331 )
by Constantin
03:08
created

RawEventStream::countCommits()   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 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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->groupedEventsArray;
29
30 2
        if ($this->groupedEventsArray instanceof \Iterator || $this->groupedEventsArray instanceof \IteratorAggregate) {
31 1
            $groupedEvents = iterator_to_array($this->groupedEventsArray);
32
        }
33
34 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...
35 1
            $groupedEvents = array_slice($groupedEvents, $this->skip);
36
        }
37
38 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...
39 1
            $groupedEvents = array_slice($groupedEvents, 0, $this->limit);
40
        }
41
42 2
        $deGrouper = new IteratorExpander(function ($group) {
43 2
            foreach ($group as $event) {
44 2
                yield $event;
45
            }
46 2
        });
47
48 2
        $events = iterator_to_array($deGrouper($groupedEvents));
49
50 2
        return new \ArrayIterator($events);
51
    }
52
53 1
    public function limitCommits(int $limit)
54
    {
55 1
        $this->limit = $limit;
56 1
    }
57
58 1
    public function skipCommits(int $numberOfCommits)
59
    {
60 1
        $this->skip = $numberOfCommits;
61 1
    }
62
63 1
    public function countCommits():int
64
    {
65 1
        return count($this->groupedEventsArray);
66
    }
67
}