CreateUpdateAwareListener   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 16
c 2
b 0
f 1
dl 0
loc 39
rs 10
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A preUpdate() 0 5 2
A prePersist() 0 10 4
A __construct() 0 6 4
1
<?php
2
3
namespace PiouPiou\RibsAdminBundle\EventListener;
4
5
use PiouPiou\RibsAdminBundle\Entity\User;
6
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
7
8
class CreateUpdateAwareListener
9
{
10
    /**
11
     * @var User
12
     */
13
    private $user;
14
15
    /**
16
     * CreateUpdateAwareListener constructor.
17
     * @param TokenStorageInterface $tokenStorage
18
     */
19
    public function __construct(TokenStorageInterface $tokenStorage)
20
    {
21
        if ($tokenStorage->getToken() && is_object($tokenStorage->getToken()->getUser()) && $tokenStorage->getToken()->getUser()->getUser()) {
0 ignored issues
show
Bug introduced by
The method getUser() does not exist on Stringable. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

21
        if ($tokenStorage->getToken() && is_object($tokenStorage->getToken()->getUser()) && $tokenStorage->getToken()->getUser()->/** @scrutinizer ignore-call */ getUser()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method getUser() does not exist on Symfony\Component\Security\Core\User\UserInterface. Did you maybe mean getUsername()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

21
        if ($tokenStorage->getToken() && is_object($tokenStorage->getToken()->getUser()) && $tokenStorage->getToken()->getUser()->/** @scrutinizer ignore-call */ getUser()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
22
            $this->user = $tokenStorage->getToken()->getUser()->getUser();
23
        } else {
24
            $this->user = null;
25
        }
26
    }
27
28
    public function prePersist($entity)
29
    {
30
        if ($this->user) {
31
            if ($entity->getCreatedBy() === null) {
32
                $entity->setCreatedAt(new \DateTime());
33
                $entity->setCreatedBy($this->user);
34
            }
35
            if ($entity->getUpdatedBy() === null) {
36
                $entity->setUpdatedAt(new \DateTime());
37
                $entity->setUpdatedBy($this->user);
38
            }
39
        }
40
    }
41
42
    public function preUpdate($entity)
43
    {
44
        if ($this->user) {
45
            $entity->setUpdatedAt(new \DateTime());
46
            $entity->setUpdatedBy($this->user);
47
        }
48
    }
49
}
50