Passed
Branch master (babc80)
by Yo
01:25
created

JsonRpcParamsValidator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
eloc 15
dl 0
loc 34
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 21 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\MethodWithValidatedParamsInterface;
7
use Yoanm\JsonRpcServer\Domain\JsonRpcMethodInterface;
8
use Yoanm\JsonRpcServer\Domain\JsonRpcMethodParamsValidatorInterface;
9
use Yoanm\JsonRpcServer\Domain\Model\JsonRpcRequest;
10
11
/**
12
 * Class JsonRpcParamsValidator
13
 */
14
class JsonRpcParamsValidator implements JsonRpcMethodParamsValidatorInterface
15
{
16
    /** @var ValidatorInterface */
17
    private $validator;
18
19
    /**
20
     * @param ValidatorInterface $validator
21
     */
22 3
    public function __construct(ValidatorInterface $validator)
23
    {
24 3
        $this->validator = $validator;
25 3
    }
26
27 3
    public function validate(JsonRpcRequest $jsonRpcRequest, JsonRpcMethodInterface $method) : array
28
    {
29 3
        $violationList = [];
30 3
        if (!$method instanceof MethodWithValidatedParamsInterface) {
31 1
            return $violationList;
32
        }
33 2
        $sfViolationList = $this->validator->validate(
34 2
            $jsonRpcRequest->getParamList(),
35 2
            $method->getParamsConstraint()
36
        );
37
38 2
        foreach ($sfViolationList as $violation) {
39
            /** @var ConstraintViolationInterface $violation */
40 1
            $violationList[] = [
41 1
                'path' => $violation->getPropertyPath(),
42 1
                'message' => $violation->getMessage(),
43 1
                'code' => $violation->getCode(),
44
            ];
45
        }
46
47 2
        return $violationList;
48
    }
49
}
50