Passed
Push — master ( ab171b...3e1bb9 )
by Philip
14:43
created

CreatedEntityListener::prePersist()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Dontdrinkandroot\Event\Listener;
4
5
use DateTime;
6
use Doctrine\ORM\Event\LifecycleEventArgs;
7
use Dontdrinkandroot\Entity\CreatedEntityInterface;
8
9
/**
10
 * @author Philip Washington Sorst <[email protected]>
11
 */
12
class CreatedEntityListener
13
{
14 28
    public function prePersist(LifecycleEventArgs $args): void
15
    {
16 28
        $entity = $args->getEntity();
17
18 28
        if (is_a($entity, CreatedEntityInterface::class)) {
19
            /** @var CreatedEntityInterface $createdEntity */
20 28
            $createdEntity = $entity;
21 28
            if (null === $createdEntity->getCreated()) {
22 28
                $createdEntity->setCreated(new DateTime());
23
            }
24
        }
25 28
    }
26
}
27