Test Failed
Push — develop ( d382d0...28e0cd )
by Daniel
10:59
created

TimestampedEntitySubscriber::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Silverback\ApiComponentBundle\EventSubscriber\EntitySubscriber;
4
5
use Doctrine\ORM\Event\LifecycleEventArgs;
6
use Doctrine\ORM\Events;
7
use Silverback\ApiComponentBundle\Entity\TimestampedEntityInterface;
8
9
class TimestampedEntitySubscriber implements EntitySubscriberInterface
10
{
11
    public function supportsEntity($entity = null): bool
12
    {
13
        return $entity instanceof TimestampedEntityInterface;
14
    }
15
16
    public function getSubscribedEvents(): array
17
    {
18
        return [
19
            Events::prePersist
20
        ];
21
    }
22
23
    public function prePersist(LifecycleEventArgs $args, TimestampedEntityInterface $entity): void
24
    {
25
        $entityManager = $args->getEntityManager();
26
        if (!$entityManager->contains($entity)) {
27
            $entity->setCreated(new \DateTimeImmutable());
28
        }
29
        $entity->setModified(new \DateTime());
30
    }
31
}
32