Passed
Pull Request — feature/publishable (#43)
by Daniel
09:34 queued 03:04
created

PublishableValidator::validate()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 9
ccs 0
cts 6
cp 0
rs 10
cc 4
nc 2
nop 2
crap 20
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 = $context['groups'] ?? ['Default'];
42
            $groups[] = (new \ReflectionClass(get_class($data)))->getShortName().':published';
43
            $context['groups'] = $groups;
44
        }
45
46
        $this->decorated->validate($data, $context);
47
    }
48
}