Completed
Push — master ( b87198...f2f319 )
by Frank
02:38
created

ConstructionBehaviour::reconstituteFromEvents()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
crap 2
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
    /**
14
     * @var AggregateRootId
15
     */
16
    protected $aggregateRootId;
17
18 7
    public function __construct(AggregateRootId $aggregateRootId)
19
    {
20 7
        $this->aggregateRootId = $aggregateRootId;
21 7
    }
22
23 7
    public function aggregateRootId(): AggregateRootId
24
    {
25 7
        return $this->aggregateRootId;
26
    }
27
28
    /**
29
     * @param AggregateRootId $aggregateRootId
30
     * @param Generator           $events
31
     *
32
     * @return static
33
     */
34 7
    public static function reconstituteFromEvents(AggregateRootId $aggregateRootId, Generator $events): AggregateRoot
35
    {
36 7
        $aggregateRoot = new static($aggregateRootId);
37
38
        /** @var Event $event */
39 7
        foreach ($events as $event) {
40 1
            $aggregateRoot->apply($event);
41
        }
42
43 7
        return $aggregateRoot;
44
    }
45
46
    abstract protected function apply(Event $event);
47
}