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

UuidEntityListener   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 15
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 4
dl 0
loc 15
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A prePersist() 0 12 3
1
<?php
2
3
namespace Dontdrinkandroot\Event\Listener;
4
5
use Doctrine\ORM\Event\LifecycleEventArgs;
6
use Dontdrinkandroot\Entity\UuidEntityInterface;
7
use Ramsey\Uuid\Uuid;
8
9
/**
10
 * @author Philip Washington Sorst <[email protected]>
11
 */
12
class UuidEntityListener
13
{
14 28
    public function prePersist(LifecycleEventArgs $args): void
15
    {
16 28
        $entity = $args->getEntity();
17
18 28
        if (is_a($entity, UuidEntityInterface::class)) {
19
            /** @var UuidEntityInterface $uuidEntity */
20 28
            $uuidEntity = $entity;
21 28
            if (null === $uuidEntity->getUuid()) {
22 2
                $uuidEntity->setUuid(Uuid::uuid4()->toString());
23
            }
24
        }
25 28
    }
26
}
27