Completed
Push — master ( d0fcc3...3bd81e )
by Christopher
03:10
created

Repository   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 95.65%

Importance

Changes 9
Bugs 0 Features 0
Metric Value
wmc 11
c 9
b 0
f 0
lcom 1
cbo 4
dl 0
loc 105
ccs 44
cts 46
cp 0.9565
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 6 1
B handle() 0 46 6
A create() 0 4 1
A find() 0 18 3
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
     * Aggregate Cache
13
     * @var array
14
     */
15
    protected static $aggregates = [];
16
17
    /**
18
     * @inheritDoc
19
     */
20 1
    public static function get($identifier)
21
    {
22 1
        $aggregate = static::find($identifier);
23
24 1
        return $aggregate->getEntity();
25
    }
26
27
    /**
28
     * @inheritDoc
29
     */
30 4
    public static function handle(CommandInterface $command)
31
    {
32 4
        $aggregate = static::find($command->getAggregateId());
33
34
        // Optimistic concurrency handling
35 4
        $expected = $command->getExpectedSequence();
36 4
        $current = $aggregate->getSequence();
37 4
        if ($expected < $current) {
38 1
            throw new OutdatedSequence(
39 1
                sprintf(
40 1
                    'The Aggregate %s has newer data than expected (%s v %s).',
41 1
                    get_class($aggregate),
42 1
                    $expected,
43
                    $current
44 1
                ),
45
                409
46 1
            );
47 3
        } elseif ($expected > $current) {
48 1
            throw new SequenceMismatch(
49 1
                sprintf(
50 1
                    'The Aggregate %s is expected to have more data than it does (%s v %s).',
51 1
                    get_class($aggregate),
52 1
                    $expected,
53
                    $current
54 1
                ),
55
                422
56 1
            );
57
        }
58
59 2
        $identifier = null;
60
61
        // Queue event for storage
62 2
        if ($event = $aggregate->handle($command)) {
63 1
            $aggregate->apply($event);
64 1
            EventStore::enqueue($event);
65 1
            $identifier = $event->getId();
66 1
        }
67
68
69
        // Cache the aggregate in memory if it is not already.
70 2
        if (!empty($identifier) && !isset(self::$aggregates[$identifier])) {
71 1
            self::$aggregates[$identifier] = $aggregate;
72 1
        }
73
74 2
        return $identifier;
75
    }
76
77
    /**
78
     * Create
79
     *
80
     * Generate a new Aggregate with no history.
81
     * @return Aggregate
82
     */
83
    protected static function create()
84
    {
85
        throw new Exception('The ' . static::class . '::create method must be defined');
86
    }
87
88
    /**
89
     * Find
90
     *
91
     * Restore an existing Aggregate from the recorded events related to it.
92
     * @param  void|string $identifier Aggregate root entity identifier.
93
     * @return Aggregate
94
     */
95 6
    protected static function &find($identifier = null)
96
    {
97 6
        $aggregate = static::create();
98
99 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...
100
            // Cache hydrated aggregate
101 6
            if (!isset(self::$aggregates[$identifier])) {
102 6
                $events = EventStore::getFor($identifier);
103 6
                $aggregate->hydrate($events);
104
105 6
                self::$aggregates[$identifier] = $aggregate;
106 6
            }
107
108 6
            $aggregate = self::$aggregates[$identifier];
109 6
        }
110
111 6
        return $aggregate;
112
    }
113
}
114