Passed
Pull Request — feature/publishable (#43)
by Daniel
05:26
created

PublishableValidator::validate()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 6
c 3
b 0
f 0
dl 0
loc 11
ccs 0
cts 7
cp 0
rs 9.6111
cc 5
nc 3
nop 2
crap 30
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Component 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\ApiComponentBundle\Validator;
15
16
use ApiPlatform\Core\Validator\ValidatorInterface;
17
use Silverback\ApiComponentBundle\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
    private ValidatorInterface $decorated;
27
    private PublishableHelper $publishableHelper;
28
29
    public function __construct(ValidatorInterface $decorated, PublishableHelper $publishableHelper)
30
    {
31
        $this->decorated = $decorated;
32
        $this->publishableHelper = $publishableHelper;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function validate($data, array $context = [])
39
    {
40
        if (\is_object($data) && $this->publishableHelper->isPublishable($data) && $this->publishableHelper->hasPublicationDate($data)) {
41
            $groups = [(new \ReflectionClass(\get_class($data)))->getShortName() . ':published'];
42
            if (!empty($this->publishableHelper->getConfiguration($data)->validationGroups)) {
43
                $groups = $this->publishableHelper->getConfiguration($data)->validationGroups;
44
            }
45
            $context['groups'] = array_merge($context['groups'] ?? ['Default'], $groups);
46
        }
47
48
        $this->decorated->validate($data, $context);
49
    }
50
}
51