Completed
Pull Request — master (#235)
by
unknown
03:27
created

Droplet::create()   C

Complexity

Conditions 13
Paths 96

Size

Total Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 52
rs 6.6166
c 0
b 0
f 0
cc 13
nc 96
nop 14

How to fix   Long Method    Complexity    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
/*
4
 * This file is part of the DigitalOceanV2 library.
5
 *
6
 * (c) Antoine Corcy <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace DigitalOceanV2\Api;
13
14
use DigitalOceanV2\Entity\Action as ActionEntity;
15
use DigitalOceanV2\Entity\Droplet as DropletEntity;
16
use DigitalOceanV2\Entity\Image as ImageEntity;
17
use DigitalOceanV2\Entity\Kernel as KernelEntity;
18
use DigitalOceanV2\Entity\Upgrade as UpgradeEntity;
19
use DigitalOceanV2\Exception\HttpException;
20
21
/**
22
 * @author Yassir Hannoun <[email protected]>
23
 * @author Graham Campbell <[email protected]>
24
 */
25
class Droplet extends AbstractApi
26
{
27
    /**
28
     * @param int $per_page
29
     * @param int $page
30
     * @param string|null $tag
31
     *
32
     * @return DropletEntity[]
33
     */
34
    public function getAll($per_page = 200, $page = 1, $tag = null)
35
    {
36
        $url = sprintf('%s/droplets?per_page=%d&page=%d', $this->endpoint, $per_page, $page);
37
38
        if (null !== $tag) {
39
            $url .= '&tag_name='.$tag;
40
        }
41
42
        $droplets = json_decode($this->adapter->get($url));
43
44
        $this->extractMeta($droplets);
45
46
        return array_map(function ($droplet) {
47
            return new DropletEntity($droplet);
48
        }, $droplets->droplets);
49
    }
50
51
    /**
52
     * @return $total
0 ignored issues
show
Documentation introduced by
The doc-type $total could not be parsed: Unknown type name "$total" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
53
     */
54
    public function getTotal()
55
    {
56
        $url = sprintf('%s/droplets?per_page=1&page=1', $this->endpoint);
57
        $droplets = json_decode($this->adapter->get($url));
58
        $total = $droplets->meta->total;
59
        
60
        return $total;
61
    }
62
63
    /**
64
     * @param int $id
65
     *
66
     * @return DropletEntity[]
67
     */
68
    public function getNeighborsById($id)
69
    {
70
        $droplets = $this->adapter->get(sprintf('%s/droplets/%d/neighbors', $this->endpoint, $id));
71
72
        $droplets = json_decode($droplets);
73
74
        return array_map(function ($droplet) {
75
            return new DropletEntity($droplet);
76
        }, $droplets->droplets);
77
    }
78
79
    /**
80
     * @return DropletEntity[]
81
     */
82
    public function getAllNeighbors()
83
    {
84
        $neighbors = $this->adapter->get(sprintf('%s/reports/droplet_neighbors', $this->endpoint));
85
86
        $neighbors = json_decode($neighbors);
87
88
        return array_map(function ($neighbor) {
89
            return new DropletEntity($neighbor);
90
        }, $neighbors->neighbors);
91
    }
92
93
    /**
94
     * @return UpgradeEntity[]
95
     */
96
    public function getUpgrades()
97
    {
98
        $upgrades = $this->adapter->get(sprintf('%s/droplet_upgrades', $this->endpoint));
99
100
        $upgrades = json_decode($upgrades);
101
102
        return array_map(function ($upgrade) {
103
            return new UpgradeEntity($upgrade);
104
        }, $upgrades);
105
    }
106
107
    /**
108
     * @param int $id
109
     *
110
     * @throws HttpException
111
     *
112
     * @return DropletEntity
113
     */
114
    public function getById($id)
115
    {
116
        $droplet = $this->adapter->get(sprintf('%s/droplets/%d', $this->endpoint, $id));
117
118
        $droplet = json_decode($droplet);
119
120
        return new DropletEntity($droplet->droplet);
121
    }
122
123
    /**
124
     * @param array|string $names
125
     * @param string       $region
126
     * @param string       $size
127
     * @param string|int   $image
128
     * @param bool         $backups
129
     * @param bool         $ipv6
130
     * @param bool         $privateNetworking
131
     * @param int[]        $sshKeys
132
     * @param string       $userData
133
     * @param bool         $monitoring
134
     * @param array        $volumes
135
     * @param array        $tags
136
     * @param bool         $wait
137
     * @param int          $waitTimeout
138
     *
139
     * @throws HttpException
140
     *
141
     * @return DropletEntity|null
142
     */
143
    public function create($names, $region, $size, $image, $backups = false, $ipv6 = false, $privateNetworking = false, array $sshKeys = [], $userData = '', $monitoring = true, array $volumes = [], array $tags = [], $wait = false, $waitTimeout = 300)
144
    {
145
        $data = is_array($names) ? ['names' => $names] : ['name' => $names];
146
147
        $data = array_merge($data, [
148
            'region' => $region,
149
            'size' => $size,
150
            'image' => $image,
151
            'backups' => $backups ? 'true' : 'false',
152
            'ipv6' => $ipv6 ? 'true' : 'false',
153
            'private_networking' => $privateNetworking ? 'true' : 'false',
154
            'monitoring' => $monitoring ? 'true' : 'false',
155
        ]);
156
157
        if (0 < count($sshKeys)) {
158
            $data['ssh_keys'] = $sshKeys;
159
        }
160
161
        if (!empty($userData)) {
162
            $data['user_data'] = $userData;
163
        }
164
165
        if (0 < count($volumes)) {
166
            $data['volumes'] = $volumes;
167
        }
168
169
        if (0 < count($tags)) {
170
            $data['tags'] = $tags;
171
        }
172
173
        $droplet = $this->adapter->post(sprintf('%s/droplets', $this->endpoint), $data);
174
175
        $droplet = json_decode($droplet);
176
177
        if (is_array($names)) {
178
            return array_map(function ($droplet) use ($wait, $waitTimeout) {
179
                $dropletEntity = new DropletEntity($droplet);
180
                if ($wait) {
181
                    return $this->waitForActive($dropletEntity, $waitTimeout);
182
                }
183
                return $dropletEntity;
184
            }, $droplet->droplets);
185
        }
186
187
        $dropletEntity = new DropletEntity($droplet->droplet);
188
189
        if ($wait) {
190
            return $this->waitForActive($dropletEntity, $waitTimeout);
191
        }
192
193
        return $dropletEntity;
194
    }
195
196
    /**
197
     * @param int $id
198
     *
199
     * @throws HttpException
200
     */
201
    public function delete($id)
202
    {
203
        $this->adapter->delete(sprintf('%s/droplets/%d', $this->endpoint, $id));
204
    }
205
206
    /**
207
     * @param int $id
208
     *
209
     * @throws HttpException
210
     *
211
     * @return KernelEntity[]
212
     */
213
    public function getAvailableKernels($id)
214
    {
215
        $kernels = $this->adapter->get(sprintf('%s/droplets/%d/kernels', $this->endpoint, $id));
216
217
        $kernels = json_decode($kernels);
218
219
        $this->meta = $this->extractMeta($kernels);
220
221
        return array_map(function ($kernel) {
222
            return new KernelEntity($kernel);
223
        }, $kernels->kernels);
224
    }
225
226
    /**
227
     * @param int $id
228
     *
229
     * @return ImageEntity[]
230
     */
231
    public function getSnapshots($id)
232
    {
233
        $snapshots = $this->adapter->get(sprintf('%s/droplets/%d/snapshots?per_page=%d', $this->endpoint, $id, 200));
234
235
        $snapshots = json_decode($snapshots);
236
237
        $this->meta = $this->extractMeta($snapshots);
238
239
        return array_map(function ($snapshot) {
240
            $snapshot = new ImageEntity($snapshot);
241
242
            return $snapshot;
243
        }, $snapshots->snapshots);
244
    }
245
246
    /**
247
     * @param int $id
248
     *
249
     * @return ImageEntity[]
250
     */
251
    public function getBackups($id)
252
    {
253
        $backups = $this->adapter->get(sprintf('%s/droplets/%d/backups?per_page=%d', $this->endpoint, $id, 200));
254
255
        $backups = json_decode($backups);
256
257
        $this->meta = $this->extractMeta($backups);
258
259
        return array_map(function ($backup) {
260
            return new ImageEntity($backup);
261
        }, $backups->backups);
262
    }
263
264
    /**
265
     * @param int $id
266
     *
267
     * @return ActionEntity[]
268
     */
269
    public function getActions($id)
270
    {
271
        $actions = $this->adapter->get(sprintf('%s/droplets/%d/actions?per_page=%d', $this->endpoint, $id, 200));
272
273
        $actions = json_decode($actions);
274
275
        $this->meta = $this->extractMeta($actions);
276
277
        return array_map(function ($action) {
278
            return new ActionEntity($action);
279
        }, $actions->actions);
280
    }
281
282
    /**
283
     * @param int $id
284
     * @param int $actionId
285
     *
286
     * @return ActionEntity
287
     */
288
    public function getActionById($id, $actionId)
289
    {
290
        $action = $this->adapter->get(sprintf('%s/droplets/%d/actions/%d', $this->endpoint, $id, $actionId));
291
292
        $action = json_decode($action);
293
294
        return new ActionEntity($action->action);
295
    }
296
297
    /**
298
     * @param int $id
299
     *
300
     * @throws HttpException
301
     *
302
     * @return ActionEntity
303
     */
304
    public function reboot($id)
305
    {
306
        return $this->executeAction($id, ['type' => 'reboot']);
307
    }
308
309
    /**
310
     * @param int $id
311
     *
312
     * @throws HttpException
313
     *
314
     * @return ActionEntity
315
     */
316
    public function powerCycle($id)
317
    {
318
        return $this->executeAction($id, ['type' => 'power_cycle']);
319
    }
320
321
    /**
322
     * @param int $id
323
     *
324
     * @throws HttpException
325
     *
326
     * @return ActionEntity
327
     */
328
    public function shutdown($id)
329
    {
330
        return $this->executeAction($id, ['type' => 'shutdown']);
331
    }
332
333
    /**
334
     * @param int $id
335
     *
336
     * @throws HttpException
337
     *
338
     * @return ActionEntity
339
     */
340
    public function powerOff($id)
341
    {
342
        return $this->executeAction($id, ['type' => 'power_off']);
343
    }
344
345
    /**
346
     * @param int $id
347
     *
348
     * @throws HttpException
349
     *
350
     * @return ActionEntity
351
     */
352
    public function powerOn($id)
353
    {
354
        return $this->executeAction($id, ['type' => 'power_on']);
355
    }
356
357
    /**
358
     * @param int $id
359
     *
360
     * @throws HttpException
361
     *
362
     * @return ActionEntity
363
     */
364
    public function passwordReset($id)
365
    {
366
        return $this->executeAction($id, ['type' => 'password_reset']);
367
    }
368
369
    /**
370
     * @param int    $id
371
     * @param string $size
372
     * @param bool   $disk
373
     *
374
     * @throws HttpException
375
     *
376
     * @return ActionEntity
377
     */
378
    public function resize($id, $size, $disk = true)
379
    {
380
        return $this->executeAction($id, ['type' => 'resize', 'size' => $size, 'disk' => $disk ? 'true' : 'false']);
381
    }
382
383
    /**
384
     * @param int $id
385
     * @param int $image
386
     *
387
     * @throws HttpException
388
     *
389
     * @return ActionEntity
390
     */
391
    public function restore($id, $image)
392
    {
393
        return $this->executeAction($id, ['type' => 'restore', 'image' => $image]);
394
    }
395
396
    /**
397
     * @param int        $id
398
     * @param int|string $image
399
     *
400
     * @throws HttpException
401
     *
402
     * @return ActionEntity
403
     */
404
    public function rebuild($id, $image)
405
    {
406
        return $this->executeAction($id, ['type' => 'rebuild', 'image' => $image]);
407
    }
408
409
    /**
410
     * @param int    $id
411
     * @param string $name
412
     *
413
     * @throws HttpException
414
     *
415
     * @return ActionEntity
416
     */
417
    public function rename($id, $name)
418
    {
419
        return $this->executeAction($id, ['type' => 'rename', 'name' => $name]);
420
    }
421
422
    /**
423
     * @param int $id
424
     * @param int $kernel
425
     *
426
     * @throws HttpException
427
     *
428
     * @return ActionEntity
429
     */
430
    public function changeKernel($id, $kernel)
431
    {
432
        return $this->executeAction($id, ['type' => 'change_kernel', 'kernel' => $kernel]);
433
    }
434
435
    /**
436
     * @param int $id
437
     *
438
     * @throws HttpException
439
     *
440
     * @return ActionEntity
441
     */
442
    public function enableIpv6($id)
443
    {
444
        return $this->executeAction($id, ['type' => 'enable_ipv6']);
445
    }
446
447
    /**
448
     * @param int $id
449
     *
450
     * @throws HttpException
451
     *
452
     * @return ActionEntity
453
     */
454
    public function enableBackups($id)
455
    {
456
        return $this->executeAction($id, ['type' => 'enable_backups']);
457
    }
458
459
    /**
460
     * @param int $id
461
     *
462
     * @throws HttpException
463
     *
464
     * @return ActionEntity
465
     */
466
    public function disableBackups($id)
467
    {
468
        return $this->executeAction($id, ['type' => 'disable_backups']);
469
    }
470
471
    /**
472
     * @param int $id
473
     *
474
     * @throws HttpException
475
     *
476
     * @return ActionEntity
477
     */
478
    public function enablePrivateNetworking($id)
479
    {
480
        return $this->executeAction($id, ['type' => 'enable_private_networking']);
481
    }
482
483
    /**
484
     * @param int    $id
485
     * @param string $name
486
     *
487
     * @throws HttpException
488
     *
489
     * @return ActionEntity
490
     */
491
    public function snapshot($id, $name)
492
    {
493
        return $this->executeAction($id, ['type' => 'snapshot', 'name' => $name]);
494
    }
495
496
    /**
497
     * @param int   $id
498
     * @param array $options
499
     *
500
     * @throws HttpException
501
     *
502
     * @return ActionEntity
503
     */
504
    private function executeAction($id, array $options)
505
    {
506
        $action = $this->adapter->post(sprintf('%s/droplets/%d/actions', $this->endpoint, $id), $options);
507
508
        $action = json_decode($action);
509
510
        return new ActionEntity($action->action);
511
    }
512
513
    /**
514
     * @param DropletEntity $droplet
515
     * @param int           $waitTimeout
516
     *
517
     * @throws HttpException
518
     *
519
     * @return DropletEntity|null
520
     */
521
    public function waitForActive($droplet, $waitTimeout)
522
    {
523
        $endTime = time() + $waitTimeout;
524
525
        while (time() < $endTime) {
526
            sleep(min(20, $endTime - time()));
527
            $droplet = $this->getById($droplet->id);
528
            if ($droplet->status === 'active') {
529
                return $droplet;
530
            }
531
        }
532
533
        return $droplet;
534
    }
535
}
536