Failed Conditions
Pull Request — release/1.0.0 (#4)
by Yo
01:59 queued 16s
created

JsonRpcParamsValidator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
crap 2
1
<?php
2
namespace Yoanm\JsonRpcParamsSymfonyValidator\Infra;
3
4
use Symfony\Component\Validator\Validator\ValidatorInterface;
5
use Yoanm\JsonRpcParamsSymfonyValidator\Domain\Model\MethodWithValidatedParams;
6
use Yoanm\JsonRpcServer\Domain\Event\Action\ValidateParamsEvent;
7
8
/**
9
 * Class JsonRpcParamsValidator
10
 */
11
class JsonRpcParamsValidator
12
{
13
    /** @var ValidatorInterface */
14
    private $validator;
15
16
    /**
17
     * @param ValidatorInterface $validator
18
     */
19
    public function __construct(ValidatorInterface $validator)
20
    {
21
        $this->validator = $validator;
22
    }
23
24
    /**
25
     * @param ValidateParamsEvent $event
26
     */
27
    public function validate(ValidateParamsEvent $event)
28
    {
29
        $method = $event->getMethod();
30
        if (!$method instanceof MethodWithValidatedParams) {
31
            return;
32
        }
33
        foreach ($this->validator->validate($event->getParamList(), $method->getParamsConstraint()) as $violation) {
34
            $event->addViolation([
35
                'path' => $violation->getPropertyPath(),
36
                'message' => $violation->getMessage(),
37
                'code' => $violation->getCode(),
38
            ]);
39
        }
40
    }
41
}
42