FloatingIp::createAssigned()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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