Completed
Push — master ( 8a4be1...fe7d42 )
by Constantin
06:22
created

AggregateRepository   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 3
dl 0
loc 53
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A loadAggregate() 0 13 1
A saveAggregate() 0 8 1
1
<?php
2
/******************************************************************************
3
 * Copyright (c) 2016 Constantin Galbenu <[email protected]>             *
4
 ******************************************************************************/
5
6
namespace Gica\Cqrs\Aggregate;
7
8
9
use Gica\Cqrs\Event\EventsApplier\EventsApplierOnAggregate;
10
use Gica\Cqrs\Event\EventWithMetaData;
11
use Gica\Cqrs\EventStore;
12
use Gica\Cqrs\EventStore\AggregateEventStream;
13
use Gica\Cqrs\EventStore\EventStream;
14
15
class AggregateRepository
16
{
17
    /**
18
     * @var EventStore
19
     */
20
    private $eventStore;
21
22
    /**
23
     * @var EventsApplierOnAggregate
24
     */
25
    private $eventsApplierOnAggregate;
26
27
    /**
28
     * @var EventStream[]
29
     */
30
    private $aggregateToEventStreamMap;
31
32 6
    public function __construct(
33
        EventStore $eventStore,
34
        EventsApplierOnAggregate $eventsApplierOnAggregate
35
    )
36
    {
37 6
        $this->eventStore = $eventStore;
38 6
        $this->eventsApplierOnAggregate = $eventsApplierOnAggregate;
39 6
        $this->aggregateToEventStreamMap = new \SplObjectStorage();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \SplObjectStorage() of type object<SplObjectStorage> is incompatible with the declared type array<integer,object<Gic...ventStore\EventStream>> of property $aggregateToEventStreamMap.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
40 6
    }
41
42 5
    public function loadAggregate(string $aggregateClass, $aggregateId)
43
    {
44 5
        $aggregate = new $aggregateClass;
45
46 5
        $priorEvents = $this->eventStore->loadEventsForAggregate($aggregateClass, $aggregateId);
47
48 5
        $this->aggregateToEventStreamMap[$aggregate] = $priorEvents;
49
50
        /** @var EventWithMetaData[] $priorEvents */
51 5
        $this->eventsApplierOnAggregate->applyEventsOnAggregate($aggregate, $priorEvents);
52
53 5
        return $aggregate;
54
    }
55
56
    /**
57
     * @inheritdoc
58
     */
59 3
    public function saveAggregate($aggregateId, $aggregate, $newEventsWithMetaData)
60
    {
61
        /** @var AggregateEventStream $priorEvents */
62 3
        $priorEvents = $this->aggregateToEventStreamMap[$aggregate];
63
64 3
        $this->eventStore->appendEventsForAggregate(
65 3
            $aggregateId, get_class($aggregate), $newEventsWithMetaData, $priorEvents->getVersion(), $priorEvents->getSequence());
66
    }
67
}