Completed
Pull Request — master (#132)
by Yassir
02:00
created

Volume   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 8
Bugs 0 Features 7
Metric Value
wmc 7
c 8
b 0
f 7
lcom 1
cbo 3
dl 0
loc 101
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getAll() 0 13 2
A getByNameAndRegion() 0 12 1
A getById() 0 8 1
A create() 0 15 1
A delete() 0 4 1
A deleteWithNameAndRegion() 0 4 1
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\Volume as VolumeEntity;
15
16
/**
17
 * @author Yassir Hannoun <[email protected]>
18
 */
19
class Volume extends AbstractApi
20
{
21
    /**
22
     * @param string $regionSlug restricts results to volumes available in a specific region.
23
     *
24
     * @return VolumeEntity[] Lists all of the Block Storage volumes available.
25
     */
26
    public function getAll($regionSlug = null)
27
    {
28
        $regionQueryParameter = is_null($regionSlug) ? '' : sprintf('&region=%s', $regionSlug);
29
        $volumes = $this->adapter->get(sprintf('%s/volumes?per_page=%d%s', $this->endpoint, 200, $regionQueryParameter));
30
31
        $volumes = json_decode($volumes);
32
33
        $this->extractMeta($volumes);
34
35
        return array_map(function ($volume) {
36
            return new VolumeEntity($volume);
37
        }, $volumes->volumes);
38
    }
39
40
    /**
41
     * @param string $driveName  restricts results to volumes with the specified name.
42
     * @param string $regionSlug restricts results to volumes available in a specific region.
43
     *
44
     * @return VolumeEntity[] Lists all of the Block Storage volumes available.
45
     */
46
    public function getByNameAndRegion($driveName, $regionSlug)
47
    {
48
        $volumes = $this->adapter->get(sprintf('%s/volumes?per_page=%d&region=%s&name=%s', $this->endpoint, 200, $regionSlug, $driveName));
49
50
        $volumes = json_decode($volumes);
51
52
        $this->extractMeta($volumes);
53
54
        return array_map(function ($volume) {
55
            return new VolumeEntity($volume);
56
        }, $volumes->volumes);
57
    }
58
59
    /**
60
     * @param string $id
61
     *
62
     * @return VolumeEntity the Block Storage volume with the specified id.
63
     */
64
    public function getById($id)
65
    {
66
        $volume = $this->adapter->get(sprintf('%s/volumes/%s?per_page=%d', $this->endpoint, $id, 200));
67
68
        $volume = json_decode($volume);
69
70
        return new VolumeEntity($volume->volume);
71
    }
72
73
    /**
74
     * @param string $name            A human-readable name for the Block Storage volume.
75
     * @param string $description     Free-form text field to describe a Block Storage volume.
76
     * @param string $sizeInGigabytes The size of the Block Storage volume in GiB.
77
     * @param string $regionSlug      The region where the Block Storage volume will be created.
78
     *
79
     * @throws HttpException
80
     * 
81
     * @return VolumeEntity the Block Storage volume that was created.
82
     */
83
    public function create($name, $description, $sizeInGigabytes, $regionSlug)
84
    {
85
        $data = [
86
            'size_gigabytes' => $sizeInGigabytes,
87
            'name' => $name,
88
            'description' => $description,
89
            'region' => $regionSlug,
90
        ];
91
92
        $volume = $this->adapter->post(sprintf('%s/volumes', $this->endpoint), $data);
93
94
        $volume = json_decode($volume);
95
96
        return new VolumeEntity($volume->volume);
97
    }
98
99
    /**
100
     * @param string $id
101
     *
102
     * @throws HttpException
103
     */
104
    public function delete($id)
105
    {
106
        $this->adapter->delete(sprintf('%s/volumes/%s', $this->endpoint, $id));
107
    }
108
109
    /**
110
     * @param string $driveName  restricts the search to volumes with the specified name.
111
     * @param string $regionSlug restricts the search to volumes available in a specific region.
112
     *
113
     * @throws HttpException
114
     */
115
    public function deleteWithNameAndRegion($driveName, $regionSlug)
116
    {
117
        $this->adapter->delete(sprintf('%s/volumes?name=%s&region=%s', $this->endpoint, $driveName, $regionSlug));
118
    }
119
}
120