Completed
Push — master ( eb0ddf...6e23e2 )
by Daniel
03:16
created

EventStream::isEmpty()   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
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace DDDominio\EventSourcing\Common;
4
5
use DDDominio\Common\EventInterface;
6
7
class EventStream implements EventStreamInterface
8
{
9
    /**
10
     * @var EventInterface[]
11
     */
12
    private $events;
13
14
    /**
15
     * @param EventInterface[] $events
16
     */
17 86
    public function __construct(array $events)
18
    {
19 86
        $this->events = $events;
20 86
    }
21
22
    /**
23
     * @return EventStream
24
     */
25 26
    public static function buildEmpty()
26
    {
27 26
        return new self([]);
28
    }
29
30
    /**
31
     * @param EventInterface|EventInterface[] $events
32
     * @return EventStream
33
     */
34 21
    public function append($events)
35
    {
36 21
        if (!is_array($events)) {
37 16
            $events = [$events];
38
        }
39 21
        return new self(array_merge($this->events(), $events));
40
    }
41
42
    /**
43
     * @return EventInterface[]
44
     */
45 34
    public function events()
46
    {
47 34
        return $this->events;
48
    }
49
50
    /**
51
     * @param int $offset
52
     * @return EventInterface
53
     * @throws \OutOfBoundsException
54
     */
55 31
    public function get($offset)
56
    {
57 31
        if (!isset($this->events[$offset])) {
58 2
            throw new \OutOfBoundsException();
59
        }
60 29
        return $this->events[$offset];
61
    }
62
63
    /**
64
     * @return EventInterface
65
     * @throws \OutOfBoundsException
66
     */
67 8
    public function last()
68
    {
69 8
        if ($this->isEmpty()) {
70 1
            throw new \OutOfBoundsException();
71
        }
72 7
        return end($this->events);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression end($this->events); of type DDDominio\Common\EventInterface|false adds false to the return on line 72 which is incompatible with the return type declared by the interface DDDominio\EventSourcing\...ntStreamInterface::last of type DDDominio\Common\EventInterface. It seems like you forgot to handle an error condition.
Loading history...
73
    }
74
75
    /**
76
     * @return bool
77
     */
78 18
    public function isEmpty()
79
    {
80 18
        return empty($this->events);
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86 48
    public function count()
87
    {
88 48
        return count($this->events);
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94 14
    public function getIterator()
95
    {
96 14
        return new \ArrayIterator($this->events());
97
    }
98
99
    /**
100
     * @param \Closure $closure
101
     * @return EventStream
102
     */
103 4
    public function filter(\Closure $closure)
104
    {
105 4
        return new self(array_values(array_filter($this->events, $closure)));
106
    }
107
108
    /**
109
     * @param \Closure $closure
110
     * @return EventStream
111
     */
112 44
    public function map(\Closure $closure)
113
    {
114 44
        return new self(array_map($closure, $this->events));
115
    }
116
117
    /**
118
     * @param int $offset
119
     * @param int|null $length
120
     * @return EventStream
121
     */
122 6
    public function slice($offset, $length = null)
123
    {
124 6
        return new self(array_slice($this->events, $offset, $length));
125
    }
126
}
127