Completed
Push — master ( 41d0c8...5d03eb )
by Graham
688:52 queued 607:19
created

Droplet::create()   C

Complexity

Conditions 8
Paths 16

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 9
Bugs 4 Features 1
Metric Value
c 9
b 4
f 1
dl 0
loc 31
rs 5.3846
cc 8
eloc 18
nc 16
nop 9

How to fix   Many Parameters   

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
20
/**
21
 * @author Yassir Hannoun <[email protected]>
22
 * @author Graham Campbell <[email protected]>
23
 */
24
class Droplet extends AbstractApi
25
{
26
    /**
27
     * @return DropletEntity[]
28
     */
29
    public function getAll()
30
    {
31
        $droplets = $this->adapter->get(sprintf('%s/droplets?per_page=%d', $this->endpoint, 200));
32
33
        $droplets = json_decode($droplets);
34
35
        $this->extractMeta($droplets);
36
37
        return array_map(function ($droplet) {
38
            return new DropletEntity($droplet);
39
        }, $droplets->droplets);
40
    }
41
42
    /**
43
     * @param int $id
44
     *
45
     * @return DropletEntity[]
46
     */
47
    public function getNeighborsById($id)
48
    {
49
        $droplets = $this->adapter->get(sprintf('%s/droplets/%d/neighbors', $this->endpoint, $id));
50
51
        $droplets = json_decode($droplets);
52
53
        return array_map(function ($droplet) {
54
            return new DropletEntity($droplet);
55
        }, $droplets->droplets);
56
    }
57
58
    /**
59
     * @return DropletEntity[]
60
     */
61
    public function getAllNeighbors()
62
    {
63
        $neighbors = $this->adapter->get(sprintf('%s/reports/droplet_neighbors', $this->endpoint));
64
65
        $neighbors = json_decode($neighbors);
66
67
        return array_map(function ($neighbor) {
68
            return new DropletEntity($neighbor);
69
        }, $neighbors->neighbors);
70
    }
71
72
    /**
73
     * @return UpgradeEntity[]
74
     */
75
    public function getUpgrades()
76
    {
77
        $upgrades = $this->adapter->get(sprintf('%s/droplet_upgrades', $this->endpoint));
78
79
        $upgrades = json_decode($upgrades);
80
81
        return array_map(function ($upgrade) {
82
            return new UpgradeEntity($upgrade);
83
        }, $upgrades);
84
    }
85
86
    /**
87
     * @param int $id
88
     *
89
     * @throws \RuntimeException
90
     *
91
     * @return DropletEntity
92
     */
93
    public function getById($id)
94
    {
95
        $droplet = $this->adapter->get(sprintf('%s/droplets/%d', $this->endpoint, $id));
96
97
        $droplet = json_decode($droplet);
98
99
        return new DropletEntity($droplet->droplet);
100
    }
101
102
    /**
103
     * @param array|string $names
104
     * @param string       $region
105
     * @param string       $size
106
     * @param string|int   $image
107
     * @param bool         $backups
108
     * @param bool         $ipv6
109
     * @param bool         $privateNetworking
110
     * @param int[]        $sshKeys
111
     * @param string       $userData
112
     *
113
     * @throws \RuntimeException
114
     *
115
     * @return DropletEntity|null
116
     */
117
    public function create($names, $region, $size, $image, $backups = false, $ipv6 = false, $privateNetworking = false, array $sshKeys = [], $userData = '')
118
    {
119
        $data = is_array($names) ? ['names' => $names] : ['name' => $names];
120
121
        $data = array_merge($data, [
122
            'region' => $region,
123
            'size' => $size,
124
            'image' => $image,
125
            'backups' => $backups ? 'true' : 'false',
126
            'ipv6' => $ipv6 ? 'true' : 'false',
127
            'private_networking' => $privateNetworking ? 'true' : 'false',
128
        ]);
129
130
        if (0 < count($sshKeys)) {
131
            $data['ssh_keys'] = $sshKeys;
132
        }
133
134
        if (!empty($userData)) {
135
            $data['user_data'] = $userData;
136
        }
137
138
        $droplet = $this->adapter->post(sprintf('%s/droplets', $this->endpoint), $data);
139
140
        if (is_array($names)) {
141
            return;
142
        }
143
144
        $droplet = json_decode($droplet);
145
146
        return new DropletEntity($droplet->droplet);
147
    }
148
149
    /**
150
     * @param int $id
151
     *
152
     * @throws \RuntimeException
153
     */
154
    public function delete($id)
155
    {
156
        $this->adapter->delete(sprintf('%s/droplets/%d', $this->endpoint, $id));
157
    }
158
159
    /**
160
     * @param int $id
161
     *
162
     * @throws \RuntimeException
163
     *
164
     * @return KernelEntity[]
165
     */
166
    public function getAvailableKernels($id)
167
    {
168
        $kernels = $this->adapter->get(sprintf('%s/droplets/%d/kernels', $this->endpoint, $id));
169
170
        $kernels = json_decode($kernels);
171
172
        $this->meta = $this->extractMeta($kernels);
173
174
        return array_map(function ($kernel) {
175
            return new KernelEntity($kernel);
176
        }, $kernels->kernels);
177
    }
178
179
    /**
180
     * @param int $id
181
     *
182
     * @return ImageEntity[]
183
     */
184
    public function getSnapshots($id)
185
    {
186
        $snapshots = $this->adapter->get(sprintf('%s/droplets/%d/snapshots?per_page=%d', $this->endpoint, $id, 200));
187
188
        $snapshots = json_decode($snapshots);
189
190
        $this->meta = $this->extractMeta($snapshots);
191
192
        return array_map(function ($snapshot) {
193
            $snapshot = new ImageEntity($snapshot);
194
195
            return $snapshot;
196
        }, $snapshots->snapshots);
197
    }
198
199
    /**
200
     * @param int $id
201
     *
202
     * @return ImageEntity[]
203
     */
204
    public function getBackups($id)
205
    {
206
        $backups = $this->adapter->get(sprintf('%s/droplets/%d/backups?per_page=%d', $this->endpoint, $id, 200));
207
208
        $backups = json_decode($backups);
209
210
        $this->meta = $this->extractMeta($backups);
211
212
        return array_map(function ($backup) {
213
            return new ImageEntity($backup);
214
        }, $backups->backups);
215
    }
216
217
    /**
218
     * @param int $id
219
     *
220
     * @return ActionEntity[]
221
     */
222
    public function getActions($id)
223
    {
224
        $actions = $this->adapter->get(sprintf('%s/droplets/%d/actions?per_page=%d', $this->endpoint, $id, 200));
225
226
        $actions = json_decode($actions);
227
228
        $this->meta = $this->extractMeta($actions);
229
230
        return array_map(function ($action) {
231
            return new ActionEntity($action);
232
        }, $actions->actions);
233
    }
234
235
    /**
236
     * @param int $id
237
     * @param int $actionId
238
     *
239
     * @return ActionEntity
240
     */
241
    public function getActionById($id, $actionId)
242
    {
243
        $action = $this->adapter->get(sprintf('%s/droplets/%d/actions/%d', $this->endpoint, $id, $actionId));
244
245
        $action = json_decode($action);
246
247
        return new ActionEntity($action->action);
248
    }
249
250
    /**
251
     * @param int $id
252
     *
253
     * @throws \RuntimeException
254
     *
255
     * @return ActionEntity
256
     */
257
    public function reboot($id)
258
    {
259
        return $this->executeAction($id, ['type' => 'reboot']);
260
    }
261
262
    /**
263
     * @param int $id
264
     *
265
     * @throws \RuntimeException
266
     *
267
     * @return ActionEntity
268
     */
269
    public function powerCycle($id)
270
    {
271
        return $this->executeAction($id, ['type' => 'power_cycle']);
272
    }
273
274
    /**
275
     * @param int $id
276
     *
277
     * @throws \RuntimeException
278
     *
279
     * @return ActionEntity
280
     */
281
    public function shutdown($id)
282
    {
283
        return $this->executeAction($id, ['type' => 'shutdown']);
284
    }
285
286
    /**
287
     * @param int $id
288
     *
289
     * @throws \RuntimeException
290
     *
291
     * @return ActionEntity
292
     */
293
    public function powerOff($id)
294
    {
295
        return $this->executeAction($id, ['type' => 'power_off']);
296
    }
297
298
    /**
299
     * @param int $id
300
     *
301
     * @throws \RuntimeException
302
     *
303
     * @return ActionEntity
304
     */
305
    public function powerOn($id)
306
    {
307
        return $this->executeAction($id, ['type' => 'power_on']);
308
    }
309
310
    /**
311
     * @param int $id
312
     *
313
     * @throws \RuntimeException
314
     *
315
     * @return ActionEntity
316
     */
317
    public function passwordReset($id)
318
    {
319
        return $this->executeAction($id, ['type' => 'password_reset']);
320
    }
321
322
    /**
323
     * @param int    $id
324
     * @param string $size
325
     * @param bool   $disk
326
     *
327
     * @throws \RuntimeException
328
     *
329
     * @return ActionEntity
330
     */
331
    public function resize($id, $size, $disk = true)
332
    {
333
        return $this->executeAction($id, ['type' => 'resize', 'size' => $size, 'disk' => $disk ? 'true' : 'false']);
334
    }
335
336
    /**
337
     * @param int $id
338
     * @param int $image
339
     *
340
     * @throws \RuntimeException
341
     *
342
     * @return ActionEntity
343
     */
344
    public function restore($id, $image)
345
    {
346
        return $this->executeAction($id, ['type' => 'restore', 'image' => $image]);
347
    }
348
349
    /**
350
     * @param int        $id
351
     * @param int|string $image
352
     *
353
     * @throws \RuntimeException
354
     *
355
     * @return ActionEntity
356
     */
357
    public function rebuild($id, $image)
358
    {
359
        return $this->executeAction($id, ['type' => 'rebuild', 'image' => $image]);
360
    }
361
362
    /**
363
     * @param int    $id
364
     * @param string $name
365
     *
366
     * @throws \RuntimeException
367
     *
368
     * @return ActionEntity
369
     */
370
    public function rename($id, $name)
371
    {
372
        return $this->executeAction($id, ['type' => 'rename', 'name' => $name]);
373
    }
374
375
    /**
376
     * @param int $id
377
     * @param int $kernel
378
     *
379
     * @throws \RuntimeException
380
     *
381
     * @return ActionEntity
382
     */
383
    public function changeKernel($id, $kernel)
384
    {
385
        return $this->executeAction($id, ['type' => 'change_kernel', 'kernel' => $kernel]);
386
    }
387
388
    /**
389
     * @param int $id
390
     *
391
     * @throws \RuntimeException
392
     *
393
     * @return ActionEntity
394
     */
395
    public function enableIpv6($id)
396
    {
397
        return $this->executeAction($id, ['type' => 'enable_ipv6']);
398
    }
399
400
    /**
401
     * @param int $id
402
     *
403
     * @throws \RuntimeException
404
     *
405
     * @return ActionEntity
406
     */
407
    public function enableBackups($id)
408
    {
409
        return $this->executeAction($id, ['type' => 'enable_backups']);
410
    }
411
412
    /**
413
     * @param int $id
414
     *
415
     * @throws \RuntimeException
416
     *
417
     * @return ActionEntity
418
     */
419
    public function disableBackups($id)
420
    {
421
        return $this->executeAction($id, ['type' => 'disable_backups']);
422
    }
423
424
    /**
425
     * @param int $id
426
     *
427
     * @throws \RuntimeException
428
     *
429
     * @return ActionEntity
430
     */
431
    public function enablePrivateNetworking($id)
432
    {
433
        return $this->executeAction($id, ['type' => 'enable_private_networking']);
434
    }
435
436
    /**
437
     * @param int    $id
438
     * @param string $name
439
     *
440
     * @throws \RuntimeException
441
     *
442
     * @return ActionEntity
443
     */
444
    public function snapshot($id, $name)
445
    {
446
        return $this->executeAction($id, ['type' => 'snapshot', 'name' => $name]);
447
    }
448
449
    /**
450
     * @param int   $id
451
     * @param array $options
452
     *
453
     * @throws \RuntimeException
454
     *
455
     * @return ActionEntity
456
     */
457
    private function executeAction($id, array $options)
458
    {
459
        $action = $this->adapter->post(sprintf('%s/droplets/%d/actions', $this->endpoint, $id), $options);
460
461
        $action = json_decode($action);
462
463
        return new ActionEntity($action->action);
464
    }
465
}
466