CreatedEntityListener   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 72.72%

Importance

Changes 0
Metric Value
dl 0
loc 23
c 0
b 0
f 0
wmc 5
lcom 0
cbo 3
ccs 8
cts 11
cp 0.7272
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A prePersist() 0 20 5
1
<?php
2
3
namespace Dontdrinkandroot\Event\Listener;
4
5
use DateTime;
6
use Doctrine\ORM\Event\LifecycleEventArgs;
7
use Dontdrinkandroot\Entity\CreatedEntityInterface;
8
use Dontdrinkandroot\Entity\CreatedTimestampEntityInterface;
9
10
/**
11
 * @author Philip Washington Sorst <[email protected]>
12
 */
13
class CreatedEntityListener
14
{
15 32
    public function prePersist(LifecycleEventArgs $args): void
16
    {
17 32
        $entity = $args->getEntity();
18
19 32
        if (is_a($entity, CreatedEntityInterface::class)) {
20
            /** @var CreatedEntityInterface $createdEntity */
21 32
            $createdEntity = $entity;
22 32
            if (null === $createdEntity->getCreated()) {
23 32
                $createdEntity->setCreated(new DateTime());
24
            }
25
        }
26
27 32
        if (is_a($entity, CreatedTimestampEntityInterface::class)) {
28
            /** @var CreatedTimestampEntityInterface $createdTimestampEntity */
29
            $createdTimestampEntity = $entity;
30
            if (null === $createdTimestampEntity->getCreatedTimestamp()) {
31
                $createdTimestampEntity->setCreatedTimestamp((int)(microtime(true) * 1000));
32
            }
33
        }
34 32
    }
35
}
36