Passed
Push — master ( 8de9f1...2c24c4 )
by Artem
01:57
created

LinodeRepository::getCurrentTransfer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 1
rs 10
1
<?php
2
3
// ---------------------------------------------------------------------
4
//
5
//  Copyright (C) 2018-2024 Artem Rodygin
6
//
7
//  You should have received a copy of the MIT License along with
8
//  this file. If not, see <http://opensource.org/licenses/MIT>.
9
//
10
// ---------------------------------------------------------------------
11
12
namespace Linode\Internal;
13
14
use Linode\Entity\Entity;
15
use Linode\Entity\Linode;
16
use Linode\Repository\LinodeRepositoryInterface;
17
18
class LinodeRepository extends AbstractRepository implements LinodeRepositoryInterface
19
{
20 1
    public function create(array $parameters): Linode
21
    {
22 1
        $this->checkParametersSupport($parameters);
23
24 1
        $response = $this->client->api($this->client::REQUEST_POST, $this->getBaseUri(), $parameters);
25 1
        $contents = $response->getBody()->getContents();
26 1
        $json     = json_decode($contents, true);
27
28 1
        return new Linode($this->client, $json);
29
    }
30
31 1
    public function update(int $id, array $parameters): Linode
32
    {
33 1
        $this->checkParametersSupport($parameters);
34
35 1
        $response = $this->client->api($this->client::REQUEST_PUT, sprintf('%s/%s', $this->getBaseUri(), $id), $parameters);
36 1
        $contents = $response->getBody()->getContents();
37 1
        $json     = json_decode($contents, true);
38
39 1
        return new Linode($this->client, $json);
40
    }
41
42 1
    public function delete(int $id): void
43
    {
44 1
        $this->client->api($this->client::REQUEST_DELETE, sprintf('%s/%s', $this->getBaseUri(), $id));
45
    }
46
47 1
    public function clone(int $id, array $parameters): void
48
    {
49 1
        $this->checkParametersSupport($parameters);
50
51 1
        $this->client->api($this->client::REQUEST_POST, sprintf('%s/%s/clone', $this->getBaseUri(), $id), $parameters);
52
    }
53
54 1
    public function rebuild(int $id, array $parameters): Linode
55
    {
56 1
        $this->checkParametersSupport($parameters);
57
58 1
        $response = $this->client->api($this->client::REQUEST_POST, sprintf('%s/%s/rebuild', $this->getBaseUri(), $id), $parameters);
59 1
        $contents = $response->getBody()->getContents();
60 1
        $json     = json_decode($contents, true);
61
62 1
        return new Linode($this->client, $json);
63
    }
64
65 1
    public function resize(int $id, string $type, bool $allow_auto_disk_resize = true): void
66
    {
67 1
        $parameters = [
68 1
            'type'                   => $type,
69 1
            'allow_auto_disk_resize' => $allow_auto_disk_resize,
70 1
        ];
71
72 1
        $this->client->api($this->client::REQUEST_POST, sprintf('%s/%s/resize', $this->getBaseUri(), $id), $parameters);
73
    }
74
75 1
    public function mutate(int $id, bool $allow_auto_disk_resize = true): void
76
    {
77 1
        $parameters = [
78 1
            'allow_auto_disk_resize' => $allow_auto_disk_resize,
79 1
        ];
80
81 1
        $this->client->api($this->client::REQUEST_POST, sprintf('%s/%s/mutate', $this->getBaseUri(), $id), $parameters);
82
    }
83
84 1
    public function migrate(int $id, string $region = null): void
85
    {
86 1
        $parameters = [];
87
88 1
        if ($region) {
89 1
            $parameters['region'] = $region;
90
        }
91
92 1
        $this->client->api($this->client::REQUEST_POST, sprintf('%s/%s/migrate', $this->getBaseUri(), $id), $parameters);
93
    }
94
95 2
    public function boot(int $id, int $config_id = null): void
96
    {
97 2
        $parameters = [
98 2
            'config_id' => $config_id,
99 2
        ];
100
101 2
        $this->client->api($this->client::REQUEST_POST, sprintf('%s/%s/boot', $this->getBaseUri(), $id), $parameters);
102
    }
103
104 2
    public function reboot(int $id, int $config_id = null): void
105
    {
106 2
        $parameters = [
107 2
            'config_id' => $config_id,
108 2
        ];
109
110 2
        $this->client->api($this->client::REQUEST_POST, sprintf('%s/%s/reboot', $this->getBaseUri(), $id), $parameters);
111
    }
112
113 1
    public function shutdown(int $id): void
114
    {
115 1
        $this->client->api($this->client::REQUEST_POST, sprintf('%s/%s/shutdown', $this->getBaseUri(), $id));
116
    }
117
118 1
    public function rescue(int $id, array $parameters): void
119
    {
120 1
        $this->checkParametersSupport($parameters);
121
122 1
        $this->client->api($this->client::REQUEST_POST, sprintf('%s/%s/rescue', $this->getBaseUri(), $id), $parameters);
123
    }
124
125 1
    public function enableBackups(int $id): void
126
    {
127 1
        $this->client->api($this->client::REQUEST_POST, sprintf('%s/%s/backups/enable', $this->getBaseUri(), $id));
128
    }
129
130 1
    public function cancelBackups(int $id): void
131
    {
132 1
        $this->client->api($this->client::REQUEST_POST, sprintf('%s/%s/backups/cancel', $this->getBaseUri(), $id));
133
    }
134
135 1
    public function createSnapshot(int $id, string $label): Linode\Backup
136
    {
137 1
        $parameters = [
138 1
            'label' => $label,
139 1
        ];
140
141 1
        $response = $this->client->api($this->client::REQUEST_POST, sprintf('%s/%s/backups', $this->getBaseUri(), $id), $parameters);
142 1
        $contents = $response->getBody()->getContents();
143 1
        $json     = json_decode($contents, true);
144
145 1
        return new Linode\Backup($this->client, $json);
146
    }
147
148 1
    public function restoreBackup(int $source_id, int $backup_id, int $target_id, bool $overwrite = true): void
149
    {
150 1
        $parameters = [
151 1
            'linode_id' => $target_id,
152 1
            'overwrite' => $overwrite,
153 1
        ];
154
155 1
        $uri = sprintf('%s/%s/backups/%s/restore', $this->getBaseUri(), $source_id, $backup_id);
156
157 1
        $this->client->api($this->client::REQUEST_POST, $uri, $parameters);
158
    }
159
160 1
    public function getBackup(int $id, int $backup_id): Linode\Backup
161
    {
162 1
        $response = $this->client->api($this->client::REQUEST_GET, sprintf('%s/%s/backups/%s', $this->getBaseUri(), $id, $backup_id));
163 1
        $contents = $response->getBody()->getContents();
164 1
        $json     = json_decode($contents, true);
165
166 1
        return new Linode\Backup($this->client, $json);
167
    }
168
169 1
    public function getAllBackups(int $id): array
170
    {
171 1
        $response = $this->client->api($this->client::REQUEST_GET, sprintf('%s/%s/backups', $this->getBaseUri(), $id));
172 1
        $contents = $response->getBody()->getContents();
173
174 1
        return json_decode($contents, true);
175
    }
176
177 1
    public function getCurrentTransfer(int $id): Linode\LinodeTransfer
178
    {
179 1
        $response = $this->client->api($this->client::REQUEST_GET, sprintf('%s/%s/transfer', $this->getBaseUri(), $id));
180 1
        $contents = $response->getBody()->getContents();
181 1
        $json     = json_decode($contents, true);
182
183 1
        return new Linode\LinodeTransfer($this->client, $json);
184
    }
185
186 1
    public function getCurrentStats(int $id): Linode\LinodeStats
187
    {
188 1
        $response = $this->client->api($this->client::REQUEST_GET, sprintf('%s/%s/stats', $this->getBaseUri(), $id));
189 1
        $contents = $response->getBody()->getContents();
190 1
        $json     = json_decode($contents, true);
191
192 1
        return new Linode\LinodeStats($this->client, $json);
193
    }
194
195 1
    public function getMonthlyStats(int $id, int $year, int $month): Linode\LinodeStats
196
    {
197 1
        $response = $this->client->api($this->client::REQUEST_GET, sprintf('%s/%s/stats/%s/%02s', $this->getBaseUri(), $id, $year, $month));
198 1
        $contents = $response->getBody()->getContents();
199 1
        $json     = json_decode($contents, true);
200
201 1
        return new Linode\LinodeStats($this->client, $json);
202
    }
203
204 24
    protected function getBaseUri(): string
205
    {
206 24
        return '/linode/instances';
207
    }
208
209 6
    protected function getSupportedFields(): array
210
    {
211 6
        return [
212 6
            Linode::FIELD_ID,
213 6
            Linode::FIELD_LABEL,
214 6
            Linode::FIELD_REGION,
215 6
            Linode::FIELD_IMAGE,
216 6
            Linode::FIELD_TYPE,
217 6
            Linode::FIELD_STATUS,
218 6
            Linode::FIELD_IPV4,
219 6
            Linode::FIELD_IPV6,
220 6
            Linode::FIELD_HYPERVISOR,
221 6
            Linode::FIELD_WATCHDOG_ENABLED,
222 6
            Linode::FIELD_CREATED,
223 6
            Linode::FIELD_UPDATED,
224 6
            Linode::FIELD_GROUP,
225 6
            Linode::FIELD_TAGS,
226 6
            Linode::FIELD_SPECS,
227 6
            Linode::FIELD_ALERTS,
228 6
            Linode::FIELD_BACKUPS,
229 6
            Linode::FIELD_LINODE_ID,
230 6
            Linode::FIELD_ROOT_PASS,
231 6
            Linode::FIELD_SWAP_SIZE,
232 6
            Linode::FIELD_BOOTED,
233 6
            Linode::FIELD_PRIVATE_IP,
234 6
            Linode::FIELD_AUTHORIZED_KEYS,
235 6
            Linode::FIELD_AUTHORIZED_USERS,
236 6
            Linode::FIELD_BACKUP_ID,
237 6
            Linode::FIELD_BACKUPS_ENABLED,
238 6
            Linode::FIELD_STACKSCRIPT_ID,
239 6
            Linode::FIELD_STACKSCRIPT_DATA,
240 6
            Linode::FIELD_DEVICES,
241 6
            Linode::FIELD_DISKS,
242 6
            Linode::FIELD_CONFIGS,
243 6
        ];
244
    }
245
246 1
    protected function jsonToEntity(array $json): Entity
247
    {
248 1
        return new Linode($this->client, $json);
249
    }
250
}
251