Completed
Push — master ( d9d7f1...cbb893 )
by Frank
04:08
created

ConstructionBehaviour   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 0
dl 0
loc 45
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A aggregateRootId() 0 4 1
A aggregateRootVersion() 0 4 1
A reconstituteFromEvents() 0 12 2
apply() 0 1 ?
1
<?php
2
3
namespace EventSauce\EventSourcing\AggregateRootBehaviour;
4
5
use EventSauce\EventSourcing\AggregateRoot;
6
use EventSauce\EventSourcing\AggregateRootId;
7
use EventSauce\EventSourcing\Event;
8
use Generator;
9
10
trait ConstructionBehaviour
11
{
12
    /**
13
     * @var AggregateRootId
14
     */
15
    private $aggregateRootId;
16
17
    private $aggregateRootVersion = 0;
18
19 8
    public function __construct(AggregateRootId $aggregateRootId)
20
    {
21 8
        $this->aggregateRootId = $aggregateRootId;
22 8
    }
23
24 8
    public function aggregateRootId(): AggregateRootId
25
    {
26 8
        return $this->aggregateRootId;
27
    }
28
29 8
    public function aggregateRootVersion(): int
30
    {
31 8
        return $this->aggregateRootVersion;
32
    }
33
34
    /**
35
     * @param AggregateRootId $aggregateRootId
36
     * @param Generator           $events
37
     *
38
     * @return static
39
     */
40 8
    public static function reconstituteFromEvents(AggregateRootId $aggregateRootId, Generator $events): AggregateRoot
41
    {
42 8
        $aggregateRoot = new static($aggregateRootId);
43
44
        /** @var Event $event */
45 8
        foreach ($events as $event) {
46 2
            $aggregateRoot->apply($event);
47 2
            $aggregateRoot->aggregateRootVersion++;
48
        }
49
50 8
        return $aggregateRoot;
51
    }
52
53
    abstract protected function apply(Event $event);
54
}