Passed
Push — master ( f06103...576e75 )
by Gerhard
10:12
created

TimestampableSubscriber::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: gseidel
5
 * Date: 2019-08-26
6
 * Time: 00:45
7
 */
8
9
namespace Enhavo\Bundle\AppBundle\EventListener;
10
11
use Enhavo\Bundle\AppBundle\Event\ResourceEvent;
12
use Enhavo\Bundle\AppBundle\Event\ResourceEvents;
13
use Enhavo\Bundle\AppBundle\Model\Timestampable;
14
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
15
16
17
class TimestampableSubscriber implements EventSubscriberInterface
18
{
19
    public static function getSubscribedEvents()
20
    {
21
        return array(
22
            ResourceEvents::PRE_CREATE => 'preSave',
23
            ResourceEvents::PRE_UPDATE => 'preSave'
24
        );
25
    }
26
27
    public function preSave(ResourceEvent $event)
28
    {
29
        $resource = $event->getSubject();
30
31
        if ($resource instanceof Timestampable) {
32
            $now = new \DateTime();
33
            $resource->setUpdatedAt($now);
34
            if ($resource->getCreatedAt() === null) {
35
                $resource->setCreatedAt($now);
36
            }
37
        }
38
    }
39
}
40