UpdatedEntityListener   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 81.25%

Importance

Changes 0
Metric Value
dl 0
loc 31
c 0
b 0
f 0
wmc 6
lcom 0
cbo 3
ccs 13
cts 16
cp 0.8125
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A prePersist() 0 4 1
A preUpdate() 0 4 1
A checkAndSetUpdated() 0 18 4
1
<?php
2
3
namespace Dontdrinkandroot\Event\Listener;
4
5
use DateTime;
6
use Doctrine\ORM\Event\LifecycleEventArgs;
7
use Dontdrinkandroot\Entity\UpdatedEntityInterface;
8
use Dontdrinkandroot\Entity\UpdatedTimestampEntityInterface;
9
10
/**
11
 * @author Philip Washington Sorst <[email protected]>
12
 */
13
class UpdatedEntityListener
14
{
15 32
    public function prePersist(LifecycleEventArgs $args): void
16
    {
17 32
        $this->checkAndSetUpdated($args);
18 32
    }
19
20 4
    public function preUpdate(LifecycleEventArgs $args): void
21
    {
22 4
        $this->checkAndSetUpdated($args);
23 4
    }
24
25 32
    protected function checkAndSetUpdated(LifecycleEventArgs $args): void
26
    {
27 32
        $entity = $args->getEntity();
28
29 32
        if (is_a($entity, UpdatedEntityInterface::class)) {
30
            /** @var UpdatedEntityInterface $updatedEntity */
31 32
            $updatedEntity = $entity;
32 32
            $updatedEntity->setUpdated(new DateTime());
33
        }
34
35 32
        if (is_a($entity, UpdatedTimestampEntityInterface::class)) {
36
            /** @var UpdatedTimestampEntityInterface $updatedTimestampEntity */
37
            $updatedTimestampEntity = $entity;
38
            if (null === $updatedTimestampEntity->getUpdatedTimestamp()) {
39
                $updatedTimestampEntity->setUpdatedTimestamp((int)(microtime(true) * 1000));
40
            }
41
        }
42 32
    }
43
}
44