Passed
Push — master ( def3b9...9746d2 )
by Daniel
06:04
created

TimestampedLoader::loadClassMetadata()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 30
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 15
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 30
ccs 16
cts 16
cp 1
crap 2
rs 9.7666
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\Validator\MappingLoader;
15
16
use Silverback\ApiComponentsBundle\AnnotationReader\TimestampedAnnotationReader;
17
use Symfony\Component\Validator\Constraints as Assert;
18
use Symfony\Component\Validator\Mapping\ClassMetadata;
19
use Symfony\Component\Validator\Mapping\Loader\LoaderInterface;
20
21
/**
22
 * @author Vincent Chalamon <[email protected]>
23
 */
24
final class TimestampedLoader implements LoaderInterface
25
{
26
    private TimestampedAnnotationReader $annotationReader;
27
28 7
    public function __construct(TimestampedAnnotationReader $annotationReader)
29
    {
30 7
        $this->annotationReader = $annotationReader;
31 7
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 2
    public function loadClassMetadata(ClassMetadata $metadata): bool
37
    {
38 2
        if (!$this->annotationReader->isConfigured($metadata->getClassName())) {
39 2
            return false;
40
        }
41
42 2
        $configuration = $this->annotationReader->getConfiguration($metadata->getClassName());
43 2
        $shortName = (new \ReflectionClass($metadata->getClassName()))->getShortName();
44
45 2
        $metadata->addPropertyConstraint(
46 2
            $configuration->createdAtField,
47 2
            new Assert\NotNull(
48
                [
49 2
                    'groups' => [sprintf('%s:timestamped', $shortName)],
50 2
                    'message' => sprintf('%s should not be null', $configuration->createdAtField),
51
                ]
52
            )
53
        );
54
55 2
        $metadata->addPropertyConstraint(
56 2
            $configuration->modifiedAtField,
57 2
            new Assert\NotNull(
58
                [
59 2
                    'groups' => [sprintf('%s:timestamped', $shortName)],
60 2
                    'message' => sprintf('%s should not be null', $configuration->modifiedAtField),
61
                ]
62
            )
63
        );
64
65 2
        return true;
66
    }
67
}
68