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.

ReadOnlySubscriber::getSubscribedEvents()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace steevanb\DoctrineReadOnlyHydrator\EventSubscriber;
4
5
use Doctrine\ORM\Event\OnClassMetadataNotFoundEventArgs;
6
use steevanb\DoctrineReadOnlyHydrator\Hydrator\SimpleObjectHydrator;
7
use Doctrine\Common\EventSubscriber;
8
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
9
use Doctrine\ORM\Event\PreFlushEventArgs;
10
use Doctrine\ORM\Events;
11
use steevanb\DoctrineReadOnlyHydrator\Entity\ReadOnlyEntityInterface;
12
use steevanb\DoctrineReadOnlyHydrator\Exception\ReadOnlyEntityCantBeFlushedException;
13
use steevanb\DoctrineReadOnlyHydrator\Exception\ReadOnlyEntityCantBePersistedException;
14
15
class ReadOnlySubscriber implements EventSubscriber
16
{
17
    /** @return array */
18
    public function getSubscribedEvents()
19
    {
20
        return [
21
            Events::prePersist,
22
            Events::preFlush,
23
            Events::onClassMetadataNotFound,
24
            Events::postLoad
25
        ];
26
    }
27
28
    /**
29
     * @param LifecycleEventArgs $args
30
     * @throws ReadOnlyEntityCantBePersistedException
31
     */
32
    public function prePersist(LifecycleEventArgs $args)
33
    {
34
        if ($this->isReadOnlyEntity($args->getObject())) {
35
            throw new ReadOnlyEntityCantBePersistedException($args->getObject());
36
        }
37
    }
38
39
    /**
40
     * @param PreFlushEventArgs $args
41
     * @throws ReadOnlyEntityCantBeFlushedException
42
     */
43
    public function preFlush(PreFlushEventArgs $args)
44
    {
45
        $unitOfWork = $args->getEntityManager()->getUnitOfWork();
46
        $entities = array_merge(
47
            $unitOfWork->getScheduledEntityInsertions(),
48
            $unitOfWork->getScheduledEntityUpdates(),
49
            $unitOfWork->getScheduledEntityDeletions()
50
        );
51
        foreach ($entities as $entity) {
52
            if ($this->isReadOnlyEntity($entity)) {
53
                throw new ReadOnlyEntityCantBeFlushedException($entity);
54
            }
55
        }
56
    }
57
58
    /** @param OnClassMetadataNotFoundEventArgs $eventArgs */
59
    public function onClassMetadataNotFound(OnClassMetadataNotFoundEventArgs $eventArgs)
60
    {
61
        try {
62
            if (class_implements(
63
                $eventArgs->getClassName(),
64
                'steevanb\\DoctrineReadOnlyHydrator\\Entity\\ReadOnlyEntityInterface'
65
            )) {
66
                $eventArgs->setFoundMetadata(
67
                    $eventArgs->getObjectManager()->getClassMetadata(get_parent_class($eventArgs->getClassName()))
68
                );
69
            }
70
        } catch (\Exception $exception) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
71
    }
72
73
    /** @param LifecycleEventArgs $eventArgs */
74
    public function postLoad(LifecycleEventArgs $eventArgs)
75
    {
76
        if ($eventArgs->getObject() instanceof ReadOnlyEntityInterface) {
77
            // add ReadOnlyProxy to classMetada list
78
            // without it, you can't use Doctrine automatic id finder
79
            // like $queryBuilder->setParameter('foo', $foo)
80
            // instead of  $queryBuilder->setParameter('foo', $foo->getId())
81
            $eventArgs->getObjectManager()->getClassMetadata(get_class($eventArgs->getObject()));
82
        }
83
    }
84
85
    /**
86
     * @param object $entity
87
     * @return bool
88
     */
89
    protected function isReadOnlyEntity($entity)
90
    {
91
        return
92
            $entity instanceof ReadOnlyEntityInterface
93
            || isset($entity->{SimpleObjectHydrator::READ_ONLY_PROPERTY});
94
    }
95
}
96