Passed
Push — master ( 60b1cb...62a444 )
by Anthony
07:00
created

CreateUpdateAwareListener   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 11
dl 0
loc 32
rs 10
c 1
b 0
f 1
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A preUpdate() 0 4 1
A prePersist() 0 9 3
1
<?php
2
3
namespace PiouPiou\RibsAdminBundle\EventListener;
4
5
use PiouPiou\RibsAdminBundle\Entity\User;
6
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
7
8
class CreateUpdateAwareListener
9
{
10
	/**
11
	 * @var User
12
	 */
13
	private $user;
14
15
    /**
16
     * CreateUpdateAwareListener constructor.
17
     * @param TokenStorage $tokenStorage
18
     */
19
    public function __construct(TokenStorage $tokenStorage)
20
	{
21
        $this->user = $tokenStorage->getToken()->getUser()->getUser();
22
	}
23
	
24
	public function prePersist($entity)
25
	{
26
		if ($entity->getCreatedBy() === null) {
27
            $entity->setCreatedAt(new \DateTime());
28
            $entity->setCreatedBy($this->user);
29
		}
30
        if ($entity->getUpdatedBy() === null) {
31
            $entity->setUpdatedAt(new \DateTime());
32
            $entity->setUpdatedBy($this->user);
33
        }
34
	}
35
36
    public function preUpdate($entity)
37
    {
38
        $entity->setUpdatedAt(new \DateTime());
39
        $entity->setUpdatedBy($this->user);
40
    }
41
}
42