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

TimestampedLoader   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 15.79%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
dl 0
loc 42
ccs 3
cts 19
cp 0.1579
rs 10
c 1
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A loadClassMetadata() 0 30 2
A __construct() 0 3 1
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