1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
declare(strict_types=1);t |
4
|
|
|
|
5
|
|
|
namespace Psi\Bridge\ObjectAgent\Doctrine\PhpcrOdm; |
|
|
|
|
6
|
|
|
|
7
|
|
|
use Psi\Component\ObjectAgent\AgentInterface; |
8
|
|
|
use Doctrine\ODM\PHPCR\DocumentManagerInterface; |
9
|
|
|
use Doctrine\Common\Util\ClassUtils; |
10
|
|
|
use Psi\Component\ObjectAgent\Exception\ObjectNotFound; |
11
|
|
|
use Psi\Component\ObjectAgent\Agent\Doctrine\Event\PhpcrOdmObjectEvent; |
12
|
|
|
use Psi\Component\ObjectAgent\Agent\Doctrine\Event\ObjectEvent; |
13
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
14
|
|
|
use Psi\Component\ObjectAgent\Events; |
15
|
|
|
|
16
|
|
|
class PhpcrOdmAgent implements AgentInterface |
17
|
|
|
{ |
18
|
|
|
private $documentManager; |
19
|
|
|
private $alias; |
20
|
|
|
private $eventDispatcher; |
21
|
|
|
|
22
|
|
|
public function __construct( |
23
|
|
|
$alias, |
24
|
|
|
EventDispatcherInterface $eventDispatcher, |
25
|
|
|
DocumentManagerInterface $documentManager |
26
|
|
|
) |
27
|
|
|
{ |
28
|
|
|
$this->alias = $alias; |
29
|
|
|
$this->documentManager = $documentManager; |
30
|
|
|
$this->eventDispatcher = $eventDispatcher; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
|
|
public function find($identifier) |
37
|
|
|
{ |
38
|
|
|
$object = $this->documentManager->find(null, $identifier); |
39
|
|
|
|
40
|
|
|
if (null === $object) { |
41
|
|
|
throw new ObjectNotFound($identifier); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $object; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
|
|
public function save($object) |
51
|
|
|
{ |
52
|
|
|
$this->eventDispatcher->dispatch(Events::PRE_SAVE, new ObjectEvent( |
53
|
|
|
$this->documentManager, $object |
54
|
|
|
)); |
55
|
|
|
|
56
|
|
|
$this->documentManager->persist($object); |
57
|
|
|
$this->documentManager->flush(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
public function getIdentifier($object) |
64
|
|
|
{ |
65
|
|
|
$objectFqn = ClassUtils::getRealClass(get_class($object)); |
66
|
|
|
$metadata = $this->documentManager->getClassMetadata($objectFqn); |
67
|
|
|
$uuidFieldName = $metadata->getUuidFieldName(); |
68
|
|
|
|
69
|
|
|
if (!$uuidFieldName) { |
70
|
|
|
throw new \RuntimeException(sprintf( |
71
|
|
|
'Document "%s" does not have a UUID-mapped property. All '. |
72
|
|
|
'PHPCR-ODM documents must have a mapped UUID proprety.', |
73
|
|
|
$objectFqn |
74
|
|
|
)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$node = $this->documentManager->getNodeForDocument($object); |
78
|
|
|
|
79
|
|
|
return $node->getIdentifier(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
|
|
public function setParent($object, $parent) |
86
|
|
|
{ |
87
|
|
|
$objectFqn = ClassUtils::getRealClass(get_class($object)); |
88
|
|
|
$metadata = $this->documentManager->getClassMetadata($objectFqn); |
89
|
|
|
$parentField = $metadata->parentMapping; |
90
|
|
|
|
91
|
|
|
if (!$parentField) { |
92
|
|
|
throw new \RuntimeException(sprintf( |
93
|
|
|
'Document "%s" does not have a ParentDocument mapping All '. |
94
|
|
|
'PHPCR-ODM documents must have a mapped parent proprety.', |
95
|
|
|
$objectFqn |
96
|
|
|
)); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$metadata->setFieldValue($object, $parentField, $parent); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritdoc} |
104
|
|
|
*/ |
105
|
|
|
public function getAlias() |
106
|
|
|
{ |
107
|
|
|
return $this->alias; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* {@inheritdoc} |
112
|
|
|
*/ |
113
|
|
|
public function supports($class) |
114
|
|
|
{ |
115
|
|
|
$metadataFactory = $this->documentManager->getMetadataFactory(); |
116
|
|
|
$supports = $metadataFactory->getMetadataFor(ClassUtils::getRealClass($class)); |
117
|
|
|
|
118
|
|
|
return $supports ? true : false; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.