Passed
Push — v2 ( 36b0ac...56b613 )
by Daniel
04:35
created

TimestampedListener   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
dl 0
loc 20
ccs 0
cts 10
cp 0
rs 10
c 1
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 11 3
A __construct() 0 3 1
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Component Bundle Project
5
 *
6
 * (c) Daniel West <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentBundle\EventListener\Doctrine;
15
16
use DateTime;
17
use DateTimeImmutable;
18
use Doctrine\ORM\EntityManagerInterface;
19
use Silverback\ApiComponentBundle\Entity\Utility\TimestampedInterface;
20
use Symfony\Component\HttpKernel\Event\ViewEvent;
21
22
/**
23
 * @author Daniel West <[email protected]>
24
 */
25
class TimestampedListener
26
{
27
    private EntityManagerInterface $entityManager;
28
29
    public function __construct(EntityManagerInterface $entityManager)
30
    {
31
        $this->entityManager = $entityManager;
32
    }
33
34
    public function __invoke(ViewEvent $event)
35
    {
36
        $entity = $event->getControllerResult();
37
        if (!$entity instanceof TimestampedInterface) {
38
            return;
39
        }
40
41
        if (!$this->entityManager->contains($entity)) {
42
            $entity->setCreated(new DateTimeImmutable());
43
        }
44
        $entity->modified = new DateTime();
0 ignored issues
show
Bug introduced by
Accessing modified on the interface Silverback\ApiComponentB...ty\TimestampedInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
45
    }
46
}
47