JsonRpcCallDenormalizer   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 79
ccs 24
cts 24
cp 1
rs 10
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A populateItem() 0 17 5
A __construct() 0 3 1
A guessBatchOrNot() 0 16 3
A denormalize() 0 9 1
1
<?php
2
namespace Yoanm\JsonRpcServer\App\Serialization;
3
4
use Yoanm\JsonRpcServer\Domain\Model\JsonRpcCall;
5
6
/**
7
 * Class JsonRpcCallDenormalizer
8
 */
9
class JsonRpcCallDenormalizer
10
{
11
    /** @var JsonRpcRequestDenormalizer */
12
    private $requestDenormalizer;
13
14
    /**
15
     * @param JsonRpcRequestDenormalizer $requestDenormalizer
16
     */
17 52
    public function __construct(JsonRpcRequestDenormalizer $requestDenormalizer)
18
    {
19 52
        $this->requestDenormalizer = $requestDenormalizer;
20
    }
21
22
    /**
23
     * @param array $decodedContent
24
     *
25
     * @return JsonRpcCall
26
     *
27
     * @throws \Exception
28
     */
29 41
    public function denormalize(array $decodedContent) : JsonRpcCall
30
    {
31 41
        $jsonRpcCall = new JsonRpcCall(
32 41
            $this->guessBatchOrNot($decodedContent)
33 41
        );
34
35 41
        $this->populateItem($jsonRpcCall, $decodedContent);
36
37 32
        return $jsonRpcCall;
38
    }
39
40
    /**
41
     * @param array $decodedContent
42
     *
43
     * @return bool
44
     */
45 41
    private function guessBatchOrNot(array $decodedContent) : bool
46
    {
47 41
        $isBatch = (0 !== count($decodedContent));
48
        // Loop over each items
49
        // -> In case it's a valid batch request -> all keys will have numeric type -> iterations = Number of requests
50
        // -> In case it's a valid normal request -> all keys will have string type -> iterations = 1 (stopped on #1)
51
        // => Better performance for normal request (Probably most of requests)
52 41
        foreach ($decodedContent as $key => $item) {
53
            // At least a key is a string (that do not contain an int)
54 41
            if (!is_int($key)) {
55 31
                $isBatch = false;
56 31
                break;
57
            }
58
        }
59
60 41
        return $isBatch;
61
    }
62
63
    /**
64
     * @param JsonRpcCall $jsonRpcCall
65
     * @param array       $decodedContent
66
     *
67
     * @return void
68
     *
69
     * @throws \Exception
70
     */
71 41
    private function populateItem(JsonRpcCall $jsonRpcCall, array $decodedContent) : void
72
    {
73
        // convert to array in any cases for simpler use
74 41
        $itemList = $jsonRpcCall->isBatch() ? $decodedContent : [$decodedContent];
75 41
        foreach ($itemList as $itemPosition => $item) {
76
            // Safely denormalize items
77
            try {
78 41
                $item = $this->requestDenormalizer->denormalize($item);
79
80 28
                $jsonRpcCall->addRequestItem($item);
81 15
            } catch (\Exception $exception) {
82 15
                if (false === $jsonRpcCall->isBatch()) {
83
                    // If it's not a batch call, throw the exception
84 9
                    throw $exception;
85
                }
86
                // Else populate the item (exception will be managed later
87 6
                $jsonRpcCall->addExceptionItem($exception);
88
            }
89
        }
90
    }
91
}
92