Failed Conditions
Push — master ( 48b44f...392b56 )
by Vladimir
04:42
created

OperationParams::isReadOnly()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GraphQL\Server;
6
7
use const CASE_LOWER;
8
use function array_change_key_case;
9
use function is_string;
10
use function json_decode;
11
use function json_last_error;
12
13
/**
14
 * Structure representing parsed HTTP parameters for GraphQL operation
15
 */
16
class OperationParams
17
{
18
    /**
19
     * Id of the query (when using persistent queries).
20
     *
21
     * Valid aliases (case-insensitive):
22
     * - id
23
     * - queryId
24
     * - documentId
25
     *
26
     * @api
27
     * @var string
28
     */
29
    public $queryId;
30
31
    /**
32
     * @api
33
     * @var string
34
     */
35
    public $query;
36
37
    /**
38
     * @api
39
     * @var string
40
     */
41
    public $operation;
42
43
    /**
44
     * @api
45
     * @var mixed[]|null
46
     */
47
    public $variables;
48
49
    /** @var mixed[] */
50
    private $originalInput;
51
52
    /** @var bool */
53
    private $readOnly;
54
55
    /**
56
     * Creates an instance from given array
57
     *
58
     * @api
59
     * @param mixed[] $params
60
     * @param bool    $readonly
61
     * @return OperationParams
62
     */
63 41
    public static function create(array $params, $readonly = false)
64
    {
65 41
        $instance = new static();
66
67 41
        $params                  = array_change_key_case($params, CASE_LOWER);
68 41
        $instance->originalInput = $params;
69
70
        $params += [
71 41
            'query' => null,
72
            'queryid' => null,
73
            'documentid' => null, // alias to queryid
74
            'id' => null, // alias to queryid
75
            'operationname' => null,
76
            'variables' => null,
77
        ];
78
79 41
        if ($params['variables'] === '') {
80
            $params['variables'] = null;
81
        }
82
83 41
        if (is_string($params['variables'])) {
84 2
            $tmp = json_decode($params['variables'], true);
85 2
            if (! json_last_error()) {
86 1
                $params['variables'] = $tmp;
87
            }
88
        }
89
90 41
        $instance->query     = $params['query'];
91 41
        $instance->queryId   = $params['queryid'] ?: $params['documentid'] ?: $params['id'];
92 41
        $instance->operation = $params['operationname'];
93 41
        $instance->variables = $params['variables'];
94 41
        $instance->readOnly  = (bool) $readonly;
95
96 41
        return $instance;
97
    }
98
99
    /**
100
     * @api
101
     * @param string $key
102
     * @return mixed
103
     */
104 1
    public function getOriginalInput($key)
105
    {
106 1
        return $this->originalInput[$key] ?? null;
107
    }
108
109
    /**
110
     * Indicates that operation is executed in read-only context
111
     * (e.g. via HTTP GET request)
112
     *
113
     * @api
114
     * @return bool
115
     */
116 8
    public function isReadOnly()
117
    {
118 8
        return $this->readOnly;
119
    }
120
}
121