Route   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 202
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 0

Importance

Changes 0
Metric Value
wmc 22
lcom 3
cbo 0
dl 0
loc 202
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A to() 0 6 1
A toApi() 0 4 1
A toStream() 0 4 1
A toSocket() 0 4 1
A toFaye() 0 4 1
A toWebhook() 0 4 1
A route() 0 8 2
A method() 0 8 2
A with() 0 6 1
A withMany() 0 8 2
A withBody() 0 6 1
A getBody() 0 8 2
A build() 0 21 4
A __callStatic() 0 4 1
1
<?php
2
/**
3
 * This file is part of GitterApi package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Gitter;
11
12
/**
13
 * Class Route
14
 * @package Gitter
15
 *
16
 * @method static Route get(string $route)
17
 * @method static Route put(string $route)
18
 * @method static Route post(string $route)
19
 * @method static Route patch(string $route)
20
 * @method static Route delete(string $route)
21
 * @method static Route options(string $route)
22
 * @method static Route head(string $route)
23
 * @method static Route connect(string $route)
24
 * @method static Route trace(string $route)
25
 */
26
class Route
27
{
28
    /**
29
     * @var string
30
     */
31
    private $route;
32
33
    /**
34
     * @var array
35
     */
36
    private $parameters = [];
37
38
    /**
39
     * @var string
40
     */
41
    private $method;
42
43
    /**
44
     * @var string|null
45
     */
46
    private $url;
47
48
    /**
49
     * @var array
50
     */
51
    private $body = [];
52
53
    /**
54
     * Route constructor.
55
     * @param string $route
56
     * @param string $method
57
     */
58
    public function __construct(string $route, string $method = 'GET')
59
    {
60
        $this->route($route);
61
        $this->method($method);
62
        $this->toApi();
63
    }
64
65
    /**
66
     * @param string $url
67
     * @return $this|Route
68
     */
69
    public function to(string $url): Route
70
    {
71
        $this->url = $url;
72
73
        return $this;
74
    }
75
76
    /**
77
     * @return Route
78
     */
79
    public function toApi(): Route
80
    {
81
        return $this->to('https://api.gitter.im/v1/');
82
    }
83
84
    /**
85
     * @return Route
86
     */
87
    public function toStream(): Route
88
    {
89
        return $this->to('https://stream.gitter.im/v1/');
90
    }
91
92
    /**
93
     * @return Route
94
     */
95
    public function toSocket(): Route
96
    {
97
        return $this->to('wss://ws.gitter.im/');
98
    }
99
100
    /**
101
     * @return Route
102
     */
103
    public function toFaye(): Route
104
    {
105
        return $this->to('https://gitter.im/api/v1/');
106
    }
107
108
    /**
109
     * @return Route
110
     */
111
    public function toWebhook(): Route
112
    {
113
        return $this->to('https://webhooks.gitter.im/e/');
114
    }
115
116
    /**
117
     * @param string|null $route
118
     * @return string
119
     */
120
    public function route(string $route = null): string
121
    {
122
        if ($route !== null) {
123
            $this->route = $route;
124
        }
125
126
        return $this->route;
127
    }
128
129
    /**
130
     * @param string|null $method
131
     * @return string
132
     */
133
    public function method(string $method = null): string
134
    {
135
        if ($method !== null) {
136
            $this->method = \strtoupper($method);
137
        }
138
139
        return $this->method;
140
    }
141
142
    /**
143
     * @param string $parameter
144
     * @param string|int $value
145
     * @return $this|Route
146
     */
147
    public function with(string $parameter, $value): Route
148
    {
149
        $this->parameters[$parameter] = (string)$value;
150
151
        return $this;
152
    }
153
154
    /**
155
     * @param array $parameters
156
     * @return $this|Route
157
     */
158
    public function withMany(array $parameters): Route
159
    {
160
        foreach ($parameters as $parameter => $value) {
161
            $this->with($parameter, $value);
162
        }
163
164
        return $this;
165
    }
166
167
    /**
168
     * @param string $field
169
     * @param $value
170
     * @return Route|$this
171
     */
172
    public function withBody(string $field, $value): Route
173
    {
174
        $this->body[$field] = $value;
175
176
        return $this;
177
    }
178
179
    /**
180
     * @return string|null
181
     */
182
    public function getBody()
183
    {
184
        if (\count($this->body)) {
185
            return \json_encode($this->body);
186
        }
187
188
        return null;
189
    }
190
191
    /**
192
     * @param array $parameters
193
     * @return string
194
     * @throws \InvalidArgumentException
195
     */
196
    public function build(array $parameters = []): string
197
    {
198
        if ($this->url === null) {
199
            throw new \InvalidArgumentException('Can not build route string. URL does not set');
200
        }
201
202
        $route = $this->route;
203
        $query = $parameters = array_merge($this->parameters, $parameters);
204
205
        foreach ($parameters as $parameter => $value) {
206
            $updatedRoute = str_replace(sprintf('{%s}', $parameter), $value, $route);
207
208
            if ($updatedRoute !== $route) {
209
                unset($query[$parameter]);
210
            }
211
212
            $route = $updatedRoute;
213
        }
214
215
        return $this->url . $route . '?' . http_build_query($query);
216
    }
217
218
    /**
219
     * @param string $name
220
     * @param array $arguments
221
     * @return static
222
     */
223
    public static function __callStatic(string $name, array $arguments = [])
224
    {
225
        return new static($arguments[0] ?? '', $name);
226
    }
227
}
228