CommonBody::withArguments()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * FreeIPA library for PHP
5
 * Copyright (C) 2015-2019 Tobias Sette <[email protected]>
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Lesser General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
declare(strict_types=1);
21
22
namespace Gnumoksha\FreeIpa\Infra\Rpc\Request;
23
24
use stdClass;
25
26
class CommonBody implements Body
27
{
28
    /** @var string */
29
    private $method;
30
    /** @var string|null */
31
    private $methodVersion;
32
    /** @var mixed[] */
33
    private $arguments;
34
    /** @var \stdClass */
35
    private $options;
36
    /** @var string|mixed */
37
    private $id;
38
39
    /**
40
     * @param mixed $id
41
     */
42
    public function __construct(
43
        string $method = '',
44
        array $arguments = [],
45
        stdClass $options = null,
46
        ?string $methodVersion = null,
47
        $id = null
48
    ) {
49
        $this->method        = $method;
50
        $this->arguments     = $arguments;
51
        $this->options       = $options ?? new stdClass();
52
        $this->methodVersion = $methodVersion;
53
        $this->id            = $id ?? uniqid(sprintf('%s.', 'php-FreeIPA'), true);
54
    }
55
56
    /**
57
     * {@inheritDoc}
58
     */
59
    public function getMethod(): string
60
    {
61
        return $this->method;
62
    }
63
64
    /**
65
     * {@inheritDoc}
66
     */
67
    public function withMethod(string $method, string $version = null): Body
68
    {
69
        $new         = clone $this;
70
        $new->method = $method;
71
72
        return $new;
73
    }
74
75
    /**
76
     * {@inheritDoc}
77
     */
78
    public function getArguments(): array
79
    {
80
        return $this->arguments;
81
    }
82
83
    /**
84
     * {@inheritDoc}
85
     */
86
    public function withArgument($argument): Body
87
    {
88
        $new              = clone $this;
89
        $new->arguments[] = $argument;
90
91
        return $new;
92
    }
93
94
    /**
95
     * {@inheritDoc}
96
     * #TODO assert array is not associative
97
     */
98
    public function withArguments(array $arguments): Body
99
    {
100
        $new            = clone $this;
101
        $new->arguments = $arguments;
102
103
        return $new;
104
    }
105
106
    /**
107
     * {@inheritDoc}
108
     */
109
    public function getOptions(): stdClass
110
    {
111
        return $this->options;
112
    }
113
114
    /**
115
     * {@inheritDoc}
116
     */
117
    public function withOption($name, $value): Body
118
    {
119
        $new                   = clone $this;
120
        $new->options->{$name} = $value;
121
122
        return $new;
123
    }
124
125
    /**
126
     * {@inheritDoc}
127
     */
128
    public function withOptions(array $options): Body
129
    {
130
        $new          = clone $this;
131
        $new->options = (object)$options;
132
133
        return $new;
134
    }
135
136
    /**
137
     * {@inheritDoc}
138
     */
139
    public function withAddedOptions(array $options): Body
140
    {
141
        $new          = clone $this;
142
        $new->options = (object)array_merge($options, (array)$new->options);
143
144
        return $new;
145
    }
146
147
    /**
148
     * @return string|mixed
149
     */
150
    public function getId()
151
    {
152
        return $this->id;
153
    }
154
155
    /**
156
     * {@inheritDoc}
157
     */
158
    public function jsonSerialize(): array
159
    {
160
        return [
161
            'method' => $this->method . ($this->methodVersion !== null ? sprintf('/%s', $this->methodVersion) : ''),
162
            'params' => [$this->arguments, $this->options],
163
            'id'     => $this->id,
164
        ];
165
    }
166
}
167