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 ( 688b2c...d1fd4e )
by Steevan
06:31
created

ReadOnlySubscriber::postLoad()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
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
        if (class_implements(
62
            $eventArgs->getClassName(),
63
            'steevanb\\DoctrineReadOnlyHydrator\\Entity\\ReadOnlyEntityInterface'
64
        )) {
65
            $eventArgs->setFoundMetadata(
66
                $eventArgs->getObjectManager()->getClassMetadata(get_parent_class($eventArgs->getClassName()))
67
            );
68
        }
69
    }
70
71
    /** @param LifecycleEventArgs $eventArgs */
72
    public function postLoad(LifecycleEventArgs $eventArgs)
73
    {
74
        if ($eventArgs->getObject() instanceof ReadOnlyEntityInterface) {
75
            // add ReadOnlyProxy to classMetada list
76
            // without it, you can't use Doctrine automatic id finder
77
            // like $queryBuilder->setParameter('foo', $foo)
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
78
            // instead of  $queryBuilder->setParameter('foo', $foo->getId())
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
79
            $eventArgs->getObjectManager()->getClassMetadata(get_class($eventArgs->getObject()));
80
        }
81
    }
82
83
    /**
84
     * @param object $entity
85
     * @return bool
86
     */
87
    protected function isReadOnlyEntity($entity)
88
    {
89
        return
90
            $entity instanceof ReadOnlyEntityInterface
91
            || isset($entity->{SimpleObjectHydrator::READ_ONLY_PROPERTY});
92
    }
93
}
94