Passed
Push — master ( 97a5b7...e77720 )
by Frank
46s queued 10s
created

AggregateRootBehaviour/ConstructionBehaviour.php (1 issue)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace EventSauce\EventSourcing\AggregateRootBehaviour;
6
7
use EventSauce\EventSourcing\AggregateRoot;
8
use EventSauce\EventSourcing\AggregateRootId;
9
use Generator;
10
11
trait ConstructionBehaviour
12
{
13
    /**
14
     * @var AggregateRootId
15
     */
16
    private $aggregateRootId;
17
18
    private $aggregateRootVersion = 0;
19
20 8
    public function __construct(AggregateRootId $aggregateRootId)
21
    {
22 8
        $this->aggregateRootId = $aggregateRootId;
23 8
    }
24
25 8
    public function aggregateRootId(): AggregateRootId
26
    {
27 8
        return $this->aggregateRootId;
28
    }
29
30 8
    public function aggregateRootVersion(): int
31
    {
32 8
        return $this->aggregateRootVersion;
33
    }
34
35
    /**
36
     * @param AggregateRootId $aggregateRootId
37
     * @param Generator       $events
38
     *
39
     * @return static
40
     */
41 8
    public static function reconstituteFromEvents(AggregateRootId $aggregateRootId, Generator $events): AggregateRoot
42
    {
43 8
        $aggregateRoot = new static($aggregateRootId);
44
45
        /** @var object $event */
46 8
        foreach ($events as $event) {
47 2
            $aggregateRoot->apply($event);
48 2
            ++$aggregateRoot->aggregateRootVersion;
49
        }
50
51 8
        return $aggregateRoot;
0 ignored issues
show
Bug Best Practice introduced by Frank de Jonge
The expression return $aggregateRoot returns the type EventSauce\EventSourcing...r\ConstructionBehaviour which is incompatible with the type-hinted return EventSauce\EventSourcing\AggregateRoot.
Loading history...
52
    }
53
54
    abstract protected function apply(object $event);
55
}
56