Completed
Push — master ( 11380b...3570da )
by Guillaume
03:20
created

EntitySubscriber   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 20
lcom 1
cbo 9
dl 0
loc 71
rs 10
c 1
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getSubscribedEvents() 0 7 1
B prePersist() 0 18 8
A getUtilisateur() 0 8 2
B onFlush() 0 20 8
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 Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
14
15
//
16
class EntitySubscriber implements EventSubscriber
17
{
18
19
    /**
20
     * @var TokenStorage
21
     */
22
    protected $utilisateur;
23
24
    public function __construct($utilisateur)
25
    {
26
        $this->utilisateur = $utilisateur;
27
28
    }
29
30
    public function getSubscribedEvents()
31
    {
32
        return [
33
            'prePersist',
34
            'onFlush',
35
        ];
36
    }
37
38
    public function prePersist(LifecycleEventArgs $args)
39
    {
40
        $entity = $args->getEntity();
41
42
        if ($entity instanceof TimestampEntity && empty($entity->getCreatedAt())) {
43
            $entity->setCreatedAt(new DateTime());
44
        }
45
        if ($entity instanceof Entity && empty($entity->getUuid())) {
46
            $uuid = Uuid::uuid4();
47
            $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...
48
        }
49
        if (!$entity instanceof UtilisateurEntity) {
50
            return false;
51
        }
52
        if (empty($entity->getUtilisateur()) && $utilisateur = $this->getUtilisateur()) {
53
            $entity->setUtilisateur($utilisateur);
54
        }
55
    }
56
57
    protected function getUtilisateur()
58
    {
59
        if (!$token = $this->utilisateur->getToken()) {
60
            return null;
61
        }
62
63
        return $token->getUser();
64
    }
65
66
    public function onFlush(OnFlushEventArgs $event)
67
    {
68
        $entityManager = $event->getEntityManager();
69
        $uow = $entityManager->getUnitOfWork();
70
        foreach ($uow->getScheduledEntityUpdates() as $entity) {
71
            if ($entity instanceof TimestampEntity) {
72
                $entity->setUpdatedAt(new DateTime());
73
            }
74
            if ($entity instanceof Entity && empty($entity->getUuid())) {
75
                $uuid = Uuid::uuid4();
76
                $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...
77
            }
78
            if (!$entity instanceof UtilisateurEntity) {
79
                continue;
80
            }
81
            if (empty($entity->getUtilisateur()) && $utilisateur = $this->getUtilisateur()) {
82
                $entity->setUtilisateur($utilisateur);
83
            }
84
        }
85
    }
86
}
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...
87