Passed
Push — master ( 0676a7...01ab94 )
by Artem
11:54
created

LinodeRepository   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 19
eloc 58
dl 0
loc 141
c 0
b 0
f 0
rs 10

19 Methods

Rating   Name   Duplication   Size   Complexity  
A createLinodeInstance() 0 7 1
A mutateLinodeInstance() 0 3 1
A resizeLinodeInstance() 0 3 1
A getLinodeTransfer() 0 7 1
A getLinodeStats() 0 7 1
A shutdownLinodeInstance() 0 3 1
A rescueLinodeInstance() 0 3 1
A getLinodeVolumes() 0 7 1
A migrateLinodeInstance() 0 3 1
A getBaseUri() 0 3 1
A bootLinodeInstance() 0 3 1
A deleteLinodeInstance() 0 3 1
A updateLinodeInstance() 0 7 1
A rebuildLinodeInstance() 0 7 1
A cloneLinodeInstance() 0 3 1
A getSupportedFields() 0 20 1
A getLinodeStatsByYearMonth() 0 7 1
A rebootLinodeInstance() 0 3 1
A jsonToEntity() 0 3 1
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 <https://opensource.org/licenses/MIT>.
9
//
10
// ---------------------------------------------------------------------
11
12
namespace Linode\LinodeInstances\Repository;
13
14
use Linode\Account\Transfer;
15
use Linode\Entity;
16
use Linode\Internal\AbstractRepository;
17
use Linode\LinodeInstances\Linode;
18
use Linode\LinodeInstances\LinodeRepositoryInterface;
19
use Linode\LinodeInstances\LinodeStats;
20
use Linode\Volumes\Volume;
21
22
/**
23
 * @codeCoverageIgnore This class was autogenerated.
24
 */
25
class LinodeRepository extends AbstractRepository implements LinodeRepositoryInterface
26
{
27
    public function createLinodeInstance(array $parameters = []): Linode
28
    {
29
        $response = $this->client->post($this->getBaseUri(), $parameters);
30
        $contents = $response->getBody()->getContents();
31
        $json     = json_decode($contents, true);
32
33
        return new Linode($this->client, $json);
34
    }
35
36
    public function updateLinodeInstance(int $linodeId, array $parameters = []): Linode
37
    {
38
        $response = $this->client->put(sprintf('%s/%s', $this->getBaseUri(), $linodeId), $parameters);
39
        $contents = $response->getBody()->getContents();
40
        $json     = json_decode($contents, true);
41
42
        return new Linode($this->client, $json);
43
    }
44
45
    public function deleteLinodeInstance(int $linodeId): void
46
    {
47
        $this->client->delete(sprintf('%s/%s', $this->getBaseUri(), $linodeId));
48
    }
49
50
    public function bootLinodeInstance(int $linodeId, array $parameters = []): void
51
    {
52
        $this->client->post(sprintf('%s/%s/boot', $this->getBaseUri(), $linodeId), $parameters);
53
    }
54
55
    public function cloneLinodeInstance(int $linodeId, array $parameters = []): void
56
    {
57
        $this->client->post(sprintf('%s/%s/clone', $this->getBaseUri(), $linodeId), $parameters);
58
    }
59
60
    public function migrateLinodeInstance(int $linodeId, array $parameters = []): void
61
    {
62
        $this->client->post(sprintf('%s/%s/migrate', $this->getBaseUri(), $linodeId), $parameters);
63
    }
64
65
    public function mutateLinodeInstance(int $linodeId, array $parameters = []): void
66
    {
67
        $this->client->post(sprintf('%s/%s/mutate', $this->getBaseUri(), $linodeId), $parameters);
68
    }
69
70
    public function rebootLinodeInstance(int $linodeId, array $parameters = []): void
71
    {
72
        $this->client->post(sprintf('%s/%s/reboot', $this->getBaseUri(), $linodeId), $parameters);
73
    }
74
75
    public function rebuildLinodeInstance(int $linodeId, array $parameters = []): Linode
76
    {
77
        $response = $this->client->post(sprintf('%s/%s/rebuild', $this->getBaseUri(), $linodeId), $parameters);
78
        $contents = $response->getBody()->getContents();
79
        $json     = json_decode($contents, true);
80
81
        return new Linode($this->client, $json);
82
    }
83
84
    public function rescueLinodeInstance(int $linodeId, array $parameters = []): void
85
    {
86
        $this->client->post(sprintf('%s/%s/rescue', $this->getBaseUri(), $linodeId), $parameters);
87
    }
88
89
    public function resizeLinodeInstance(int $linodeId, array $parameters = []): void
90
    {
91
        $this->client->post(sprintf('%s/%s/resize', $this->getBaseUri(), $linodeId), $parameters);
92
    }
93
94
    public function shutdownLinodeInstance(int $linodeId): void
95
    {
96
        $this->client->post(sprintf('%s/%s/shutdown', $this->getBaseUri(), $linodeId));
97
    }
98
99
    public function getLinodeStats(int $linodeId): LinodeStats
100
    {
101
        $response = $this->client->get(sprintf('%s/%s/stats', $this->getBaseUri(), $linodeId));
102
        $contents = $response->getBody()->getContents();
103
        $json     = json_decode($contents, true);
104
105
        return new LinodeStats($this->client, $json);
106
    }
107
108
    public function getLinodeStatsByYearMonth(int $linodeId, int $year, int $month): LinodeStats
109
    {
110
        $response = $this->client->get(sprintf('%s/%s/stats/%s/%s', $this->getBaseUri(), $linodeId, $year, $month));
111
        $contents = $response->getBody()->getContents();
112
        $json     = json_decode($contents, true);
113
114
        return new LinodeStats($this->client, $json);
115
    }
116
117
    public function getLinodeTransfer(int $linodeId): Transfer
118
    {
119
        $response = $this->client->get(sprintf('%s/%s/transfer', $this->getBaseUri(), $linodeId));
120
        $contents = $response->getBody()->getContents();
121
        $json     = json_decode($contents, true);
122
123
        return new Transfer($this->client, $json);
124
    }
125
126
    public function getLinodeVolumes(int $linodeId): array
127
    {
128
        $response = $this->client->get(sprintf('%s/%s/volumes', $this->getBaseUri(), $linodeId));
129
        $contents = $response->getBody()->getContents();
130
        $json     = json_decode($contents, true);
131
132
        return array_map(fn ($data) => new Volume($this->client, $data), $json['data']);
133
    }
134
135
    protected function getBaseUri(): string
136
    {
137
        return '/linode/instances';
138
    }
139
140
    protected function getSupportedFields(): array
141
    {
142
        return [
143
            Linode::FIELD_ID,
144
            Linode::FIELD_LABEL,
145
            Linode::FIELD_GROUP,
146
            Linode::FIELD_TAGS,
147
            Linode::FIELD_REGION,
148
            Linode::FIELD_TYPE,
149
            Linode::FIELD_IMAGE,
150
            Linode::FIELD_STATUS,
151
            Linode::FIELD_HYPERVISOR,
152
            Linode::FIELD_CREATED,
153
            Linode::FIELD_UPDATED,
154
            Linode::FIELD_WATCHDOG_ENABLED,
155
            Linode::FIELD_IPV4,
156
            Linode::FIELD_IPV6,
157
            Linode::FIELD_SPECS,
158
            Linode::FIELD_ALERTS,
159
            Linode::FIELD_BACKUPS,
160
        ];
161
    }
162
163
    protected function jsonToEntity(array $json): Entity
164
    {
165
        return new Linode($this->client, $json);
166
    }
167
}
168