Passed
Push — feature/uploadable ( 7c6d25...a7ed20 )
by Daniel
11:07
created

PublishableValidator   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 31
ccs 0
cts 13
cp 0
rs 10
c 0
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A validate() 0 15 6
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\Publishable\PublishableHelper;
18
19
/**
20
 * Builds and add validation group for published resources.
21
 *
22
 * @author Vincent Chalamon <[email protected]>
23
 */
24
final class PublishableValidator implements ValidatorInterface
25
{
26
    public const PUBLISHED_KEY = 'published';
27
28
    private ValidatorInterface $decorated;
29
    private PublishableHelper $publishableHelper;
30
31
    public function __construct(ValidatorInterface $decorated, PublishableHelper $publishableHelper)
32
    {
33
        $this->decorated = $decorated;
34
        $this->publishableHelper = $publishableHelper;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function validate($data, array $context = []): void
41
    {
42
        if (
43
            \is_object($data) &&
44
            $this->publishableHelper->getAnnotationReader()->isConfigured($data) &&
45
            ($this->publishableHelper->hasPublicationDate($data) || isset($context[self::PUBLISHED_KEY]))
46
        ) {
47
            $groups = [(new \ReflectionClass(\get_class($data)))->getShortName() . ':published'];
48
            if (!empty($this->publishableHelper->getAnnotationReader()->getConfiguration($data)->validationGroups)) {
49
                $groups = $this->publishableHelper->getAnnotationReader()->getConfiguration($data)->validationGroups;
50
            }
51
            $context['groups'] = array_merge($context['groups'] ?? ['Default'], $groups);
52
        }
53
54
        $this->decorated->validate($data, $context);
55
    }
56
}
57