EventsCommit::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 3
crap 1
1
<?php
2
3
4
namespace Gica\Cqrs\EventStore;
5
6
7
use Gica\CodeAnalysis\Shared\ClassComparison\SubclassComparator;
8
use Gica\Cqrs\Event\EventWithMetaData;
9
10
class EventsCommit
11
{
12
13
    /**
14
     * @var int
15
     */
16
    private $sequence;
17
    /**
18
     * @var int
19
     */
20
    private $version;
21
    /**
22
     * @var EventWithMetaData[]
23
     */
24
    private $eventsWithMetadata;
25
26 18
    public function __construct(
27
        int $sequence,
28
        int $version,
29
        array $eventsWithMetadata
30
    )
31
    {
32 18
        $this->sequence = $sequence;
33 18
        $this->version = $version;
34 18
        $this->eventsWithMetadata = $eventsWithMetadata;
35 18
    }
36
37
    /**
38
     * @return int
39
     */
40 3
    public function getSequence(): int
41
    {
42 3
        return $this->sequence;
43
    }
44
45
    /**
46
     * @return int
47
     */
48 1
    public function getVersion(): int
49
    {
50 1
        return $this->version;
51
    }
52
53
    /**
54
     * @return EventWithMetaData[]
55
     */
56 14
    public function getEventsWithMetadata()
57
    {
58 14
        return $this->eventsWithMetadata;
59
    }
60
61
    public function filterEventsByClass(array $eventClasses): self
62
    {
63 7
        $events = array_filter($this->eventsWithMetadata, function (EventWithMetaData $eventWithMetaData) use ($eventClasses) {
64 7
            return $this->eventHasAnyOfThisClasses($eventWithMetaData->getEvent(), $eventClasses);
65 7
        });
66
67 7
        return new self(
68 7
            $this->sequence,
69 7
            $this->version,
70 7
            $events
71
        );
72
    }
73
74 7
    private function eventHasAnyOfThisClasses($event, array $eventClasses)
75
    {
76 7
        foreach ($eventClasses as $eventClass) {
77
78 7
            if ((new SubclassComparator())->isASubClassOrSameClass($event, $eventClass)) {
79 7
                return true;
80
            }
81
        }
82
83 3
        return false;
84
    }
85
}