ResponseBodyValidator::validate()   C
last analyzed

Complexity

Conditions 14
Paths 12

Size

Total Lines 46
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 14

Importance

Changes 0
Metric Value
dl 0
loc 46
ccs 26
cts 26
cp 1
rs 5.0744
c 0
b 0
f 0
cc 14
eloc 24
nc 12
nop 1
crap 14

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace ScayTrase\Api\JsonRpc;
4
5
use ScayTrase\Api\JsonRpc\Exception\ResponseParseException;
6
7
/** @internal */
8
final class ResponseBodyValidator
9
{
10
    /**
11
     * @param \stdClass $response
12
     * @throws ResponseParseException
13
     */
14 21
    public function validate(\stdClass $response)
15
    {
16 21
        if (property_exists($response, JsonRpcResponseInterface::ERROR_FIELD) && property_exists($response, JsonRpcResponseInterface::RESULT_FIELD)) {
17 1
            throw ResponseParseException::bothErrorAndResultPresent();
18
        }
19
20 20
        if (!property_exists($response, JsonRpcResponseInterface::ERROR_FIELD) && !property_exists($response, JsonRpcResponseInterface::RESULT_FIELD)) {
21 4
            throw ResponseParseException::noErrorOrResultPresent();
22
        }
23
24 16
        if (!property_exists($response, JsonRpcResponseInterface::ID_FIELD)) {
25 1
            throw ResponseParseException::noIdSpecified();
26
        }
27
28 15
        if (!property_exists($response, JsonRpcResponseInterface::VERSION_FIELD)) {
29 1
            throw ResponseParseException::noVersionSpecified();
30
        }
31
32 14
        if ($response->jsonrpc !== JsonRpcClient::VERSION) {
33 1
            throw ResponseParseException::inconsistentVersionReceived();
34
        }
35
36 13
        if (property_exists($response, JsonRpcResponseInterface::ERROR_FIELD)) {
37 9
            if (!is_object($response->{JsonRpcResponseInterface::ERROR_FIELD})) {
38 1
                throw ResponseParseException::errorIsNotAnObject();
39
            }
40
41 8
            if (!property_exists($response->{JsonRpcResponseInterface::ERROR_FIELD}, JsonRpcErrorInterface::ERROR_CODE_FIELD)) {
42 1
                throw ResponseParseException::noErrorCodePresent();
43
            }
44
45 7
            $error = $response->{JsonRpcResponseInterface::ERROR_FIELD}->{JsonRpcErrorInterface::ERROR_CODE_FIELD};
46 7
            if (!is_int($error)) {
47 1
                throw ResponseParseException::unexpectedType('error.code', 'integer', gettype($error));
48
            }
49
50 6
            if (!property_exists($response->{JsonRpcResponseInterface::ERROR_FIELD}, JsonRpcErrorInterface::ERROR_MESSAGE_FIELD)) {
51 1
                throw ResponseParseException::noErrorMessagePresent();
52
            }
53
54 5
            $message = $response->{JsonRpcResponseInterface::ERROR_FIELD}->{JsonRpcErrorInterface::ERROR_MESSAGE_FIELD};
55 5
            if (!is_string($message)) {
56 1
                throw ResponseParseException::unexpectedType('error.message', 'string', gettype($message));
57
            }
58 4
        }
59 8
    }
60
}
61