Passed
Push — master ( 29bb16...b3add9 )
by Biao
03:22
created

Client::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 5
rs 10
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace Hhxsv5\LaravelS\Components\Eureka;
4
5
class Client
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class Client
Loading history...
6
{
7
    protected $host;
8
9
    protected $port;
10
11
    protected $context;
12
13
    public function __construct($host, $port, $context = 'eureka/v2 ')
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
14
    {
15
        $this->host = $host;
16
        $this->port = $port;
17
        $this->context = $context;
18
    }
19
20
    protected function getEurekaUri()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getEurekaUri()
Loading history...
21
    {
22
        return $this->host . ':' . $this->port . '/' . $this->context;
23
    }
24
25
    /**
26
     * Register app in eureka.
27
     *
28
     * @param string $appId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
29
     * @param array $data
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
30
     * @return
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
Coding Style introduced by
Return type missing for @return tag in function comment
Loading history...
31
     */
32
    public function register($appId, array $data)
33
    {
34
        return $this->client->request('POST', $this->getEurekaUri() . '/apps/' . $appId, [
0 ignored issues
show
Bug Best Practice introduced by
The property client does not exist on Hhxsv5\LaravelS\Components\Eureka\Client. Did you maybe forget to declare it?
Loading history...
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
35
            'json' => [
36
                'instance' => $data,
37
            ],
38
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
39
    }
40
41
    /**
42
     * De-register app from eureka.
43
     *
44
     * @param string $appId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
45
     * @param string $instanceId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
46
     *
47
     * @return ResponseInterface
0 ignored issues
show
Bug introduced by
The type Hhxsv5\LaravelS\Componen...ureka\ResponseInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
48
     * @throws GuzzleException
49
     *
50
     */
0 ignored issues
show
Coding Style introduced by
Additional blank lines found at end of doc comment
Loading history...
51
    public function deRegister($appId, $instanceId)
52
    {
53
        return $this->client->request('DELETE', $this->getEurekaUri() . '/apps/' . $appId . '/' . $instanceId);
0 ignored issues
show
Bug Best Practice introduced by
The property client does not exist on Hhxsv5\LaravelS\Components\Eureka\Client. Did you maybe forget to declare it?
Loading history...
54
    }
55
56
    /**
57
     * Send app heartbeat.
58
     *
59
     * @param string $appId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
60
     * @param string $instanceId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
61
     *
62
     * @return ResponseInterface
63
     * @throws GuzzleException
64
     *
65
     */
0 ignored issues
show
Coding Style introduced by
Additional blank lines found at end of doc comment
Loading history...
66
    public function heartBeat($appId, $instanceId)
67
    {
68
        return $this->client->request('PUT', $this->getEurekaUri() . '/apps/' . $appId . '/' . $instanceId);
0 ignored issues
show
Bug Best Practice introduced by
The property client does not exist on Hhxsv5\LaravelS\Components\Eureka\Client. Did you maybe forget to declare it?
Loading history...
69
    }
70
71
    /**
72
     * Get all registered applications.
73
     *
74
     * @return array
75
     * @throws GuzzleException
76
     *
77
     */
0 ignored issues
show
Coding Style introduced by
Additional blank lines found at end of doc comment
Loading history...
78
    public function getAllApps()
79
    {
80
        $response = $this->client->request('GET', $this->getEurekaUri() . '/apps', [
0 ignored issues
show
Bug Best Practice introduced by
The property client does not exist on Hhxsv5\LaravelS\Components\Eureka\Client. Did you maybe forget to declare it?
Loading history...
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
81
            'headers' => [
82
                'Accept' => 'application/json',
83
            ],
84
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
85
86
        return \GuzzleHttp\json_decode($response->getBody(), true);
87
    }
88
89
    /**
90
     * Get application.
91
     *
92
     * @param string $appId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
93
     *
94
     * @return array
95
     * @throws GuzzleException
96
     *
97
     */
0 ignored issues
show
Coding Style introduced by
Additional blank lines found at end of doc comment
Loading history...
98
    public function getApp($appId)
99
    {
100
        $response = $this->client->request('GET', $this->getEurekaUri() . '/apps/' . $appId, [
0 ignored issues
show
Bug Best Practice introduced by
The property client does not exist on Hhxsv5\LaravelS\Components\Eureka\Client. Did you maybe forget to declare it?
Loading history...
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
101
            'headers' => [
102
                'Accept' => 'application/json',
103
            ],
104
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
105
106
        return \GuzzleHttp\json_decode($response->getBody(), true);
107
    }
108
109
    /**
110
     * Get application Instance.
111
     *
112
     * @param string $appId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
113
     * @param string $instanceId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
114
     *
115
     * @return array
116
     * @throws GuzzleException
117
     *
118
     */
0 ignored issues
show
Coding Style introduced by
Additional blank lines found at end of doc comment
Loading history...
119
    public function getAppInstance($appId, $instanceId)
120
    {
121
        $response = $this->client->request('GET', $this->getEurekaUri() . '/apps/' . $appId . '/' . $instanceId, [
0 ignored issues
show
Bug Best Practice introduced by
The property client does not exist on Hhxsv5\LaravelS\Components\Eureka\Client. Did you maybe forget to declare it?
Loading history...
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
122
            'headers' => [
123
                'Accept' => 'application/json',
124
            ],
125
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
126
127
        return \GuzzleHttp\json_decode($response->getBody(), true);
128
    }
129
130
    /**
131
     * Get Instance.
132
     *
133
     * @param string $instanceId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
134
     *
135
     * @return array
136
     * @throws GuzzleException
137
     *
138
     */
0 ignored issues
show
Coding Style introduced by
Additional blank lines found at end of doc comment
Loading history...
139
    public function getInstance($instanceId)
140
    {
141
        $response = $this->client->request('GET', $this->getEurekaUri() . '/instances/' . $instanceId, [
0 ignored issues
show
Bug Best Practice introduced by
The property client does not exist on Hhxsv5\LaravelS\Components\Eureka\Client. Did you maybe forget to declare it?
Loading history...
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
142
            'headers' => [
143
                'Accept' => 'application/json',
144
            ],
145
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
146
147
        return json_decode($response->getBody(), true);
148
    }
149
150
    /**
151
     * Take Instance out of the service.
152
     *
153
     * @param string $appId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
154
     * @param string $instanceId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
155
     *
156
     *
157
     */
0 ignored issues
show
Coding Style introduced by
Additional blank lines found at end of doc comment
Loading history...
Coding Style introduced by
Missing @return tag in function comment
Loading history...
158
    public function takeInstanceOut($appId, $instanceId)
159
    {
160
        return $this->client->request('PUT', $this->getEurekaUri() . '/apps/' . $appId . '/' . $instanceId . '/status', [
0 ignored issues
show
Bug Best Practice introduced by
The property client does not exist on Hhxsv5\LaravelS\Components\Eureka\Client. Did you maybe forget to declare it?
Loading history...
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
161
            'query' => [
162
                'value' => 'OUT_OF_SERVICE',
163
            ],
164
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
165
    }
166
167
    /**
168
     * Put Instance back into the service.
169
     *
170
     * @param string $appId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
171
     * @param string $instanceId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
172
     *
173
     * @return ResponseInterface
174
     * @throws GuzzleException
175
     *
176
     */
0 ignored issues
show
Coding Style introduced by
Additional blank lines found at end of doc comment
Loading history...
177
    public function putInstanceBack($appId, $instanceId)
178
    {
179
        return $this->client->request('PUT', $this->getEurekaUri() . '/apps/' . $appId . '/' . $instanceId . '/status', [
0 ignored issues
show
Bug Best Practice introduced by
The property client does not exist on Hhxsv5\LaravelS\Components\Eureka\Client. Did you maybe forget to declare it?
Loading history...
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
180
            'query' => [
181
                'value' => 'UP',
182
            ],
183
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
184
    }
185
186
    /**
187
     * Update app Instance metadata.
188
     *
189
     * @param string $appId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
190
     * @param string $instanceId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
191
     * @param array $metadata
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
192
     *
193
     * @return ResponseInterface
194
     * @throws GuzzleException
195
     *
196
     */
0 ignored issues
show
Coding Style introduced by
Additional blank lines found at end of doc comment
Loading history...
197
    public function updateAppInstanceMetadata($appId, $instanceId, array $metadata)
198
    {
199
        return $this->client->request('PUT', $this->getEurekaUri() . '/apps/' . $appId . '/' . $instanceId . '/metadata', [
0 ignored issues
show
Bug Best Practice introduced by
The property client does not exist on Hhxsv5\LaravelS\Components\Eureka\Client. Did you maybe forget to declare it?
Loading history...
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
200
            'query' => $metadata,
201
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
202
    }
203
204
    /**
205
     * Get all instances by a vip address.
206
     *
207
     * @param string $vipAddress
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
208
     *
209
     */
0 ignored issues
show
Coding Style introduced by
Additional blank lines found at end of doc comment
Loading history...
Coding Style introduced by
Missing @return tag in function comment
Loading history...
210
    public function getInstancesByVipAddress($vipAddress)
211
    {
212
        $response = $this->client->request('GET', $this->getEurekaUri() . '/vips/' . $vipAddress, [
0 ignored issues
show
Unused Code introduced by
The assignment to $response is dead and can be removed.
Loading history...
Bug Best Practice introduced by
The property client does not exist on Hhxsv5\LaravelS\Components\Eureka\Client. Did you maybe forget to declare it?
Loading history...
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
213
            'headers' => [
214
                'Accept' => 'application/json',
215
            ],
216
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
217
    }
218
219
    /**
220
     * Get all instances by a secure vip address.
221
     *
222
     * @param string $secureVipAddress
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
223
     *
224
     * @return array
225
     */
226
    public function getInstancesBySecureVipAddress($secureVipAddress)
227
    {
228
        $response = $this->client->request('GET', $this->getEurekaUri() . '/svips/' . $secureVipAddress, [
0 ignored issues
show
Bug Best Practice introduced by
The property client does not exist on Hhxsv5\LaravelS\Components\Eureka\Client. Did you maybe forget to declare it?
Loading history...
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
229
            'headers' => [
230
                'Accept' => 'application/json',
231
            ],
232
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
233
234
        return json_decode($response->getBody(), true);
235
    }
236
237
    public function up(string $appId, string $instanceId)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function up()
Loading history...
238
    {
239
        return $this->client->request('PUT', $this->getEurekaUri() . '/apps/' . $appId . '/' . $instanceId . '/status', [
0 ignored issues
show
Bug Best Practice introduced by
The property client does not exist on Hhxsv5\LaravelS\Components\Eureka\Client. Did you maybe forget to declare it?
Loading history...
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
240
            'query' => [
241
                'value' => 'UP',
242
            ],
243
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
244
    }
245
246
    public function down($appId, $instanceId)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function down()
Loading history...
247
    {
248
        return $this->client->request('PUT', $this->getEurekaUri() . '/apps/' . $appId . '/' . $instanceId . '/status', [
0 ignored issues
show
Bug Best Practice introduced by
The property client does not exist on Hhxsv5\LaravelS\Components\Eureka\Client. Did you maybe forget to declare it?
Loading history...
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
249
            'query' => [
250
                'value' => 'DOWN',
251
            ],
252
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
253
    }
254
255
    /**
256
     * Get the alive instances
257
     * @param string $appId
0 ignored issues
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
258
     * @return array
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
259
     * @throws \GuzzleHttp\Exception\GuzzleException
0 ignored issues
show
Coding Style introduced by
Tag @throws cannot be grouped with parameter tags in a doc comment
Loading history...
260
     */
261
    public function getUpInstances($appId)
262
    {
263
        $apps = $this->getApp($appId);
264
        return array_filter($apps['application']['instance'], function ($instance) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
265
            return $instance['status'] === 'UP';
266
        });
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
267
    }
268
269
    /**
270
     * Get the url of instance.
271
     * @param array $instance
0 ignored issues
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
272
     * @return string
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
273
     */
274
    public function getInstanceUrl(array $instance)
275
    {
276
        if ($instance['securePort']['@enabled'] === 'true') {
277
            $url = sprintf('%s://%s:%d', 'https', $instance['ipAddr'], $instance['securePort']['$']);
278
        } elseif ($instance['port']['@enabled'] === 'true') {
279
            $url = sprintf('%s://%s:%d', 'http', $instance['ipAddr'], $instance['port']['$']);
280
        } else {
281
            $parts = parse_url($instance['homePageUrl']);
282
            if (!isset($parts['host'])) {
283
                throw new \RuntimeException('Invalid homePageUrl: ' . $instance['homePageUrl']);
284
            }
285
            $url = sprintf('%s://%s:%d', isset($parts['scheme']) ? $parts['scheme'] : 'http', $parts['host'], isset($parts['port']) ? $parts['port'] : 80);
286
        }
287
        return $url;
288
    }
289
}
290