Completed
Push — master ( db4111...70a8a6 )
by Guillaume
02:27
created

EntitySubscriber::prePersist()   B

Complexity

Conditions 8
Paths 12

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 18
rs 7.7777
c 1
b 0
f 0
cc 8
eloc 11
nc 12
nop 1
1
<?php
2
3
namespace Starkerxp\StructureBundle\Listener;
4
5
use DateTime;
6
use Doctrine\Common\EventSubscriber;
7
use Doctrine\ORM\Event\LifecycleEventArgs;
8
use Doctrine\ORM\Event\OnFlushEventArgs;
9
use Ramsey\Uuid\Uuid;
10
use Starkerxp\StructureBundle\Entity\Entity;
11
use Starkerxp\StructureBundle\Entity\TimestampEntity;
12
use Starkerxp\StructureBundle\Entity\UtilisateurEntity;
13
use Starkerxp\StructureBundle\Events;
14
use Starkerxp\StructureBundle\Manager\EntityReadOnlyInterface;
15
use Symfony\Component\EventDispatcher\EventDispatcher;
16
use Symfony\Component\EventDispatcher\GenericEvent;
17
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
18
19
//
20
class EntitySubscriber implements EventSubscriber
21
{
22
23
    /**
24
     * @var TokenStorage
25
     */
26
    protected $utilisateur;
27
28
    /**
29
     * @var EventDispatcher
30
     */
31
    protected $eventDispatcher;
32
33
    public function __construct($utilisateur, $eventDispatcher)
34
    {
35
        $this->utilisateur = $utilisateur;
36
        $this->eventDispatcher = $eventDispatcher;
37
    }
38
39
    public function getSubscribedEvents()
40
    {
41
        return [
42
            'prePersist',
43
            'onFlush',
44
        ];
45
    }
46
47
    public function prePersist(LifecycleEventArgs $args)
48
    {
49
        $entity = $args->getEntity();
50
51
        if ($entity instanceof TimestampEntity && empty($entity->getCreatedAt())) {
52
            $entity->setCreatedAt(new DateTime());
53
        }
54
        if ($entity instanceof Entity && empty($entity->getUuid())) {
55
            $uuid = Uuid::uuid4();
56
            $entity->setUuid($uuid);
0 ignored issues
show
Documentation introduced by
$uuid is of type object<Ramsey\Uuid\UuidInterface>, but the function expects a object<Starkerxp\StructureBundle\Entity\guid>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
57
        }
58
        if (!$entity instanceof UtilisateurEntity) {
59
            return false;
60
        }
61
        if (empty($entity->getUtilisateur()) && $utilisateur = $this->getUtilisateur()) {
62
            $entity->setUtilisateur($utilisateur);
63
        }
64
    }
65
66
    protected function getUtilisateur()
67
    {
68
        if (!$token = $this->utilisateur->getToken()) {
69
            return null;
70
        }
71
72
        return $token->getUser();
73
    }
74
75
    public function onFlush(OnFlushEventArgs $event)
76
    {
77
        $entityManager = $event->getEntityManager();
78
        $uow = $entityManager->getUnitOfWork();
79
        foreach ($uow->getScheduledEntityUpdates() as $entity) {
80
            if ($entity instanceof EntityReadOnlyInterface) {
81
                $uow->detach($entity);
82
                $this->eventDispatcher->dispatch(Events::ENTITY_READ_ONLY, new GenericEvent($entity));
83
                continue;
84
            }
85
            if ($entity instanceof TimestampEntity) {
86
                $entity->setUpdatedAt(new DateTime());
87
            }
88
            if ($entity instanceof Entity && empty($entity->getUuid())) {
89
                $uuid = Uuid::uuid4();
90
                $entity->setUuid($uuid);
0 ignored issues
show
Documentation introduced by
$uuid is of type object<Ramsey\Uuid\UuidInterface>, but the function expects a object<Starkerxp\StructureBundle\Entity\guid>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
91
            }
92
            if (!$entity instanceof UtilisateurEntity) {
93
                continue;
94
            }
95
            if (empty($entity->getUtilisateur()) && $utilisateur = $this->getUtilisateur()) {
96
                $entity->setUtilisateur($utilisateur);
97
            }
98
        }
99
    }
100
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
101