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

ConstructionBehaviour::apply()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
ccs 0
cts 0
cp 0
nc 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
    /**
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
}