Passed
Pull Request — master (#7)
by Igor
04:31
created

ResponseObjectDenormalizer::denormalizeResult()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 19
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 19
ccs 10
cts 10
cp 1
rs 9.9666
cc 4
nc 3
nop 3
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\Bridge\Symfony\Serialization;
12
13
use Strider2038\JsonRpcClient\Request\RequestObject;
14
use Strider2038\JsonRpcClient\Response\ErrorObject;
15
use Strider2038\JsonRpcClient\Response\ResponseObject;
16
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
17
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
18
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
19
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
20
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
21
22
/**
23
 * @author Igor Lazarev <[email protected]>
24
 */
25
class ResponseObjectDenormalizer implements DenormalizerInterface, DenormalizerAwareInterface, CacheableSupportsMethodInterface
26
{
27
    use DenormalizerAwareTrait;
28
29 4
    public function hasCacheableSupportsMethod(): bool
30
    {
31 4
        return true;
32
    }
33
34 4
    public function supportsDenormalization($data, $type, $format = null): bool
35
    {
36 4
        return ResponseObject::class === $type;
37
    }
38
39 8
    public function denormalize($data, $type, $format = null, array $context = []): ResponseObject
40
    {
41 8
        if (!is_array($data)) {
42 1
            throw new UnexpectedValueException('Denormalization data is expected to be an array');
43
        }
44
45 7
        $response = new ResponseObject(
46 7
            $data['jsonrpc'] ?? '',
47 7
            $this->denormalizeResult($data, $format, $context),
48 7
            $data['id'] ?? null
49
        );
50
51 7
        if (array_key_exists('error', $data)) {
52 3
            $response->setError($this->denormalizeError($data, $format, $context));
53
        }
54
55 7
        return $response;
56
    }
57
58 7
    private function denormalizeResult($data, $format, array $context)
59
    {
60 7
        $result = $data['result'] ?? null;
61
62 7
        $request = $context['json_rpc']['request'] ?? null;
63
64 7
        if ($request instanceof RequestObject) {
65 4
            $method = $request->getMethod();
66
67 4
            $resultTypesByMethods = $context['json_rpc']['result_types_by_methods'] ?? [];
68
69 4
            if (null !== $result && array_key_exists($method, $resultTypesByMethods)) {
70 3
                $resultType = $resultTypesByMethods[$method];
71
72 3
                $result = $this->denormalizer->denormalize($result, $resultType, $format, $context);
73
            }
74
        }
75
76 7
        return $result;
77
    }
78
79 3
    private function denormalizeError($data, $format, array $context): ErrorObject
80
    {
81 3
        return new ErrorObject(
82 3
            $data['error']['code'] ?? null,
0 ignored issues
show
Bug introduced by
It seems like $data['error']['code'] ?? null can also be of type null; however, parameter $code of Strider2038\JsonRpcClien...orObject::__construct() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

82
            /** @scrutinizer ignore-type */ $data['error']['code'] ?? null,
Loading history...
83 3
            $data['error']['message'] ?? null,
0 ignored issues
show
Bug introduced by
It seems like $data['error']['message'] ?? null can also be of type null; however, parameter $message of Strider2038\JsonRpcClien...orObject::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

83
            /** @scrutinizer ignore-type */ $data['error']['message'] ?? null,
Loading history...
84 3
            $this->denormalizeErrorData($data['error']['data'] ?? null, $format, $context)
85
        );
86
    }
87
88 3
    private function denormalizeErrorData($errorData, $format, array $context)
89
    {
90 3
        $errorType = $context['json_rpc']['error_type'] ?? null;
91
92 3
        if (null !== $errorData && null !== $errorType) {
93 2
            $errorData = $this->denormalizer->denormalize($errorData, $errorType, $format, $context);
94
        }
95
96 3
        return $errorData;
97
    }
98
}
99