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

TimestampedValidator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
dl 0
loc 25
ccs 0
cts 10
cp 0
rs 10
c 1
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A validate() 0 11 3
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