|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* Copyright (c) 2012-2014 Janos Szurovecz |
|
4
|
|
|
* |
|
5
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
|
6
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
|
7
|
|
|
* the Software without restriction, including without limitation the rights to |
|
8
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
|
9
|
|
|
* of the Software, and to permit persons to whom the Software is furnished to do |
|
10
|
|
|
* so, subject to the following conditions: |
|
11
|
|
|
* |
|
12
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
13
|
|
|
* copies or substantial portions of the Software. |
|
14
|
|
|
* |
|
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
16
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
18
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
19
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
20
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
21
|
|
|
* SOFTWARE. |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
namespace predaddy\inmemory; |
|
25
|
|
|
|
|
26
|
|
|
use precore\lang\Object; |
|
27
|
|
|
use precore\util\Preconditions; |
|
28
|
|
|
use predaddy\domain\AggregateId; |
|
29
|
|
|
use predaddy\domain\AggregateRoot; |
|
30
|
|
|
use predaddy\domain\Repository; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @author Janos Szurovecz <[email protected]> |
|
34
|
|
|
*/ |
|
35
|
|
|
final class InMemoryRepository extends Object implements Repository |
|
36
|
|
|
{ |
|
37
|
|
|
/** |
|
38
|
|
|
* @var array |
|
39
|
|
|
*/ |
|
40
|
|
|
private $aggregates = []; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Load the aggregate identified by $aggregateId from the persistent storage. |
|
44
|
|
|
* |
|
45
|
|
|
* @param AggregateId $aggregateId |
|
46
|
|
|
* @return AggregateRoot |
|
47
|
|
|
* @throws \InvalidArgumentException If the $aggregateId is invalid |
|
48
|
|
|
*/ |
|
49
|
|
|
public function load(AggregateId $aggregateId) |
|
50
|
|
|
{ |
|
51
|
|
|
$key = $this->createKey($aggregateId); |
|
52
|
|
|
Preconditions::checkArgument( |
|
53
|
|
|
array_key_exists($key, $this->aggregates), |
|
54
|
|
|
'Aggregate with ID [%s] does not exist', |
|
55
|
|
|
$aggregateId |
|
56
|
|
|
); |
|
57
|
|
|
self::getLogger()->debug('Aggregate identified by [{}] has been loaded', [$aggregateId]); |
|
58
|
|
|
return $this->aggregates[$key]; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Persisting the given $aggregateRoot. |
|
63
|
|
|
* |
|
64
|
|
|
* @param AggregateRoot $aggregateRoot |
|
65
|
|
|
*/ |
|
66
|
|
|
public function save(AggregateRoot $aggregateRoot) |
|
67
|
|
|
{ |
|
68
|
|
|
$this->aggregates[$this->createKey($aggregateRoot->getId())] = $aggregateRoot; |
|
69
|
|
|
self::getLogger()->debug('Aggregate identified by [{}] has been persisted', [$aggregateRoot->getId()]); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
private function createKey(AggregateId $aggregateId) |
|
73
|
|
|
{ |
|
74
|
|
|
return $aggregateId->aggregateClass() . $aggregateId->value(); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|