Completed
Push — master ( 8bbcd1...4bee86 )
by Denis
04:24
created

RequestParser::parse()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 36
Code Lines 21

Duplication

Lines 14
Ratio 38.89 %

Importance

Changes 0
Metric Value
dl 14
loc 36
c 0
b 0
f 0
rs 5.3846
cc 8
eloc 21
nc 8
nop 1
1
<?php
2
3
namespace PhpJsonRpc\Server;
4
5
use PhpJsonRpc\Core\Call\CallUnit;
6
use PhpJsonRpc\Core\Call\CallError;
7
use PhpJsonRpc\Core\Call\CallNotification;
8
use PhpJsonRpc\Core\CallSpecifier;
9
use PhpJsonRpc\Error\InvalidRequestException;
10
use PhpJsonRpc\Error\ParseErrorException;
11
12
/**
13
 * Request parser
14
 */
15
class RequestParser
16
{
17
    /**
18
     * Parse request data
19
     *
20
     * @param string $data
21
     * @return CallSpecifier
22
     * @throws ParseErrorException
23
     */
24
    public function parse(string $data): CallSpecifier
25
    {
26
        $payload = @json_decode($data, true);
27
28
        if (!is_array($payload)) {
29
            return new CallSpecifier([new CallError(new ParseErrorException())], true);
30
        }
31
32
        $units = [];
33
34
        // Single request
35
        if ($this->isSingleRequest($payload)) {
36 View Code Duplication
            if ($this->isValidCall($payload)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
37
                $units[] = new CallUnit($payload['id'], $payload['method'], $payload['params'] ?? []);
38
            } elseif ($this->isValidNotification($payload)) {
39
                $units[] = new CallNotification($payload['method'], $payload['params']);
40
            } else {
41
                $units[] = new CallError(new InvalidRequestException());
42
            }
43
44
            return new CallSpecifier($units, true);
45
        }
46
47
        // Batch request
48
        foreach ($payload as $record) {
49 View Code Duplication
            if ($this->isValidCall($record)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
                $units[] = new CallUnit($record['id'], $record['method'], $record['params'] ?? []);
51
            } elseif ($this->isValidNotification($record)) {
52
                $units[] = new CallNotification($record['method'], $record['params'] ?? []);
53
            } else {
54
                $units[] = new CallError(new InvalidRequestException());
55
            }
56
        }
57
58
        return new CallSpecifier($units, false);
59
    }
60
61
    /**
62
     * @param array $payload
63
     * @return bool
64
     */
65
    private function isSingleRequest(array $payload): bool
66
    {
67
        return array_keys($payload) !== range(0, count($payload) - 1);
68
    }
69
70
    /**
71
     * @param array $payload
72
     * @return bool
73
     */
74 View Code Duplication
    private function isValidCall($payload): bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
    {
76
        if (!is_array($payload)) {
77
            return false;
78
        }
79
80
        $headerValid = array_key_exists('jsonrpc', $payload) && $payload['jsonrpc'] === '2.0';
81
        $methodValid = array_key_exists('method', $payload)  && is_string($payload['method']);
82
        $idValid     = array_key_exists('id', $payload);
83
84
        // This member MAY be omitted
85
        $paramsValid = true;
86
        if (array_key_exists('params', $payload) && !is_array($payload['params'])) {
87
            $paramsValid = false;
88
        }
89
90
        return $headerValid && $methodValid && $paramsValid && $idValid;
91
    }
92
93
    /**
94
     * @param array $payload
95
     * @return bool
96
     */
97 View Code Duplication
    private function isValidNotification($payload): bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
98
    {
99
        if (!is_array($payload)) {
100
            return false;
101
        }
102
103
        $headerValid = array_key_exists('jsonrpc', $payload) && $payload['jsonrpc'] === '2.0';
104
        $methodValid = array_key_exists('method', $payload)  && is_string($payload['method']);
105
        $idValid     = !array_key_exists('id', $payload);
106
107
        // This member MAY be omitted
108
        $paramsValid = true;
109
        if (array_key_exists('params', $payload) && !is_array($payload['params'])) {
110
            $paramsValid = false;
111
        }
112
113
        return $headerValid && $methodValid && $paramsValid && $idValid;
114
    }
115
}
116