1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | /* |
||
6 | * (c) Christian Gripp <[email protected]> |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | namespace Core23\Doctrine\EventListener\ORM; |
||
13 | |||
14 | use Core23\Doctrine\Model\PositionAwareInterface; |
||
15 | use Core23\Doctrine\Model\Traits\SortableTrait; |
||
16 | use Core23\Doctrine\Util\ClassUtils; |
||
17 | use Doctrine\Common\EventSubscriber; |
||
18 | use Doctrine\ORM\EntityManager; |
||
19 | use Doctrine\ORM\Event\LifecycleEventArgs; |
||
20 | use Doctrine\ORM\Event\LoadClassMetadataEventArgs; |
||
21 | use Doctrine\ORM\Event\PreUpdateEventArgs; |
||
22 | use Doctrine\ORM\Events; |
||
23 | use Doctrine\ORM\Mapping\ClassMetadata; |
||
24 | use Doctrine\ORM\Mapping\MappingException; |
||
25 | use Doctrine\ORM\NonUniqueResultException; |
||
26 | use Doctrine\ORM\QueryBuilder; |
||
27 | use Doctrine\ORM\UnitOfWork; |
||
28 | use LogicException; |
||
29 | use Symfony\Component\PropertyAccess\PropertyAccess; |
||
30 | use Symfony\Component\PropertyAccess\PropertyAccessor; |
||
31 | |||
32 | final class SortableListener implements EventSubscriber |
||
33 | { |
||
34 | /** |
||
35 | * @var PropertyAccessor |
||
36 | */ |
||
37 | private $propertyAccessor; |
||
38 | |||
39 | /** |
||
40 | * @param PropertyAccessor $propertyAccessor |
||
41 | */ |
||
42 | public function __construct(PropertyAccessor $propertyAccessor = null) |
||
43 | { |
||
44 | if (null === $propertyAccessor) { |
||
45 | $propertyAccessor = PropertyAccess::createPropertyAccessor(); |
||
46 | } |
||
47 | |||
48 | $this->propertyAccessor = $propertyAccessor; |
||
49 | } |
||
50 | |||
51 | public function getSubscribedEvents() |
||
52 | { |
||
53 | return [ |
||
54 | Events::prePersist, |
||
55 | Events::preUpdate, |
||
56 | Events::preRemove, |
||
57 | Events::loadClassMetadata, |
||
58 | ]; |
||
59 | } |
||
60 | |||
61 | public function prePersist(LifecycleEventArgs $args): void |
||
62 | { |
||
63 | if (!$args->getEntity() instanceof PositionAwareInterface) { |
||
64 | return; |
||
65 | } |
||
66 | |||
67 | $this->uniquePosition($args); |
||
68 | } |
||
69 | |||
70 | public function preUpdate(PreUpdateEventArgs $args): void |
||
71 | { |
||
72 | if (!$args->getEntity() instanceof PositionAwareInterface) { |
||
73 | return; |
||
74 | } |
||
75 | |||
76 | $position = $args->getEntity()->getPosition(); |
||
77 | |||
78 | if ($args->hasChangedField('position')) { |
||
79 | $position = $args->getOldValue('position'); |
||
80 | } |
||
81 | |||
82 | $this->uniquePosition($args, $position); |
||
83 | } |
||
84 | |||
85 | public function preRemove(LifecycleEventArgs $args): void |
||
86 | { |
||
87 | $entity = $args->getEntity(); |
||
88 | |||
89 | if ($entity instanceof PositionAwareInterface) { |
||
90 | $this->movePosition($args->getEntityManager(), $entity, -1); |
||
91 | } |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * @throws MappingException |
||
96 | */ |
||
97 | public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs): void |
||
98 | { |
||
99 | $meta = $eventArgs->getClassMetadata(); |
||
100 | |||
101 | if (!$meta instanceof ClassMetadata) { |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
102 | throw new LogicException('Class metadata was no ORM'); |
||
103 | } |
||
104 | |||
105 | $reflClass = $meta->getReflectionClass(); |
||
106 | |||
107 | if (null === $reflClass || !ClassUtils::containsTrait($reflClass, SortableTrait::class)) { |
||
108 | return; |
||
109 | } |
||
110 | |||
111 | if (!$meta->hasField('position')) { |
||
112 | $meta->mapField([ |
||
113 | 'type' => 'integer', |
||
114 | 'fieldName' => 'position', |
||
115 | ]); |
||
116 | } |
||
117 | } |
||
118 | |||
119 | private function uniquePosition(LifecycleEventArgs $args, ?int $oldPosition = null): void |
||
120 | { |
||
121 | $entity = $args->getEntity(); |
||
122 | |||
123 | if ($entity instanceof PositionAwareInterface) { |
||
124 | if (null === $entity->getPosition()) { |
||
125 | $position = $this->getNextPosition($args->getEntityManager(), $entity); |
||
126 | $entity->setPosition($position); |
||
127 | } elseif ($oldPosition && $oldPosition !== $entity->getPosition()) { |
||
0 ignored issues
–
show
The expression
$oldPosition of type integer|null is loosely compared to true ; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.
In PHP, under loose comparison (like For 0 == false // true
0 == null // true
123 == false // false
123 == null // false
// It is often better to use strict comparison
0 === false // false
0 === null // false
Loading history...
|
|||
128 | $this->movePosition($args->getEntityManager(), $entity); |
||
129 | } |
||
130 | } |
||
131 | } |
||
132 | |||
133 | private function movePosition(EntityManager $em, PositionAwareInterface $entity, int $direction = 1): void |
||
134 | { |
||
135 | $uow = $em->getUnitOfWork(); |
||
136 | $meta = $em->getClassMetadata(\get_class($entity)); |
||
137 | |||
138 | $qb = $em->createQueryBuilder() |
||
139 | ->update($meta->getName(), 'e') |
||
140 | ->set('e.position', 'e.position + '.$direction) |
||
141 | ; |
||
142 | |||
143 | if ($direction > 0) { |
||
144 | $qb->andWhere('e.position <= :position')->setParameter('position', $entity->getPosition()); |
||
145 | } elseif ($direction < 0) { |
||
146 | $qb->andWhere('e.position >= :position')->setParameter('position', $entity->getPosition()); |
||
147 | } else { |
||
148 | return; |
||
149 | } |
||
150 | |||
151 | $this->addGroupFilter($qb, $entity, $uow); |
||
152 | |||
153 | $qb->getQuery()->execute(); |
||
154 | } |
||
155 | |||
156 | private function getNextPosition(EntityManager $em, PositionAwareInterface $entity): int |
||
157 | { |
||
158 | $meta = $em->getClassMetadata(\get_class($entity)); |
||
159 | |||
160 | $qb = $em->createQueryBuilder() |
||
161 | ->select('e') |
||
162 | ->from($meta->getName(), 'e') |
||
163 | ->addOrderBy('e.position', 'DESC') |
||
164 | ->setMaxResults(1) |
||
165 | ; |
||
166 | |||
167 | $this->addGroupFilter($qb, $entity); |
||
168 | |||
169 | try { |
||
170 | $result = $qb->getQuery()->getOneOrNullResult(); |
||
171 | |||
172 | return ($result instanceof PositionAwareInterface ? $result->getPosition() : 0) + 1; |
||
173 | } catch (NonUniqueResultException $ignored) { |
||
174 | return 0; |
||
175 | } |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * @param UnitOfWork $uow |
||
180 | */ |
||
181 | private function addGroupFilter(QueryBuilder $qb, PositionAwareInterface $entity, UnitOfWork $uow = null): void |
||
182 | { |
||
183 | foreach ($entity->getPositionGroup() as $field) { |
||
184 | $value = $this->propertyAccessor->getValue($entity, $field); |
||
185 | |||
186 | if (\is_object($value) && (null === $uow || null === $uow->getSingleIdentifierValue($value))) { |
||
187 | continue; |
||
188 | } |
||
189 | |||
190 | $qb->andWhere('e.'.$field.' = :'.$field)->setParameter($field, $value); |
||
191 | } |
||
192 | } |
||
193 | } |
||
194 |