WritableEventCollection::validateEvents()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 3
eloc 4
nc 3
nop 1
crap 3
1
<?php
2
namespace EventStore;
3
4
use EventStore\Exception\InvalidWritableEventObjectException;
5
6
/**
7
 * Class WritableEventCollection
8
 * @package EventStore
9
 */
10
final class WritableEventCollection implements WritableToStream
11
{
12
    /**
13
     * @var WritableEvent[]
14
     */
15
    private $events = [];
16
17
    /**
18
     * @param array $events
19
     */
20 24
    public function __construct(array $events)
21
    {
22 24
        $this->validateEvents($events);
23 23
        $this->events = $events;
24 23
    }
25
26
    /**
27
     * @return array
28
     */
29
    public function toStreamData()
30
    {
31 23
        return array_map(function (WritableEvent $event) {
32 23
            return $event->toStreamData();
33 23
        }, $this->events);
34
    }
35
36
    /**
37
     * @param array $events
38
     */
39 24
    private function validateEvents(array $events)
40
    {
41 24
        foreach ($events as $event) {
42 24
            if (!$event instanceof WritableEvent) {
43 24
                throw new InvalidWritableEventObjectException();
44
            }
45
        }
46 23
    }
47
}
48