Completed
Push — master ( 97a348...a4dab2 )
by Christopher
04:42
created

src/Domain/Repository.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 1
    public static function get($identifier)
16
    {
17 1
        $aggregate = static::find($identifier);
18
19 1
        return $aggregate->getEntity();
20
    }
21
22
    /**
23
     * @inheritDoc
24
     */
25 4
    public static function handle(CommandInterface $command)
26
    {
27 4
        $aggregate = static::find($command->getAggregateId());
28
29
        // Optimistic concurrency handling
30 4
        $expected = $command->getExpectedSequence();
31 4
        $current = $aggregate->getSequence();
32 4
        if ($expected < $current) {
33 1
            throw new OutdatedSequence(
34 1
                sprintf(
35 1
                    'The Aggregate %s has newer data than expected.',
36 1
                    get_class($aggregate)
37 1
                ),
38
                409
39 1
            );
40 3
        } elseif ($expected > $current) {
41 1
            throw new SequenceMismatch(
42 1
                sprintf(
43 1
                    'The Aggregate %s is expected to have more data than it does',
44 1
                    get_class($aggregate)
45 1
                ),
46
                422
47 1
            );
48
        }
49
50 2
        if ($event = $aggregate->handle($command)) {
51 1
            $aggregate->apply($event);
52 1
            EventStore::enqueue($event);
53 1
        }
54 2
    }
55
56
    /**
57
     * Create
58
     *
59
     * Generate a new Aggregate with no history.
60
     * @return Aggregate
61
     */
62
    abstract protected static function create();
63
64
    /**
65
     * Find
66
     *
67
     * Restore an existing Aggregate from the recorded events related to it.
68
     * @param  void|string $identifier Aggregate root entity identifier.
69
     * @return Aggregate
70
     */
71 6
    protected static function find($identifier = null)
72
    {
73 6
        $aggregate = static::create();
74
75 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...
76 5
            $events = EventStore::getFor($identifier);
77 5
            $aggregate->hydrate($events);
78 5
        }
79
80 6
        return $aggregate;
81
    }
82
}
83