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

TimestampedListener::setFields()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 2
nop 2
dl 0
loc 16
ccs 12
cts 12
cp 1
crap 3
rs 9.9
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