Validation::setViolationPathMap()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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 81
    public function __construct(array $options)
33
    {
34 81
        $this->setGroups($options['groups'] ?? [Constraint::DEFAULT_GROUP]);
35 81
        $this->setViolationPathMap($options['violationPathMap'] ?? []);
36 81
        $this->setEnabled($options['enabled'] ?? true);
37 81
    }
38
39 81
    private function setGroups(array $groups): self
40
    {
41 81
        $this->groups = $groups;
42 81
        return $this;
43
    }
44
45 81
    private function setViolationPathMap(array $violationPathMap): self
46
    {
47 81
        $this->violationPathMap = $violationPathMap;
48 81
        return $this;
49
    }
50
51 81
    private function setEnabled(bool $enabled): self
52
    {
53 81
        $this->enabled = $enabled;
54 81
        return $this;
55
    }
56
57 81
    public function isSeveralSupported(): bool
58
    {
59 81
        return true;
60
    }
61
62 81
    public function apply(RestRequestOptions $options, ReflectionMethodWrapper $reflectionMethod)
63
    {
64 81
        if (!$this->enabled) {
65 81
            $options->disableBodyValidation();
66 81
            return;
67
        }
68
69 81
        $options->setBodyValidationOptions(
70 81
            (new ValidationOptions())
71 81
                ->setValidationGroups($this->groups)
72 81
                ->setViolationPathMap($this->violationPathMap)
73
        );
74 81
    }
75
}
76