Passed
Push — master ( d70c89...2d756e )
by Christian
01:55
created

UniqueActiveListener::uniqueActive()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 31
rs 9.7
c 0
b 0
f 0
cc 4
nc 4
nop 1
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\UniqueActiveInterface;
15
use Doctrine\Common\EventSubscriber;
16
use Doctrine\ORM\Event\LifecycleEventArgs;
17
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
18
use Doctrine\ORM\Events;
19
use Doctrine\ORM\Mapping\ClassMetadata;
20
use Doctrine\ORM\Mapping\MappingException;
21
use Doctrine\ORM\QueryBuilder;
22
use Doctrine\ORM\UnitOfWork;
23
use LogicException;
24
use Symfony\Component\PropertyAccess\PropertyAccess;
25
use Symfony\Component\PropertyAccess\PropertyAccessor;
26
27
final class UniqueActiveListener implements EventSubscriber
28
{
29
    /**
30
     * @var PropertyAccessor
31
     */
32
    private $propertyAccessor;
33
34
    /**
35
     * @param PropertyAccessor $propertyAccessor
36
     */
37
    public function __construct(PropertyAccessor $propertyAccessor = null)
38
    {
39
        if (null === $propertyAccessor) {
40
            $propertyAccessor = PropertyAccess::createPropertyAccessor();
41
        }
42
43
        $this->propertyAccessor = $propertyAccessor;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function getSubscribedEvents()
50
    {
51
        return [
52
            Events::prePersist,
53
            Events::preUpdate,
54
            Events::loadClassMetadata,
55
        ];
56
    }
57
58
    /**
59
     * @param LifecycleEventArgs $args
60
     */
61
    public function prePersist(LifecycleEventArgs $args): void
62
    {
63
        $this->uniqueActive($args);
64
    }
65
66
    /**
67
     * @param LifecycleEventArgs $args
68
     */
69
    public function preUpdate(LifecycleEventArgs $args): void
70
    {
71
        $this->uniqueActive($args);
72
    }
73
74
    /**
75
     * @param LoadClassMetadataEventArgs $eventArgs
76
     *
77
     * @throws MappingException
78
     */
79
    public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs): void
80
    {
81
        $meta = $eventArgs->getClassMetadata();
82
83
        if (!$meta instanceof ClassMetadata) {
0 ignored issues
show
introduced by core23
$meta is always a sub-type of Doctrine\ORM\Mapping\ClassMetadata.
Loading history...
84
            throw new LogicException(sprintf('Class metadata was no ORM but %s', \get_class($meta)));
85
        }
86
87
        $reflClass = $meta->getReflectionClass();
88
89
        if (null === $reflClass || !$reflClass->implementsInterface(UniqueActiveInterface::class)) {
90
            return;
91
        }
92
93
        if (!$meta->hasField('active')) {
94
            $meta->mapField([
95
                'type'      => 'integer',
96
                'fieldName' => 'active',
97
            ]);
98
        }
99
    }
100
101
    /**
102
     * @param LifecycleEventArgs $args
103
     */
104
    private function uniqueActive(LifecycleEventArgs $args): void
105
    {
106
        $entity = $args->getEntity();
107
108
        if (!$entity instanceof UniqueActiveInterface) {
109
            return;
110
        }
111
112
        if (!$entity->isActive()) {
113
            return;
114
        }
115
116
        $em   = $args->getEntityManager();
117
        $uow  = $em->getUnitOfWork();
118
        $meta = $em->getClassMetadata(\get_class($entity));
119
120
        $qb = $em->createQueryBuilder()
121
            ->update($meta->getName(), 'e')
122
            ->set('e.active', 'false')
123
            ->andWhere('e.active = true')
124
        ;
125
126
        foreach ($meta->getIdentifier() as $key) {
127
            $qb->andWhere(sprintf('e.%s != :%s', $key, $key))
128
                ->setParameter($key, $this->propertyAccessor->getValue($entity, $key))
129
            ;
130
        }
131
132
        $this->addFieldFilter($qb, $entity, $uow);
133
134
        $qb->getQuery()->execute();
135
    }
136
137
    /**
138
     * @param QueryBuilder          $qb
139
     * @param UniqueActiveInterface $entity
140
     * @param UnitOfWork            $uow
141
     */
142
    private function addFieldFilter(QueryBuilder $qb, UniqueActiveInterface $entity, UnitOfWork $uow): void
143
    {
144
        foreach ($entity->getUniqueActiveFields() as $field) {
145
            $value = $this->propertyAccessor->getValue($entity, $field);
146
147
            if (\is_object($value) && null === $uow->getSingleIdentifierValue($value)) {
148
                continue;
149
            }
150
151
            $qb->andWhere('e.'.$field.' = :'.$field)->setParameter($field, $value);
152
        }
153
    }
154
}
155