Completed
Push — master ( b14e26...fac3e1 )
by Guillaume
13:47
created

EntitySubscriber::prePersist()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
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\UtilisateurInterface;
12
13
//
14
class EntitySubscriber implements EventSubscriber
15
{
16
17
    /**
18
     * @var UtilisateurInterface
19
     */
20
    protected $utilisateur;
21
22
    public function __construct($utilisateur)
23
    {
24
        $this->utilisateur = $utilisateur;
25
    }
26
27
    public function getSubscribedEvents()
28
    {
29
        return [
30
            'prePersist',
31
            'onFlush',
32
        ];
33
    }
34
35
    public function prePersist(LifecycleEventArgs $args)
36
    {
37
        $entity = $args->getEntity();
38
39
        if (!$entity instanceof Entity) {
40
            return false;
41
        }
42
43
        $this->traitement($entity);
44
45
        if (empty($entity->getCreatedAt())) {
46
            $entity->setCreatedAt(new DateTime());
47
        }
48
    }
49
50
    private function traitement(Entity $entity)
51
    {
52
        if (empty($entity->getUuid())) {
53
            $uid = Uuid::uuid4();
54
            $entity->setUuid($uid);
0 ignored issues
show
Documentation introduced by
$uid 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...
55
        }
56
        if (empty($entity->getUtilisateur())) {
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Starkerxp\StructureBundle\Entity\Entity as the method getUtilisateur() does only exist in the following sub-classes of Starkerxp\StructureBundle\Entity\Entity: Starkerxp\CampagneBundle\Entity\Campagne, Starkerxp\CampagneBundle\Entity\Cible, Starkerxp\CampagneBundle\Entity\Event, Starkerxp\CampagneBundle\Entity\Template, Starkerxp\StructureBundle\Entity\UtilisateurEntity, Starkerxp\UtilisateurBundle\Entity\RoleUtilisateur. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
57
            $entity->setUtilisateur($this->utilisateur);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Starkerxp\StructureBundle\Entity\Entity as the method setUtilisateur() does only exist in the following sub-classes of Starkerxp\StructureBundle\Entity\Entity: Starkerxp\CampagneBundle\Entity\Campagne, Starkerxp\CampagneBundle\Entity\Cible, Starkerxp\CampagneBundle\Entity\Event, Starkerxp\CampagneBundle\Entity\Template, Starkerxp\StructureBundle\Entity\UtilisateurEntity, Starkerxp\UtilisateurBundle\Entity\RoleUtilisateur. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
58
        }
59
    }
60
61
    public function onFlush(OnFlushEventArgs $event)
62
    {
63
        $entityManager = $event->getEntityManager();
64
        $uow = $entityManager->getUnitOfWork();
65
        foreach ($uow->getScheduledEntityUpdates() as $entity) {
66
            if (!$entity instanceof Entity) {
67
                continue;
68
            }
69
            $this->traitement($entity);
70
            $entity->setUpdatedAt(new DateTime());
71
        }
72
    }
73
}
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...
74