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

UpdatedEntityListener   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 2
dl 0
loc 23
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

3 Methods

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