Server   A
last analyzed

Complexity

Total Complexity 34

Size/Duplication

Total Lines 457
Duplicated Lines 18.38 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 34
lcom 1
cbo 3
dl 84
loc 457
rs 9.2
c 0
b 0
f 0

29 Methods

Rating   Name   Duplication   Size   Complexity  
A list() 0 16 2
A getNeighborsById() 0 10 1
A getAllNeighbors() 0 10 1
A getUpgrades() 10 10 1
A getById() 8 8 1
B create() 0 43 4
A delete() 0 4 1
A getAvailableKernels() 12 12 1
A getSnapshots() 14 14 1
A getBackups() 12 12 1
A getActions() 12 12 1
A getActionById() 8 8 1
A reboot() 0 4 1
A powerCycle() 0 4 1
A shutdown() 0 4 1
A powerOff() 0 4 1
A powerOn() 0 4 1
A passwordReset() 0 4 1
A resize() 0 4 2
A restore() 0 4 1
A rebuild() 0 4 1
A rename() 0 4 1
A changeKernel() 0 4 1
A enableIpv6() 0 4 1
A enableBackups() 0 4 1
A disableBackups() 0 4 1
A enablePrivateNetworking() 0 4 1
A snapshot() 0 4 1
A executeAction() 8 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/*
3
 *   This file is part of the Vultr PHP library.
4
 *
5
 *   (c) Albert Leitato <[email protected]>
6
 *
7
 *   For the full copyright and license information, please view the LICENSE
8
 *   file that was distributed with this source code.
9
 */
10
namespace Vultr\Api;
11
12
use Vultr\Entity\Action as ActionEntity;
13
use Vultr\Entity\Droplet as DropletEntity;
14
use Vultr\Entity\Image as ImageEntity;
15
use Vultr\Entity\Kernel as KernelEntity;
16
use Vultr\Entity\Upgrade as UpgradeEntity;
17
use Vultr\Exception\HttpException;
18
19
/**
20
 * @author Yassir Hannoun <[email protected]>
21
 * @author Graham Campbell <[email protected]>
22
 */
23
class Server extends AbstractApi
24
{
25
    /**
26
     * @param int         $per_page
27
     * @param int         $page
28
     * @param string|null $tag
29
     *
30
     * @return DropletEntity[]
31
     */
32
    public function list($per_page = 200, $page = 1, $tag = null)
33
    {
34
        $url = \sprintf('%s/droplets?per_page=%d&page=%d', $this->endpoint, $per_page, $page);
35
36
        if (null !== $tag) {
37
            $url .= '&tag_name=' . $tag;
38
        }
39
40
        $droplets = \json_decode($this->adapter->get($url));
41
42
        $this->extractMeta($droplets);
0 ignored issues
show
Bug introduced by
The method extractMeta() does not seem to exist on object<Vultr\Api\Server>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
43
44
        return \array_map(function ($droplet) {
45
            return new DropletEntity($droplet);
46
        }, $droplets->droplets);
47
    }
48
49
    /**
50
     * @param int $id
51
     *
52
     * @return DropletEntity[]
53
     */
54
    public function getNeighborsById($id)
55
    {
56
        $droplets = $this->adapter->get(\sprintf('%s/droplets/%d/neighbors', $this->endpoint, $id));
57
58
        $droplets = \json_decode($droplets);
59
60
        return \array_map(function ($droplet) {
61
            return new DropletEntity($droplet);
62
        }, $droplets->droplets);
63
    }
64
65
    /**
66
     * @return DropletEntity[]
67
     */
68
    public function getAllNeighbors()
69
    {
70
        $neighbors = $this->adapter->get(\sprintf('%s/reports/droplet_neighbors', $this->endpoint));
71
72
        $neighbors = \json_decode($neighbors);
73
74
        return \array_map(function ($neighbor) {
75
            return new DropletEntity($neighbor);
76
        }, $neighbors->neighbors);
77
    }
78
79
    /**
80
     * @return UpgradeEntity[]
81
     */
82 View Code Duplication
    public function getUpgrades()
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...
83
    {
84
        $upgrades = $this->adapter->get(\sprintf('%s/droplet_upgrades', $this->endpoint));
85
86
        $upgrades = \json_decode($upgrades);
87
88
        return \array_map(function ($upgrade) {
89
            return new UpgradeEntity($upgrade);
90
        }, $upgrades);
91
    }
92
93
    /**
94
     * @param int $id
95
     *
96
     * @throws HttpException
97
     *
98
     * @return DropletEntity
99
     */
100 View Code Duplication
    public function getById($id)
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...
101
    {
102
        $droplet = $this->adapter->get(\sprintf('%s/droplets/%d', $this->endpoint, $id));
103
104
        $droplet = \json_decode($droplet);
105
106
        return new DropletEntity($droplet->droplet);
107
    }
108
109
    /**
110
     * @param int    $dcId
111
     * @param string $vpsPlanId
112
     * @param string $osId
113
     * @param array  $options
114
     *
115
     * @throws HttpException
116
     *
117
     * @return DropletEntity|null
118
     */
119
    public function create($dcId, $vpsPlanId, $osId, array $options)
120
    {
121
        $content = [
0 ignored issues
show
Unused Code introduced by
$content is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
122
           'DCID'        => $dcId,
123
           'vps_plan_id' => $vpsPlanId,
124
           'OSID'        => $osId,
125
       ];
126
        $optional = [
127
            'ipxe_chain_url',
128
            'ISOID',
129
            'SCRIPTID' ,
130
            'SNAPSHOTID' ,
131
            'enable_ipv6',
132
            'enable_private_network',
133
            'label',
134
            'SSHKEYID',
135
            'auto_backups',
136
            'APPID',
137
            'userdata',
138
            'notify_activate',
139
            'ddos_protection',
140
            'reserved_ip_v4',
141
            'hostname',
142
            'tag',
143
            'FIREWALLGROUPID',
144
        ];
145
        foreach ($optional as $key => $option) {
146
            if (\array_key_exists($option, $options)) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
147
            }
148
        }
149
150
        $droplet = $this->adapter->post(\sprintf('%s/droplets', $this->endpoint), $data);
0 ignored issues
show
Bug introduced by
The variable $data does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
151
152
        $droplet = \json_decode($droplet);
153
154
        if (\is_array($names)) {
0 ignored issues
show
Bug introduced by
The variable $names does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
155
            return \array_map(function ($droplet) {
156
                return new DropletEntity($droplet);
157
            }, $droplet->droplets);
158
        }
159
160
        return new DropletEntity($droplet->droplet);
161
    }
162
163
    /**
164
     * @param int $id
165
     *
166
     * @throws HttpException
167
     */
168
    public function delete($id)
169
    {
170
        $this->adapter->delete(\sprintf('%s/droplets/%d', $this->endpoint, $id));
171
    }
172
173
    /**
174
     * @param int $id
175
     *
176
     * @throws HttpException
177
     *
178
     * @return KernelEntity[]
179
     */
180 View Code Duplication
    public function getAvailableKernels($id)
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...
181
    {
182
        $kernels = $this->adapter->get(\sprintf('%s/droplets/%d/kernels', $this->endpoint, $id));
183
184
        $kernels = \json_decode($kernels);
185
186
        $this->meta = $this->extractMeta($kernels);
0 ignored issues
show
Bug introduced by
The method extractMeta() does not seem to exist on object<Vultr\Api\Server>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
187
188
        return \array_map(function ($kernel) {
189
            return new KernelEntity($kernel);
190
        }, $kernels->kernels);
191
    }
192
193
    /**
194
     * @param int $id
195
     *
196
     * @return ImageEntity[]
197
     */
198 View Code Duplication
    public function getSnapshots($id)
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...
199
    {
200
        $snapshots = $this->adapter->get(\sprintf('%s/droplets/%d/snapshots?per_page=%d', $this->endpoint, $id, 200));
201
202
        $snapshots = \json_decode($snapshots);
203
204
        $this->meta = $this->extractMeta($snapshots);
0 ignored issues
show
Bug introduced by
The method extractMeta() does not seem to exist on object<Vultr\Api\Server>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
205
206
        return \array_map(function ($snapshot) {
207
            $snapshot = new ImageEntity($snapshot);
208
209
            return $snapshot;
210
        }, $snapshots->snapshots);
211
    }
212
213
    /**
214
     * @param int $id
215
     *
216
     * @return ImageEntity[]
217
     */
218 View Code Duplication
    public function getBackups($id)
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...
219
    {
220
        $backups = $this->adapter->get(\sprintf('%s/droplets/%d/backups?per_page=%d', $this->endpoint, $id, 200));
221
222
        $backups = \json_decode($backups);
223
224
        $this->meta = $this->extractMeta($backups);
0 ignored issues
show
Bug introduced by
The method extractMeta() does not seem to exist on object<Vultr\Api\Server>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
225
226
        return \array_map(function ($backup) {
227
            return new ImageEntity($backup);
228
        }, $backups->backups);
229
    }
230
231
    /**
232
     * @param int $id
233
     *
234
     * @return ActionEntity[]
235
     */
236 View Code Duplication
    public function getActions($id)
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...
237
    {
238
        $actions = $this->adapter->get(\sprintf('%s/droplets/%d/actions?per_page=%d', $this->endpoint, $id, 200));
239
240
        $actions = \json_decode($actions);
241
242
        $this->meta = $this->extractMeta($actions);
0 ignored issues
show
Bug introduced by
The method extractMeta() does not seem to exist on object<Vultr\Api\Server>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
243
244
        return \array_map(function ($action) {
245
            return new ActionEntity($action);
246
        }, $actions->actions);
247
    }
248
249
    /**
250
     * @param int $id
251
     * @param int $actionId
252
     *
253
     * @return ActionEntity
254
     */
255 View Code Duplication
    public function getActionById($id, $actionId)
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...
256
    {
257
        $action = $this->adapter->get(\sprintf('%s/droplets/%d/actions/%d', $this->endpoint, $id, $actionId));
258
259
        $action = \json_decode($action);
260
261
        return new ActionEntity($action->action);
262
    }
263
264
    /**
265
     * @param int $id
266
     *
267
     * @throws HttpException
268
     *
269
     * @return ActionEntity
270
     */
271
    public function reboot($id)
272
    {
273
        return $this->executeAction($id, ['type' => 'reboot']);
274
    }
275
276
    /**
277
     * @param int $id
278
     *
279
     * @throws HttpException
280
     *
281
     * @return ActionEntity
282
     */
283
    public function powerCycle($id)
284
    {
285
        return $this->executeAction($id, ['type' => 'power_cycle']);
286
    }
287
288
    /**
289
     * @param int $id
290
     *
291
     * @throws HttpException
292
     *
293
     * @return ActionEntity
294
     */
295
    public function shutdown($id)
296
    {
297
        return $this->executeAction($id, ['type' => 'shutdown']);
298
    }
299
300
    /**
301
     * @param int $id
302
     *
303
     * @throws HttpException
304
     *
305
     * @return ActionEntity
306
     */
307
    public function powerOff($id)
308
    {
309
        return $this->executeAction($id, ['type' => 'power_off']);
310
    }
311
312
    /**
313
     * @param int $id
314
     *
315
     * @throws HttpException
316
     *
317
     * @return ActionEntity
318
     */
319
    public function powerOn($id)
320
    {
321
        return $this->executeAction($id, ['type' => 'power_on']);
322
    }
323
324
    /**
325
     * @param int $id
326
     *
327
     * @throws HttpException
328
     *
329
     * @return ActionEntity
330
     */
331
    public function passwordReset($id)
332
    {
333
        return $this->executeAction($id, ['type' => 'password_reset']);
334
    }
335
336
    /**
337
     * @param int    $id
338
     * @param string $size
339
     * @param bool   $disk
340
     *
341
     * @throws HttpException
342
     *
343
     * @return ActionEntity
344
     */
345
    public function resize($id, $size, $disk = true)
346
    {
347
        return $this->executeAction($id, ['type' => 'resize', 'size' => $size, 'disk' => $disk ? 'true' : 'false']);
348
    }
349
350
    /**
351
     * @param int $id
352
     * @param int $image
353
     *
354
     * @throws HttpException
355
     *
356
     * @return ActionEntity
357
     */
358
    public function restore($id, $image)
359
    {
360
        return $this->executeAction($id, ['type' => 'restore', 'image' => $image]);
361
    }
362
363
    /**
364
     * @param int        $id
365
     * @param int|string $image
366
     *
367
     * @throws HttpException
368
     *
369
     * @return ActionEntity
370
     */
371
    public function rebuild($id, $image)
372
    {
373
        return $this->executeAction($id, ['type' => 'rebuild', 'image' => $image]);
374
    }
375
376
    /**
377
     * @param int    $id
378
     * @param string $name
379
     *
380
     * @throws HttpException
381
     *
382
     * @return ActionEntity
383
     */
384
    public function rename($id, $name)
385
    {
386
        return $this->executeAction($id, ['type' => 'rename', 'name' => $name]);
387
    }
388
389
    /**
390
     * @param int $id
391
     * @param int $kernel
392
     *
393
     * @throws HttpException
394
     *
395
     * @return ActionEntity
396
     */
397
    public function changeKernel($id, $kernel)
398
    {
399
        return $this->executeAction($id, ['type' => 'change_kernel', 'kernel' => $kernel]);
400
    }
401
402
    /**
403
     * @param int $id
404
     *
405
     * @throws HttpException
406
     *
407
     * @return ActionEntity
408
     */
409
    public function enableIpv6($id)
410
    {
411
        return $this->executeAction($id, ['type' => 'enable_ipv6']);
412
    }
413
414
    /**
415
     * @param int $id
416
     *
417
     * @throws HttpException
418
     *
419
     * @return ActionEntity
420
     */
421
    public function enableBackups($id)
422
    {
423
        return $this->executeAction($id, ['type' => 'enable_backups']);
424
    }
425
426
    /**
427
     * @param int $id
428
     *
429
     * @throws HttpException
430
     *
431
     * @return ActionEntity
432
     */
433
    public function disableBackups($id)
434
    {
435
        return $this->executeAction($id, ['type' => 'disable_backups']);
436
    }
437
438
    /**
439
     * @param int $id
440
     *
441
     * @throws HttpException
442
     *
443
     * @return ActionEntity
444
     */
445
    public function enablePrivateNetworking($id)
446
    {
447
        return $this->executeAction($id, ['type' => 'enable_private_networking']);
448
    }
449
450
    /**
451
     * @param int    $id
452
     * @param string $name
453
     *
454
     * @throws HttpException
455
     *
456
     * @return ActionEntity
457
     */
458
    public function snapshot($id, $name)
459
    {
460
        return $this->executeAction($id, ['type' => 'snapshot', 'name' => $name]);
461
    }
462
463
    /**
464
     * @param int   $id
465
     * @param array $options
466
     *
467
     * @throws HttpException
468
     *
469
     * @return ActionEntity
470
     */
471 View Code Duplication
    private function executeAction($id, array $options)
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...
472
    {
473
        $action = $this->adapter->post(\sprintf('%s/droplets/%d/actions', $this->endpoint, $id), $options);
474
475
        $action = \json_decode($action);
476
477
        return new ActionEntity($action->action);
478
    }
479
}
480