Passed
Push — master ( 43a943...86b561 )
by Daniel
12:01
created

TimestampedValidator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
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;
15
16
use ApiPlatform\Core\Validator\ValidatorInterface;
17
use Silverback\ApiComponentsBundle\AnnotationReader\TimestampedAnnotationReader;
18
19
/**
20
 * Builds and add validation group for timestamped resources.
21
 *
22
 * @author Daniel West <[email protected]>
23
 */
24
final class TimestampedValidator implements ValidatorInterface
25
{
26
    private ValidatorInterface $decorated;
27
    private TimestampedAnnotationReader $annotationReader;
28
29
    public function __construct(ValidatorInterface $decorated, TimestampedAnnotationReader $annotationReader)
30
    {
31
        $this->decorated = $decorated;
32
        $this->annotationReader = $annotationReader;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function validate($data, array $context = []): void
39
    {
40
        if (
41
            \is_object($data) &&
42
            $this->annotationReader->isConfigured($data)
43
        ) {
44
            $context['groups'] = $context['groups'] ?? ['Default'];
45
            $context['groups'][] = (new \ReflectionClass(\get_class($data)))->getShortName() . ':timestamped';
46
        }
47
48
        $this->decorated->validate($data, $context);
49
    }
50
}
51