Test Failed
Pull Request — master (#1)
by
unknown
02:09
created

Validation::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Maba\Bundle\RestBundle\Annotation;
5
6
use Maba\Bundle\RestBundle\Entity\RestRequestOptions;
7
use Maba\Bundle\RestBundle\Entity\ValidationOptions;
8
use Maba\Bundle\RestBundle\Service\Annotation\ReflectionMethodWrapper;
9
use Symfony\Component\Validator\Constraint;
10
11
/**
12
 * @Annotation
13
 * @Target({"CLASS", "METHOD", "ANNOTATION"})
14
 */
15
class Validation implements RestAnnotationInterface
16
{
17
    /**
18
     * @var array
19
     */
20
    private $groups;
21
22
    /**
23
     * @var array
24
     */
25
    private $violationPathMap;
26
27
    /**
28
     * @var bool
29
     */
30
    private $enabled;
31
32
    public function __construct(array $options)
33
    {
34
        $this->setGroups($options['groups'] ?? [Constraint::DEFAULT_GROUP]);
35
        $this->setViolationPathMap($options['violationPathMap'] ?? []);
36
        $this->setEnabled($options['enabled'] ?? true);
37
    }
38
39
    private function setGroups(array $groups): self
40
    {
41
        $this->groups = $groups;
42
        return $this;
43
    }
44
45
    private function setViolationPathMap(array $violationPathMap): self
46
    {
47
        $this->violationPathMap = $violationPathMap;
48
        return $this;
49
    }
50
51
    private function setEnabled(bool $enabled): self
52
    {
53
        $this->enabled = $enabled;
54
        return $this;
55
    }
56
57
    public function isSeveralSupported(): bool
58
    {
59
        return true;
60
    }
61
62
    public function apply(RestRequestOptions $options, ReflectionMethodWrapper $reflectionMethod)
63
    {
64
        if (!$this->enabled) {
65
            $options->disableBodyValidation();
66
            return;
67
        }
68
69
        $options->setBodyValidationOptions(
70
            (new ValidationOptions())
71
                ->setValidationGroups($this->groups)
72
                ->setViolationPathMap($this->violationPathMap)
73
        );
74
    }
75
}
76