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 ( d399db...857eb8 )
by Steevan
02:14
created

ReadOnlySubscriber   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
c 2
b 0
f 0
lcom 0
cbo 6
dl 0
loc 49
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedEvents() 0 4 1
A prePersist() 0 6 2
A preFlush() 0 14 3
A isReadOnlyEntity() 0 4 1
1
<?php
2
3
namespace steevanb\DoctrineReadOnlyHydrator\EventSubscriber;
4
5
use Doctrine\Common\EventSubscriber;
6
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
7
use Doctrine\ORM\Event\PreFlushEventArgs;
8
use Doctrine\ORM\Events;
9
use steevanb\DoctrineReadOnlyHydrator\Entity\ReadOnlyEntityInterface;
10
use steevanb\DoctrineReadOnlyHydrator\Exception\ReadOnlyEntityCantBeFlushedException;
11
use steevanb\DoctrineReadOnlyHydrator\Exception\ReadOnlyEntityCantBePersistedException;
12
13
class ReadOnlySubscriber implements EventSubscriber
14
{
15
    /**
16
     * @return array
17
     */
18
    public function getSubscribedEvents()
19
    {
20
        return array(Events::prePersist, Events::preFlush);
21
    }
22
23
    /**
24
     * @param LifecycleEventArgs $args
25
     * @throws ReadOnlyEntityCantBePersistedException
26
     */
27
    public function prePersist(LifecycleEventArgs $args)
28
    {
29
        if ($this->isReadOnlyEntity($args->getObject())) {
30
            throw new ReadOnlyEntityCantBePersistedException($args->getObject());
31
        }
32
    }
33
34
    /**
35
     * @param PreFlushEventArgs $args
36
     * @throws ReadOnlyEntityCantBeFlushedException
37
     */
38
    public function preFlush(PreFlushEventArgs $args)
39
    {
40
        $unitOfWork = $args->getEntityManager()->getUnitOfWork();
41
        $entities = array_merge(
42
            $unitOfWork->getScheduledEntityInsertions(),
43
            $unitOfWork->getScheduledEntityUpdates(),
44
            $unitOfWork->getScheduledEntityDeletions()
45
        );
46
        foreach ($entities as $entity) {
47
            if ($this->isReadOnlyEntity($entity)) {
48
                throw new ReadOnlyEntityCantBeFlushedException($entity);
49
            }
50
        }
51
    }
52
53
    /**
54
     * @param object $entity
55
     * @return bool
56
     */
57
    protected function isReadOnlyEntity($entity)
58
    {
59
        return $entity instanceof ReadOnlyEntityInterface;
60
    }
61
}
62