-
Notifications
You must be signed in to change notification settings - Fork 0
/
EntityPersister.php
142 lines (119 loc) · 4.67 KB
/
EntityPersister.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
declare(strict_types=1);
namespace XcoreCMS\InlineEditing\Model\Entity;
use Doctrine\ORM\EntityManager;
use Symfony\Component\PropertyAccess\PropertyAccessor;
use Symfony\Component\Validator\ConstraintViolationInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use XcoreCMS\InlineEditing\Exception\InvalidDataException;
use XcoreCMS\InlineEditing\Model\Entity\HtmlEntityElement\Element;
use XcoreCMS\InlineEditing\Model\Entity\HtmlEntityElement\ElementEntityBaseContainer;
use XcoreCMS\InlineEditing\Model\Entity\HtmlEntityElement\ElementEntityContainer;
use XcoreCMS\InlineEditing\Model\Entity\Mapper\InlineMapperInterface;
/**
* @author Jakub Janata <jakubjanata@gmail.com>
*/
class EntityPersister
{
/** @var EntityManager */
private $entityManager;
/** @var ValidatorInterface|null */
private $validator;
/** @var ElementEntityContainer[] */
private $entityElementContainers;
/** @var PropertyAccessor */
private $propertyAccessor;
/**
* @param EntityManager $entityManager
* @param ValidatorInterface $validator
*/
public function __construct(EntityManager $entityManager, ValidatorInterface $validator = null)
{
$this->entityManager = $entityManager;
$this->validator = $validator;
$this->entityElementContainers = [];
$this->propertyAccessor = new PropertyAccessor;
}
/**
* @param Element $element
* @param callable|null $onBeforeValidation
*/
public function update(Element $element, callable $onBeforeValidation = null)
{
$key = $element->getEntityHash();
$container = $this->entityElementContainers[$key] ?? null;
if ($container === null) {
// load entity from database
$e = $this->entityManager->find($element->getClassName(), $element->getId());
$container = new ElementEntityContainer($e);
$this->entityElementContainers[$key] = $container;
}
$container->addElement($element);
$entity = $container->getEntity();
if (is_callable($onBeforeValidation)) {
$message = $onBeforeValidation($entity);
if ($message) {
$element->setError(3, $message);
}
}
$property = $element->getProperty();
$value = $element->getValue();
// validate symfony
if ($this->validator !== null) {
$violations = $this->validator->validatePropertyValue($entity, $property, $value);
if (count($violations)) {
$element->setError(2, $violations[0]->getMessage());
}
}
// validate and set user custom
if ($entity instanceof InlineMapperInterface) {
try {
$entity->setInlineData($property, $value);
} catch (InvalidDataException $exception) {
$element->setError(2, $exception->getMessage());
return;
}
} else {
$this->propertyAccessor->setValue($entity, $property, $value);
}
}
/**
* @return ElementEntityBaseContainer
*/
public function flush(): ElementEntityBaseContainer
{
foreach ($this->entityElementContainers as $container) {
$entity = $container->getEntity();
// validate symfony
if ($this->validator !== null) {
$violations = $this->validator->validate($entity);
if (count($violations)) {
/** @var ConstraintViolationInterface $violation */
$violation = $violations[0];
trigger_error(sprintf(
'Constraint violation in %s::$%s. Returned message "%s" for given %s %s.',
get_class($violation->getRoot()),
$violation->getPropertyPath(),
$violation->getMessage(),
gettype($violation->getInvalidValue()),
var_export($violation->getInvalidValue(), true)
));
$container->setInvalid($violation->getMessage());
$this->entityManager->detach($entity);
continue;
}
}
// revalidate
foreach ($container->getElements() as $element) {
if ($element->isValid() === false) {
$container->setInvalid();
$this->entityManager->detach($entity);
continue 2;
}
}
// ok
$this->entityManager->flush($entity);
}
return new ElementEntityBaseContainer($this->entityElementContainers);
}
}