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

FloatingIp::getAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 14
rs 9.4286
cc 1
eloc 8
nc 1
nop 0
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\FloatingIp as FloatingIpEntity;
16
17
/**
18
 * @author Graham Campbell <[email protected]>
19
 */
20
class FloatingIp extends AbstractApi
21
{
22
    /**
23
     * @return FloatingIpEntity[]
24
     */
25
    public function getAll()
26
    {
27
        $query = sprintf('%s/floating_ips?per_page=%d', $this->endpoint, 200);
28
29
        $ips = $this->adapter->get($query);
30
31
        $ips = json_decode($ips);
32
33
        $this->extractMeta($ips);
34
35
        return array_map(function ($ip) {
36
            return new FloatingIpEntity($ip);
37
        }, $ips->floating_ips);
38
    }
39
40
    /**
41
     * @param int $id
42
     *
43
     * @return FloatingIpEntity
44
     */
45
    public function getById($id)
46
    {
47
        $ip = $this->adapter->get(sprintf('%s/floating_ips/%s', $this->endpoint, $id));
48
49
        $ip = json_decode($ip);
50
51
        return new FloatingIpEntity($ip->floating_ip);
52
    }
53
54
    /**
55
     * @param int $dropletId
56
     *
57
     * @throws \RuntimeException
58
     *
59
     * @return FloatingIpEntity
60
     */
61
    public function createAssigned($dropletId)
62
    {
63
        $ip = $this->adapter->post(sprintf('%s/floating_ips', $this->endpoint), ['droplet_id' => $dropletId]);
64
65
        $ip = json_decode($ip);
66
67
        return new FloatingIpEntity($ip->floating_ip);
68
    }
69
70
    /**
71
     * @param string $regionSlug
72
     *
73
     * @throws \RuntimeException
74
     *
75
     * @return FloatingIpEntity
76
     */
77
    public function createReserved($regionSlug)
78
    {
79
        $ip = $this->adapter->post(sprintf('%s/floating_ips', $this->endpoint), ['region' => $regionSlug]);
80
81
        $ip = json_decode($ip);
82
83
        return new FloatingIpEntity($ip->floating_ip);
84
    }
85
86
    /**
87
     * @param int $id
88
     *
89
     * @throws \RuntimeException
90
     */
91
    public function delete($id)
92
    {
93
        $this->adapter->delete(sprintf('%s/floating_ips/%s', $this->endpoint, $id));
94
    }
95
96
    /**
97
     * @param int $id
98
     *
99
     * @return ActionEntity[]
100
     */
101
    public function getActions($id)
102
    {
103
        $actions = $this->adapter->get(sprintf('%s/floating_ips/%s/actions?per_page=%d', $this->endpoint, $id, 200));
104
105
        $actions = json_decode($actions);
106
107
        $this->meta = $this->extractMeta($actions);
108
109
        return array_map(function ($action) {
110
            return new ActionEntity($action);
111
        }, $actions->actions);
112
    }
113
114
    /**
115
     * @param int $id
116
     * @param int $actionId
117
     *
118
     * @return ActionEntity
119
     */
120
    public function getActionById($id, $actionId)
121
    {
122
        $action = $this->adapter->get(sprintf('%s/floating_ips/%s/actions/%d', $this->endpoint, $id, $actionId));
123
124
        $action = json_decode($action);
125
126
        return new ActionEntity($action->action);
127
    }
128
129
    /**
130
     * @param int $id
131
     * @param int $dropletId
132
     *
133
     * @throws \RuntimeException
134
     *
135
     * @return ActionEntity
136
     */
137
    public function assign($id, $dropletId)
138
    {
139
        return $this->executeAction($id, ['type' => 'assign', 'droplet_id' => $dropletId]);
140
    }
141
142
    /**
143
     * @param int $id
144
     *
145
     * @throws \RuntimeException
146
     *
147
     * @return ActionEntity
148
     */
149
    public function unassign($id)
150
    {
151
        return $this->executeAction($id, ['type' => 'unassign']);
152
    }
153
154
    /**
155
     * @param int   $id
156
     * @param array $options
157
     *
158
     * @throws \RuntimeException
159
     *
160
     * @return ActionEntity
161
     */
162
    private function executeAction($id, array $options)
163
    {
164
        $action = $this->adapter->post(sprintf('%s/floating_ips/%s/actions', $this->endpoint, $id), $options);
165
166
        $action = json_decode($action);
167
168
        return new ActionEntity($action->action);
169
    }
170
}
171