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 ( 200c7c...28aba2 )
by Steevan
19:39
created

ReadOnlySubscriber::prePersist()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
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
    /**
18
     * @return array
19
     */
20
    public function getSubscribedEvents()
21
    {
22
        return [
23
            Events::prePersist,
24
            Events::preFlush,
25
            Events::onClassMetadataNotFound
26
        ];
27
    }
28
29
    /**
30
     * @param LifecycleEventArgs $args
31
     * @throws ReadOnlyEntityCantBePersistedException
32
     */
33
    public function prePersist(LifecycleEventArgs $args)
34
    {
35
        if ($this->isReadOnlyEntity($args->getObject())) {
36
            throw new ReadOnlyEntityCantBePersistedException($args->getObject());
37
        }
38
    }
39
40
    /**
41
     * @param PreFlushEventArgs $args
42
     * @throws ReadOnlyEntityCantBeFlushedException
43
     */
44
    public function preFlush(PreFlushEventArgs $args)
45
    {
46
        $unitOfWork = $args->getEntityManager()->getUnitOfWork();
47
        $entities = array_merge(
48
            $unitOfWork->getScheduledEntityInsertions(),
49
            $unitOfWork->getScheduledEntityUpdates(),
50
            $unitOfWork->getScheduledEntityDeletions()
51
        );
52
        foreach ($entities as $entity) {
53
            if ($this->isReadOnlyEntity($entity)) {
54
                throw new ReadOnlyEntityCantBeFlushedException($entity);
55
            }
56
        }
57
    }
58
59
    /**
60
     * @param OnClassMetadataNotFoundEventArgs $eventArgs
61
     */
62
    public function onClassMetadataNotFound(OnClassMetadataNotFoundEventArgs $eventArgs)
63
    {
64
        if (class_implements(
65
            $eventArgs->getClassName(),
66
            'steevanb\\DoctrineReadOnlyHydrator\\Entity\\ReadOnlyEntityInterface'
67
        )) {
68
            $eventArgs->setFoundMetadata(
69
                $eventArgs->getObjectManager()->getClassMetadata(get_parent_class($eventArgs->getClassName()))
70
            );
71
        }
72
    }
73
74
    /**
75
     * @param object $entity
76
     * @return bool
77
     */
78
    protected function isReadOnlyEntity($entity)
79
    {
80
        return
81
            $entity instanceof ReadOnlyEntityInterface
82
            || isset($entity->{SimpleObjectHydrator::READ_ONLY_PROPERTY});
83
    }
84
}
85