Repository   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 94.87%

Importance

Changes 10
Bugs 0 Features 0
Metric Value
wmc 8
c 10
b 0
f 0
lcom 1
cbo 3
dl 0
loc 86
ccs 37
cts 39
cp 0.9487
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 6 1
B handle() 0 40 4
A create() 0 4 1
A find() 0 11 2
1
<?php namespace C4tech\RayEmitter\Domain;
2
3
use C4tech\RayEmitter\Contracts\Domain\Command as CommandInterface;
4
use C4tech\RayEmitter\Contracts\Domain\Repository as RepositoryInterface;
5
use C4tech\RayEmitter\Facades\EventStore;
6
use C4tech\RayEmitter\Exceptions\OutdatedSequence;
7
use C4tech\RayEmitter\Exceptions\SequenceMismatch;
8
9
abstract class Repository implements RepositoryInterface
10
{
11
    /**
12
     * @inheritDoc
13
     */
14 1
    public static function get($identifier)
15
    {
16 1
        $aggregate = static::find($identifier);
17
18 1
        return $aggregate->getEntity();
19
    }
20
21
    /**
22
     * @inheritDoc
23
     */
24 4
    public static function handle(CommandInterface $command)
25
    {
26 4
        $aggregate = static::find($command->getAggregateId());
27
28
        // Optimistic concurrency handling
29 4
        $expected = $command->getExpectedSequence();
30 4
        $current = $aggregate->getSequence();
31 4
        if ($expected < $current) {
32 1
            throw new OutdatedSequence(
33 1
                sprintf(
34 1
                    'The Aggregate %s has newer data than expected (%s v %s).',
35 1
                    get_class($aggregate),
36 1
                    $expected,
37
                    $current
38 1
                ),
39
                409
40 1
            );
41 3
        } elseif ($expected > $current) {
42 1
            throw new SequenceMismatch(
43 1
                sprintf(
44 1
                    'The Aggregate %s is expected to have more data than it does (%s v %s).',
45 1
                    get_class($aggregate),
46 1
                    $expected,
47
                    $current
48 1
                ),
49
                422
50 1
            );
51
        }
52
53 2
        $identifier = null;
54
55
        // Queue event for storage
56 2
        if ($event = $aggregate->handle($command)) {
57 1
            $aggregate->apply($event);
58 1
            EventStore::saveEvent($event);
59 1
            $identifier = $event->getId();
60 1
        }
61
62 2
        return $identifier;
63
    }
64
65
    /**
66
     * Create
67
     *
68
     * Generate a new Aggregate with no history.
69
     * @return Aggregate
70
     */
71
    protected static function create()
72
    {
73
        throw new Exception('The ' . static::class . '::create method must be defined');
74
    }
75
76
    /**
77
     * Find
78
     *
79
     * Restore an existing Aggregate from the recorded events related to it.
80
     * @param  void|string $identifier Aggregate root entity identifier.
81
     * @return Aggregate
82
     */
83 6
    protected static function &find($identifier = null)
84
    {
85 6
        $aggregate = static::create();
86
87 6
        if ($identifier) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $identifier of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
88 6
            $events = EventStore::getFor($identifier);
89 6
            $aggregate->hydrate($events);
90 6
        }
91
92 6
        return $aggregate;
93
    }
94
}
95