Block::labelSet()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
dl 8
loc 8
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 2
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\Block as BlockEntity;
13
14
/**
15
 * @author Albert Leitato <[email protected]>
16
 */
17
class Block extends AbstractApi
18
{
19
    /**
20
     * Retrieve a list of any active block storage subscriptions on this account.
21
     *
22
     * @return BlockEntity
23
     */
24
    public function list()
25
    {
26
        $blocks = $this->adapter->get(\sprintf('%s/block/list', $this->endpoint));
27
28
        $blocks = \json_decode($blocks, true);
29
30
        return \array_map(function ($block) {
31
            return new BlockEntity($block);
32
        }, $blocks);
33
    }
34
35
    /**
36
     * Retrieve a list of any active block storage subscriptions on this account
37
     *
38
     * @return BlockEntity
39
     *
40
     * @param int $subId Id for the Block storage
41
     */
42
    public function getById($subId)
43
    {
44
        $response = $this->adapter->get(\sprintf('%s/block/list?SUBID=%d', $this->endpoint, $subId));
45
46
        return $this->handleResponse($response, BlockEntity::class);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->handleResponse($r...r\Entity\Block::class); of type array|object adds the type array to the return on line 46 which is incompatible with the return type documented by Vultr\Api\Block::getById of type Vultr\Entity\Block.
Loading history...
47
    }
48
49
    /**
50
     * Create a block storage subscription.
51
     *
52
     * @param int    $dc_id DCID of the location to create this subscription in
53
     * @param int    $size  size (in GB) of this subscription
54
     * @param string $label Text label that will be associated with the subscription
55
     **/
56
    public function create($dc_id, int $size, $label = '')
57
    {
58
        $data = [
59
            'DCID'    => $dc_id,
60
            'size_gb' => $size,
61
        ];
62
        if (!empty($label)) {
63
            $data['label'] = $label;
64
        }
65
        $response = $this->adapter->post(\sprintf('%s/block/create', $this->endpoint), http_build_query($data));
66
67
        return \json_decode($response);
68
    }
69
70
    /**
71
     * Delete a block storage subscription. All data will be permanently lost.
72
     *
73
     * There is no going back from this call.
74
     *
75
     * @param int $subid ID of the block storage subscription to delete
76
     *
77
     * @throws HttpException
78
     */
79
    public function delete($subid)
80
    {
81
        $this->adapter->post(\sprintf('%s/block/delete', $this->endpoint), http_build_query(['SUBID' => $subid]));
82
    }
83
84
    /**
85
     * Detach a block storage subscription from the currently attached instance.
86
     *
87
     * @param int $subid ID of the block storage subscription to detach
88
     *
89
     * @throws HttpException
90
     */
91
    public function detach($subid)
92
    {
93
        $this->adapter->post(\sprintf('%s/block/detach', $this->endpoint), http_build_query(['SUBID' => $subid]));
94
    }
95
96
    /**
97
     * Set the label of a block storage subscription.
98
     *
99
     * @param int    $subid ID of the block storage subscription to detach
100
     * @param string $label Text label that will be shown in the control panel
101
     *
102
     * @throws HttpException
103
     */
104 View Code Duplication
    public function labelSet($subid, $label)
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...
105
    {
106
        $data = [
107
            'SUBID' => $subid,
108
            'label' => $label,
109
        ];
110
        $this->adapter->post(\sprintf('%s/block/label_set', $this->endpoint), http_build_query($data));
111
    }
112
113
    /**
114
     * Resize the block storage volume to a new size.
115
     *
116
     * WARNING: When shrinking the volume,
117
     * you must manually shrink the filesystem and partitions beforehand,
118
     * or you will lose data.
119
     *
120
     * @param int    $subid ID of the block storage subscription to resize
121
     * @param string $size  New size (in GB) of the block storage subscription
122
     *
123
     * @throws HttpException
124
     */
125 View Code Duplication
    public function resize($subid, $size)
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...
126
    {
127
        $data = [
128
            'SUBID'   => $subid,
129
            'size_gb' => $size,
130
        ];
131
        $this->adapter->post(\sprintf('%s/block/resize', $this->endpoint), http_build_query($data));
132
    }
133
}
134