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

CreateUpdateAwareListener::prePersist()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 6
nc 4
nop 1
dl 0
loc 9
rs 10
c 1
b 0
f 1
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