EventsCommit   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 76
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A getSequence() 0 4 1
A getVersion() 0 4 1
A getEventsWithMetadata() 0 4 1
A eventHasAnyOfThisClasses() 0 11 3
A filterEventsByClass() 0 12 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
}