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

JsonRpcParamsValidator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 27
ccs 0
cts 15
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 11 3
A __construct() 0 3 1
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