Completed
Pull Request — release/1.0.0 (#4)
by Yo
01:26
created

JsonRpcParamsValidator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 28
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

2 Methods

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