1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Psi\Bridge\ObjectAgent\Doctrine\PhpcrOdm; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\Persistence\Mapping\MappingException; |
8
|
|
|
use Doctrine\Common\Util\ClassUtils; |
9
|
|
|
use Doctrine\ODM\PHPCR\DocumentManagerInterface; |
10
|
|
|
use Psi\Component\ObjectAgent\AgentInterface; |
11
|
|
|
use Psi\Component\ObjectAgent\Exception\ObjectNotFoundException; |
12
|
|
|
use Psi\Component\ObjectAgent\Query\Query; |
13
|
|
|
|
14
|
|
|
class PhpcrOdmAgent implements AgentInterface |
15
|
|
|
{ |
16
|
|
|
private $documentManager; |
17
|
|
|
|
18
|
|
|
public function __construct( |
19
|
|
|
DocumentManagerInterface $documentManager |
20
|
|
|
) { |
21
|
|
|
$this->documentManager = $documentManager; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
|
|
public function find($identifier, string $class = null) |
28
|
|
|
{ |
29
|
|
|
$object = $this->documentManager->find($class, $identifier); |
30
|
|
|
|
31
|
|
|
if (null === $object) { |
32
|
|
|
throw new ObjectNotFoundException(sprintf( |
33
|
|
|
'Could not find document with identifier "%s" (class "%s")', |
34
|
|
|
$identifier, null === $class ? '<null>' : $class |
35
|
|
|
)); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return $object; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
public function save($object) |
45
|
|
|
{ |
46
|
|
|
$this->documentManager->persist($object); |
47
|
|
|
$this->documentManager->flush(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
public function delete($object) |
54
|
|
|
{ |
55
|
|
|
$this->documentManager->remove($object); |
56
|
|
|
$this->documentManager->flush(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
|
View Code Duplication |
public function getIdentifier($object) |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$objectFqn = ClassUtils::getRealClass(get_class($object)); |
65
|
|
|
$metadata = $this->documentManager->getClassMetadata($objectFqn); |
66
|
|
|
$uuidFieldName = $metadata->getUuidFieldName(); |
67
|
|
|
|
68
|
|
|
if (!$uuidFieldName) { |
69
|
|
|
throw new \RuntimeException(sprintf( |
70
|
|
|
'Document "%s" does not have a UUID-mapped property. All '. |
71
|
|
|
'PHPCR-ODM documents must have a mapped UUID proprety.', |
72
|
|
|
$objectFqn |
73
|
|
|
)); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$node = $this->documentManager->getNodeForDocument($object); |
77
|
|
|
|
78
|
|
|
return $node->getIdentifier(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
|
View Code Duplication |
public function setParent($object, $parent) |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
$objectFqn = ClassUtils::getRealClass(get_class($object)); |
87
|
|
|
$metadata = $this->documentManager->getClassMetadata($objectFqn); |
88
|
|
|
$parentField = $metadata->parentMapping; |
|
|
|
|
89
|
|
|
|
90
|
|
|
if (!$parentField) { |
91
|
|
|
throw new \RuntimeException(sprintf( |
92
|
|
|
'Document "%s" does not have a ParentDocument mapping All '. |
93
|
|
|
'PHPCR-ODM documents must have a mapped parent proprety.', |
94
|
|
|
$objectFqn |
95
|
|
|
)); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$metadata->setFieldValue($object, $parentField, $parent); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
|
|
public function supports(string $class): bool |
105
|
|
|
{ |
106
|
|
|
$metadataFactory = $this->documentManager->getMetadataFactory(); |
107
|
|
|
|
108
|
|
|
$supports = false; |
109
|
|
|
try { |
110
|
|
|
$metadataFactory->getMetadataFor(ClassUtils::getRealClass($class)); |
111
|
|
|
$supports = true; |
112
|
|
|
} catch (MappingException $exception) { |
113
|
|
|
// no metadata - class is not known to phpcr-odm |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $supports; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritdoc} |
121
|
|
|
*/ |
122
|
|
|
public function query(Query $query): \Traversable |
123
|
|
|
{ |
124
|
|
|
$sourceAlias = 'a'; |
125
|
|
|
$queryBuilder = $this->documentManager->getRepository($query->getClassFqn())->createQueryBuilder($sourceAlias); |
126
|
|
|
$visitor = new ExpressionVisitor( |
127
|
|
|
$queryBuilder, |
128
|
|
|
$sourceAlias |
129
|
|
|
); |
130
|
|
|
|
131
|
|
|
$visitor->dispatch($query->getExpression()); |
132
|
|
|
|
133
|
|
|
return $queryBuilder->getQuery()->execute(); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
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.