Completed
Push — master ( e000b3...b0a3db )
by Christopher
06:52 queued 48s
created

Repository   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 7
c 3
b 0
f 0
lcom 1
cbo 4
dl 0
loc 71
rs 10
ccs 34
cts 34
cp 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A find() 0 7 1
A getEntity() 0 6 1
B handle() 0 30 4
A restore() 0 5 1
1
<?php namespace C4tech\RayEmitter\Domain;
2
3
use C4tech\RayEmitter\Contracts\Domain\Aggregate as AggregateInterface;
4
use C4tech\RayEmitter\Contracts\Domain\Command as CommandInterface;
5
use C4tech\RayEmitter\Contracts\Domain\Repository as RepositoryInterface;
6
use C4tech\RayEmitter\Facades\EventStore;
7
use C4tech\RayEmitter\Exceptions\OutdatedSequence;
8
use C4tech\RayEmitter\Exceptions\SequenceMismatch;
9
10
abstract class Repository implements RepositoryInterface
11
{
12
    /**
13
     * @inheritDoc
14
     */
15 2
    public static function find($identifier)
16
    {
17 2
        $aggregate = static::create();
18 2
        static::restore($identifier, $aggregate);
19
20 2
        return $aggregate;
21
    }
22
23
    /**
24
     * @inheritDoc
25
     */
26 1
    public static function getEntity($identifier)
27
    {
28 1
        $aggregate = static::find($identifier);
29
30 1
        return $aggregate->getEntity();
31
    }
32
33
    /**
34
     * @inheritDoc
35
     */
36 3
    public static function handle(CommandInterface $command)
37
    {
38 3
        $aggregate = static::create();
39 3
        if ($aggregate_id = $command->getAggregateId()) {
40 2
            static::restore($aggregate_id, $aggregate);
41 2
        }
42
43
        // Optimistic concurrency handling
44 3
        $expected = $command->getExpectedSequence();
45 3
        $current = $aggregate->getSequence();
46 3
        if ($expected < $current) {
47 1
            throw new OutdatedSequence(
48 1
                sprintf(
49 1
                    'The Aggregate %s has newer data than expected.',
50 1
                    get_class($aggregate)
51 1
                ),
52
                409
53 1
            );
54 2
        } elseif ($expected > $current) {
55 1
            throw new SequenceMismatch(
56 1
                sprintf(
57 1
                    'The Aggregate %s is expected to have more data than it does',
58 1
                    get_class($aggregate)
59 1
                ),
60
                422
61 1
            );
62
        }
63
64 1
        $aggregate->handle($command);
65 1
    }
66
67
    /**
68
     * Restore
69
     *
70
     * Hydrate an aggregate with its recorded events.
71
     * @param  string             $identifier Aggregate root entity identifier.
72
     * @param  AggregateInterface &$aggregate (Fresh) Aggregate to hydrate.
73
     * @return void
74
     */
75 4
    protected static function restore($identifier, AggregateInterface &$aggregate)
76
    {
77 4
        $events = EventStore::getFor($identifier);
78 4
        $aggregate->hydrate($events);
79 4
    }
80
}
81