EntitySubscriber::prePersist()   B
last analyzed

Complexity

Conditions 8
Paths 12

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 7.7777
c 0
b 0
f 0
cc 8
eloc 12
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\AbstractEntity;
11
use Starkerxp\StructureBundle\Entity\TimestampInterface;
12
use Starkerxp\StructureBundle\Entity\AbstractUser;
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 $user;
27
28
    /**
29
     * @var EventDispatcher
30
     */
31
    protected $eventDispatcher;
32
33
    public function __construct($user, $eventDispatcher)
34
    {
35
        $this->user = $user;
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 TimestampInterface )&& empty($entity->getCreatedAt())) {
52
            $entity->setCreatedAt(new DateTime());
53
        }
54
        if ($entity instanceof AbstractEntity && 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 AbstractUser) {
59
            return false;
60
        }
61
        if (empty($entity->getUser()) && $user = $this->getUser()) {
62
            $entity->setUser($user);
63
        }
64
65
        return true;
66
    }
67
68
    protected function getUser()
69
    {
70
        if (!$token = $this->user->getToken()) {
71
            return null;
72
        }
73
74
        return $token->getUser();
75
    }
76
77
    public function onFlush(OnFlushEventArgs $event)
78
    {
79
        $entityManager = $event->getEntityManager();
80
        $uow = $entityManager->getUnitOfWork();
81
        foreach ($uow->getScheduledEntityUpdates() as $entity) {
82
            if ($entity instanceof EntityReadOnlyInterface) {
83
                $uow->detach($entity);
84
                $this->eventDispatcher->dispatch(Events::ENTITY_READ_ONLY, new GenericEvent($entity));
85
                continue;
86
            }
87
            if ($entity instanceof TimestampInterface) {
88
                $entity->setUpdatedAt(new DateTime());
89
            }
90
            if ($entity instanceof AbstractEntity && empty($entity->getUuid())) {
91
                $uuid = Uuid::uuid4();
92
                $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...
93
            }
94
            if (!$entity instanceof AbstractUser) {
95
                continue;
96
            }
97
            if (empty($entity->getUser()) && $user = $this->getUser()) {
98
                $entity->setUser($user);
99
            }
100
        }
101
    }
102
}
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...
103