Passed
Push — master ( 3e24bf...d48eaa )
by Daniel
05:16
created

TimestampedLoader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
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\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 1
    public function __construct(TimestampedAnnotationReader $annotationReader)
29
    {
30 1
        $this->annotationReader = $annotationReader;
31 1
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function loadClassMetadata(ClassMetadata $metadata): bool
37
    {
38
        if (!$this->annotationReader->isConfigured($metadata->getClassName())) {
39
            return false;
40
        }
41
42
        $configuration = $this->annotationReader->getConfiguration($metadata->getClassName());
43
        $shortName = (new \ReflectionClass($metadata->getClassName()))->getShortName();
44
45
        $metadata->addPropertyConstraint(
46
            $configuration->createdAtField,
47
            new Assert\NotNull(
48
                [
49
                    'groups' => [sprintf('%s:timestamped', $shortName)],
50
                    'message' => sprintf('%s should not be null', $configuration->createdAtField),
51
                ]
52
            )
53
        );
54
55
        $metadata->addPropertyConstraint(
56
            $configuration->modifiedAtField,
57
            new Assert\NotNull(
58
                [
59
                    'groups' => [sprintf('%s:timestamped', $shortName)],
60
                    'message' => sprintf('%s should not be null', $configuration->modifiedAtField),
61
                ]
62
            )
63
        );
64
65
        return true;
66
    }
67
}
68