Passed
Pull Request — master (#61)
by Daniel
06:02
created

TimestampedListener::preUpdate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components 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\ApiComponentsBundle\EventListener\Doctrine;
15
16
use Doctrine\ORM\Mapping\ClassMetadataInfo;
17
use Doctrine\Persistence\Event\LoadClassMetadataEventArgs;
18
use Silverback\ApiComponentsBundle\AnnotationReader\TimestampedAnnotationReader;
19
20
/**
21
 * @author Daniel West <[email protected]>
22
 */
23
class TimestampedListener
24
{
25
    private TimestampedAnnotationReader $annotationReader;
26
27 11
    public function __construct(TimestampedAnnotationReader $annotationReader)
28
    {
29 11
        $this->annotationReader = $annotationReader;
30 11
    }
31
32 11
    public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs): void
33
    {
34
        /** @var ClassMetadataInfo $metadata */
35 11
        $metadata = $eventArgs->getClassMetadata();
36 11
        if (!$this->annotationReader->isConfigured($metadata->getName())) {
37 11
            return;
38
        }
39
40 11
        $configuration = $this->annotationReader->getConfiguration($metadata->getName());
41
42 11
        if (!$metadata->hasField($configuration->createdAtField)) {
43 11
            $metadata->mapField([
44 11
                'fieldName' => $configuration->createdAtField,
45 11
                'type' => 'datetime_immutable',
46
                'nullable' => false,
47
            ]);
48
        }
49
50 11
        if (!$metadata->hasField($configuration->modifiedAtField)) {
51 11
            $metadata->mapField([
52 11
                'fieldName' => $configuration->modifiedAtField,
53 11
                'type' => 'datetime',
54
                'nullable' => false,
55
            ]);
56
        }
57 11
    }
58
}
59