|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Stefano Torresi (http://stefanotorresi.it) |
|
4
|
|
|
* @license See the file LICENSE.txt for copying permission. |
|
5
|
|
|
* ************************************************ |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Thorr\Persistence\Doctrine\DataMapper; |
|
9
|
|
|
|
|
10
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
|
11
|
|
|
use InvalidArgumentException; |
|
12
|
|
|
use Ramsey\Uuid\Uuid; |
|
13
|
|
|
use Thorr\Persistence\DataMapper\DeferredOperationProvider; |
|
14
|
|
|
use Thorr\Persistence\DataMapper\DeferredRemoveProvider; |
|
15
|
|
|
use Thorr\Persistence\DataMapper\DeferredSaveProvider; |
|
16
|
|
|
use Thorr\Persistence\DataMapper\SimpleDataMapperInterface; |
|
17
|
|
|
use Thorr\Persistence\Doctrine\ObjectManager\ObjectManagerAwareInterface; |
|
18
|
|
|
use Thorr\Persistence\Doctrine\ObjectManager\ObjectManagerAwareTrait; |
|
19
|
|
|
|
|
20
|
|
|
class DoctrineAdapter implements |
|
21
|
|
|
SimpleDataMapperInterface, |
|
22
|
|
|
DeferredOperationProvider, |
|
23
|
|
|
DeferredSaveProvider, |
|
24
|
|
|
DeferredRemoveProvider, |
|
25
|
|
|
ObjectManagerAwareInterface |
|
26
|
|
|
{ |
|
27
|
|
|
use ObjectManagerAwareTrait; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $entityClass; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param ObjectManager $objectManager |
|
36
|
|
|
* @param string $entityClass |
|
37
|
|
|
*/ |
|
38
|
6 |
|
public function __construct($entityClass, ObjectManager $objectManager) |
|
39
|
|
|
{ |
|
40
|
6 |
|
$this->entityClass = $entityClass; |
|
41
|
6 |
|
$this->setObjectManager($objectManager); |
|
42
|
6 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* The entity class handled by this adapter. Must be set during instantiation and it's immutable. |
|
46
|
|
|
* |
|
47
|
|
|
* @return string |
|
48
|
|
|
*/ |
|
49
|
2 |
|
public function getEntityClass() |
|
50
|
|
|
{ |
|
51
|
2 |
|
return $this->entityClass; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* {@inheritdoc} |
|
56
|
|
|
*/ |
|
57
|
4 |
|
public function findByUuid($uuid) |
|
58
|
|
|
{ |
|
59
|
4 |
|
if (! $uuid instanceof Uuid) { |
|
60
|
|
|
try { |
|
61
|
4 |
|
$uuid = Uuid::fromString($uuid); |
|
62
|
4 |
|
} catch (InvalidArgumentException $e) { |
|
63
|
1 |
|
return; |
|
64
|
|
|
} |
|
65
|
3 |
|
} |
|
66
|
|
|
|
|
67
|
3 |
|
return $this->objectManager->getRepository($this->entityClass)->findOneBy(['uuid' => $uuid->toString()]); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* {@inheritdoc} |
|
72
|
|
|
*/ |
|
73
|
1 |
|
public function remove($entity) |
|
74
|
|
|
{ |
|
75
|
1 |
|
$this->objectManager->remove($entity); |
|
76
|
1 |
|
$this->objectManager->flush(); |
|
77
|
1 |
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* {@inheritdoc} |
|
81
|
|
|
*/ |
|
82
|
1 |
|
public function removeByUuid($uuid) |
|
83
|
|
|
{ |
|
84
|
1 |
|
$entity = $this->findByUuid($uuid); |
|
85
|
1 |
|
$this->remove($entity); |
|
|
|
|
|
|
86
|
1 |
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* {@inheritdoc} |
|
90
|
|
|
*/ |
|
91
|
2 |
|
public function save($entity) |
|
92
|
|
|
{ |
|
93
|
2 |
|
$this->objectManager->persist($entity); |
|
94
|
2 |
|
$this->objectManager->flush(); |
|
95
|
2 |
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* {@inheritdoc} |
|
99
|
|
|
*/ |
|
100
|
1 |
|
public function saveDeferred($entity) |
|
101
|
|
|
{ |
|
102
|
1 |
|
$this->objectManager->persist($entity); |
|
103
|
1 |
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* {@inheritdoc} |
|
107
|
|
|
*/ |
|
108
|
1 |
|
public function removeDeferred($entity) |
|
109
|
|
|
{ |
|
110
|
1 |
|
$this->objectManager->remove($entity); |
|
111
|
1 |
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* {@inheritdoc} |
|
115
|
|
|
*/ |
|
116
|
2 |
|
public function commit() |
|
117
|
|
|
{ |
|
118
|
2 |
|
$this->objectManager->flush(); |
|
119
|
2 |
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.