GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 34546e...94b458 )
by
unknown
13s
created

src/EventListener/ORM/UniqueActiveListener.php (1 issue)

Severity
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
    public function getSubscribedEvents()
47
    {
48
        return [
49
            Events::prePersist,
50
            Events::preUpdate,
51
            Events::loadClassMetadata,
52
        ];
53
    }
54
55
    public function prePersist(LifecycleEventArgs $args): void
56
    {
57
        $this->uniqueActive($args);
58
    }
59
60
    public function preUpdate(LifecycleEventArgs $args): void
61
    {
62
        $this->uniqueActive($args);
63
    }
64
65
    /**
66
     * @throws MappingException
67
     */
68
    public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs): void
69
    {
70
        $meta = $eventArgs->getClassMetadata();
71
72
        if (!$meta instanceof ClassMetadata) {
0 ignored issues
show
$meta is always a sub-type of Doctrine\ORM\Mapping\ClassMetadata.
Loading history...
73
            throw new LogicException('Class metadata was no ORM');
74
        }
75
76
        $reflClass = $meta->getReflectionClass();
77
78
        if (null === $reflClass || !$reflClass->implementsInterface(UniqueActiveInterface::class)) {
79
            return;
80
        }
81
82
        if (!$meta->hasField('active')) {
83
            $meta->mapField([
84
                'type'      => 'integer',
85
                'fieldName' => 'active',
86
            ]);
87
        }
88
    }
89
90
    private function uniqueActive(LifecycleEventArgs $args): void
91
    {
92
        $entity = $args->getEntity();
93
94
        if (!$entity instanceof UniqueActiveInterface) {
95
            return;
96
        }
97
98
        if (!$entity->isActive()) {
99
            return;
100
        }
101
102
        $em   = $args->getEntityManager();
103
        $uow  = $em->getUnitOfWork();
104
        $meta = $em->getClassMetadata(\get_class($entity));
105
106
        $qb = $em->createQueryBuilder()
107
            ->update($meta->getName(), 'e')
108
            ->set('e.active', 'false')
109
            ->andWhere('e.active = true')
110
        ;
111
112
        foreach ($meta->getIdentifier() as $key) {
113
            $qb->andWhere(sprintf('e.%s != :%s', $key, $key))
114
                ->setParameter($key, $this->propertyAccessor->getValue($entity, $key))
115
            ;
116
        }
117
118
        $this->addFieldFilter($qb, $entity, $uow);
119
120
        $qb->getQuery()->execute();
121
    }
122
123
    private function addFieldFilter(QueryBuilder $qb, UniqueActiveInterface $entity, UnitOfWork $uow): void
124
    {
125
        foreach ($entity->getUniqueActiveFields() as $field) {
126
            $value = $this->propertyAccessor->getValue($entity, $field);
127
128
            if (\is_object($value) && null === $uow->getSingleIdentifierValue($value)) {
129
                continue;
130
            }
131
132
            $qb->andWhere('e.'.$field.' = :'.$field)->setParameter($field, $value);
133
        }
134
    }
135
}
136