|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* To change this license header, choose License Headers in Project Properties. |
|
5
|
|
|
* To change this template file, choose Tools | Templates |
|
6
|
|
|
* and open the template in the editor. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Governor\Framework\Repository; |
|
10
|
|
|
|
|
11
|
|
|
use Governor\Framework\Domain\AggregateRootInterface; |
|
12
|
|
|
use Governor\Framework\EventHandling\EventBusInterface; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Description of LockingRepository |
|
16
|
|
|
* |
|
17
|
|
|
* @author "David Kalosi" <[email protected]> |
|
18
|
|
|
* @license <a href="http://www.opensource.org/licenses/mit-license.php">MIT License</a> |
|
19
|
|
|
*/ |
|
20
|
|
|
abstract class LockingRepository extends AbstractRepository |
|
21
|
|
|
{ |
|
22
|
|
|
|
|
23
|
|
|
private $lockManager; |
|
24
|
|
|
|
|
25
|
23 |
|
public function __construct($className, EventBusInterface $eventBus, LockManagerInterface $lockManager) |
|
26
|
|
|
{ |
|
27
|
23 |
|
parent::__construct($className, $eventBus); |
|
28
|
23 |
|
$this->lockManager = $lockManager; |
|
29
|
23 |
|
} |
|
30
|
|
|
|
|
31
|
3 |
View Code Duplication |
public function add(AggregateRootInterface $object) |
|
|
|
|
|
|
32
|
|
|
{ |
|
33
|
3 |
|
$aggregateId = $object->getIdentifier(); |
|
34
|
3 |
|
$this->lockManager->obtainLock($aggregateId); |
|
35
|
|
|
|
|
36
|
|
|
try { |
|
37
|
3 |
|
parent::add($object); |
|
38
|
3 |
|
} catch (\Exception $ex) { |
|
39
|
|
|
$this->lockManager->releaseLock($aggregateId); |
|
40
|
|
|
throw $ex; |
|
41
|
|
|
} |
|
42
|
3 |
|
} |
|
43
|
|
|
|
|
44
|
14 |
View Code Duplication |
public function load($id, $expectedVersion = null) |
|
|
|
|
|
|
45
|
|
|
{ |
|
46
|
14 |
|
$this->lockManager->obtainLock($id); |
|
47
|
|
|
try { |
|
48
|
14 |
|
$object = parent::load($id, $expectedVersion); |
|
49
|
|
|
|
|
50
|
10 |
|
return $object; |
|
51
|
4 |
|
} catch (\Exception $ex) { |
|
52
|
4 |
|
$this->lockManager->releaseLock($id); |
|
53
|
4 |
|
throw $ex; |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
1 |
View Code Duplication |
protected function doDelete(AggregateRootInterface $object) |
|
|
|
|
|
|
58
|
|
|
{ |
|
59
|
|
|
if (null !== $object->getVersion() && !$this->lockManager->validateLock($object)) { |
|
60
|
|
|
throw new ConcurrencyException(sprintf( |
|
61
|
1 |
|
"The aggregate of type [%s] with identifier [%s] could not be " . |
|
62
|
|
|
"saved, as a valid lock is not held. Either another thread has saved an aggregate, or " . |
|
63
|
|
|
"the current thread had released its lock earlier on.", |
|
64
|
|
|
get_class($object), $object->getIdentifier() |
|
65
|
|
|
)); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$this->doDeleteWithLock($object); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
9 |
View Code Duplication |
protected function doSave(AggregateRootInterface $object) |
|
|
|
|
|
|
72
|
|
|
{ |
|
73
|
9 |
|
if (null !== $object->getVersion() && !$this->lockManager->validateLock($object)) { |
|
74
|
|
|
throw new ConcurrencyException(sprintf( |
|
75
|
|
|
"The aggregate of type [%s] with identifier [%s] could not be " |
|
76
|
|
|
. "saved, as a valid lock is not held. Either another thread has saved an aggregate, or " |
|
77
|
|
|
. "the current thread had released its lock earlier on.", |
|
78
|
|
|
get_class($object), $object->getIdentifier() |
|
79
|
|
|
)); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
9 |
|
$this->doSaveWithLock($object); |
|
83
|
9 |
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Perform the actual saving of the aggregate. All necessary locks have been verified. |
|
87
|
|
|
* |
|
88
|
|
|
* @param AggregateRootInterface $aggregate the aggregate to store |
|
89
|
|
|
*/ |
|
90
|
|
|
protected abstract function doSaveWithLock(AggregateRootInterface $aggregate); |
|
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Perform the actual deleting of the aggregate. All necessary locks have been verifierd. |
|
94
|
|
|
* |
|
95
|
|
|
* @param AggregateRootInterface $aggregate the aggregate to delete |
|
96
|
|
|
*/ |
|
97
|
|
|
protected abstract function doDeleteWithLock(AggregateRootInterface $aggregate); |
|
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.