validateSingleResponse()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 17
ccs 10
cts 10
cp 1
rs 9.9666
cc 4
nc 4
nop 1
crap 4
1
<?php
2
/*
3
 * This file is part of JSON RPC Client.
4
 *
5
 * (c) Igor Lazarev <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Strider2038\JsonRpcClient\Response;
12
13
use Strider2038\JsonRpcClient\Exception\ErrorResponseException;
14
use Strider2038\JsonRpcClient\Exception\ResponseException;
15
16
/**
17
 * @author Igor Lazarev <[email protected]>
18
 */
19
class ExceptionalResponseValidator implements ResponseValidatorInterface
20
{
21 32
    public function validate($response): void
22
    {
23 32
        if (null !== $response) {
24 29
            $this->validateResponse($response);
25
        }
26 21
    }
27
28 29
    private function validateResponse($response): void
29
    {
30 29
        if (is_array($response)) {
31 8
            foreach ($response as $responseInBatch) {
32 8
                $this->validateSingleResponse($responseInBatch);
33
            }
34
        } else {
35 21
            $this->validateSingleResponse($response);
36
        }
37 18
    }
38
39 29
    private function validateSingleResponse($response): void
40
    {
41 29
        if (!$response instanceof ResponseObjectInterface) {
42 1
            throw new ResponseException(
43 1
                sprintf(
44 1
                    'Response from server expected to be an object or an array of objects. Given "%s".',
45 1
                    json_encode($response)
46
                )
47
            );
48
        }
49
50 28
        if ('2.0' !== $response->getProtocol()) {
51 2
            throw new ResponseException('Invalid JSON RPC version in server request.');
52
        }
53
54 26
        if ($response->hasError()) {
55 8
            throw new ErrorResponseException($response->getError());
56
        }
57 18
    }
58
}
59