1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
6
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
7
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
8
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
9
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
10
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
11
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
12
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
13
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
14
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
15
|
|
|
* |
16
|
|
|
* The software is based on the Axon Framework project which is |
17
|
|
|
* licensed under the Apache 2.0 license. For more information on the Axon Framework |
18
|
|
|
* see <http://www.axonframework.org/>. |
19
|
|
|
* |
20
|
|
|
* This software consists of voluntary contributions made by many individuals |
21
|
|
|
* and is licensed under the MIT license. For more information, see |
22
|
|
|
* <http://www.governor-framework.org/>. |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace Governor\Framework\EventSourcing; |
26
|
|
|
|
27
|
|
|
use Governor\Framework\Repository\GenericOrmRepository; |
28
|
|
|
use Governor\Framework\EventHandling\EventBusInterface; |
29
|
|
|
use Governor\Framework\Repository\LockManagerInterface; |
30
|
|
|
use Governor\Framework\EventStore\EventStoreInterface; |
31
|
|
|
use Governor\Framework\Domain\AggregateRootInterface; |
32
|
|
|
use Doctrine\ORM\EntityManager; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Description of HybridDoctrineRepository |
36
|
|
|
* |
37
|
|
|
* @author "David Kalosi" <[email protected]> |
38
|
|
|
* @license <a href="http://www.opensource.org/licenses/mit-license.php">MIT License</a> |
39
|
|
|
*/ |
40
|
|
|
class HybridDoctrineRepository extends GenericOrmRepository |
41
|
|
|
{ |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var EventStoreInterface |
45
|
|
|
*/ |
46
|
|
|
private $eventStore; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
private $aggregateTypeIdentifier; |
52
|
|
|
|
53
|
|
|
public function __construct($className, EventBusInterface $eventBus, |
54
|
|
|
LockManagerInterface $lockManager, EntityManager $entityManager, |
55
|
|
|
EventStoreInterface $eventStore) |
56
|
|
|
{ |
57
|
|
|
parent::__construct($className, $eventBus, $lockManager, $entityManager); |
58
|
|
|
$this->eventStore = $eventStore; |
59
|
|
|
|
60
|
|
|
$reflClass = new \ReflectionClass($className); |
61
|
|
|
$this->aggregateTypeIdentifier = $reflClass->getShortName(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
View Code Duplication |
protected function doDeleteWithLock(AggregateRootInterface $aggregate) |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
if (null !== $this->eventStore) { |
67
|
|
|
$this->eventStore->appendEvents($this->getTypeIdentifier(), |
68
|
|
|
$aggregate->getUncommittedEvents()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
parent::doDeleteWithLock($aggregate); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
View Code Duplication |
protected function doSaveWithLock(AggregateRootInterface $aggregate) |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
if (null !== $this->eventStore) { |
77
|
|
|
$this->eventStore->appendEvents($this->getTypeIdentifier(), |
78
|
|
|
$aggregate->getUncommittedEvents()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
parent::doSaveWithLock($aggregate); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Returns the type identifier to use when appending events in the event store. Default to the simple class name of |
86
|
|
|
* the aggregateType provided in the constructor. |
87
|
|
|
* |
88
|
|
|
* @return string the type identifier to use when appending events in the event store. |
89
|
|
|
*/ |
90
|
|
|
protected function getTypeIdentifier() |
91
|
|
|
{ |
92
|
|
|
return $this->aggregateTypeIdentifier; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
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.