Passed
Push — master ( bfa63a...41a424 )
by Artem
01:47
created

updateObjectStorageBucketAccess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 2
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\ObjectStorage\Repository;
13
14
use Linode\Entity;
15
use Linode\Internal\AbstractRepository;
16
use Linode\LinodeClient;
17
use Linode\ObjectStorage\ObjectACL;
18
use Linode\ObjectStorage\ObjectStorageBucket;
19
use Linode\ObjectStorage\ObjectStorageBucketRepositoryInterface;
20
use Linode\ObjectStorage\ObjectStorageObject;
21
22
/**
23
 * @codeCoverageIgnore This class was autogenerated.
24
 */
25
class ObjectStorageBucketRepository extends AbstractRepository implements ObjectStorageBucketRepositoryInterface
26
{
27
    /**
28
     * @param string $clusterId The ID of the cluster this bucket exists in.
29
     */
30
    public function __construct(LinodeClient $client, protected string $clusterId)
31
    {
32
        parent::__construct($client);
33
    }
34
35
    public function createObjectStorageBucket(array $parameters = []): ObjectStorageBucket
36
    {
37
        $response = $this->client->post('/object-storage/buckets', $parameters);
38
        $contents = $response->getBody()->getContents();
39
        $json     = json_decode($contents, true);
40
41
        return new ObjectStorageBucket($this->client, $json);
42
    }
43
44
    public function deleteObjectStorageBucket(string $bucket): void
45
    {
46
        $this->client->delete(sprintf('%s/%s', $this->getBaseUri(), $bucket));
47
    }
48
49
    public function modifyObjectStorageBucketAccess(string $bucket, array $parameters = []): void
50
    {
51
        $this->client->post(sprintf('%s/%s/access', $this->getBaseUri(), $bucket), $parameters);
52
    }
53
54
    public function updateObjectStorageBucketAccess(string $bucket, array $parameters = []): void
55
    {
56
        $this->client->put(sprintf('%s/%s/access', $this->getBaseUri(), $bucket), $parameters);
57
    }
58
59
    public function viewObjectStorageBucketACL(string $bucket): ObjectACL
60
    {
61
        $response = $this->client->get(sprintf('%s/%s/object-acl', $this->getBaseUri(), $bucket));
62
        $contents = $response->getBody()->getContents();
63
        $json     = json_decode($contents, true);
64
65
        return new ObjectACL($this->client, $json);
66
    }
67
68
    public function updateObjectStorageBucketACL(string $bucket, array $parameters = []): ObjectACL
69
    {
70
        $response = $this->client->put(sprintf('%s/%s/object-acl', $this->getBaseUri(), $bucket), $parameters);
71
        $contents = $response->getBody()->getContents();
72
        $json     = json_decode($contents, true);
73
74
        return new ObjectACL($this->client, $json);
75
    }
76
77
    public function getObjectStorageBucketContent(string $bucket): array
78
    {
79
        $response = $this->client->get(sprintf('%s/%s/object-list', $this->getBaseUri(), $bucket));
80
        $contents = $response->getBody()->getContents();
81
        $json     = json_decode($contents, true);
82
83
        return array_map(fn ($data) => new ObjectStorageObject($this->client, $data), $json['data']);
84
    }
85
86
    public function createObjectStorageObjectURL(string $bucket, array $parameters = []): string
87
    {
88
        $response = $this->client->post(sprintf('%s/%s/object-url', $this->getBaseUri(), $bucket), $parameters);
89
        $contents = $response->getBody()->getContents();
90
        $json     = json_decode($contents, true);
91
92
        return $json['url'];
93
    }
94
95
    public function getObjectStorageSSL(string $bucket): bool
96
    {
97
        $response = $this->client->get(sprintf('%s/%s/ssl', $this->getBaseUri(), $bucket));
98
        $contents = $response->getBody()->getContents();
99
        $json     = json_decode($contents, true);
100
101
        return $json['ssl'];
102
    }
103
104
    public function createObjectStorageSSL(string $bucket, array $parameters = []): bool
105
    {
106
        $response = $this->client->post(sprintf('%s/%s/ssl', $this->getBaseUri(), $bucket), $parameters);
107
        $contents = $response->getBody()->getContents();
108
        $json     = json_decode($contents, true);
109
110
        return $json['ssl'];
111
    }
112
113
    public function deleteObjectStorageSSL(string $bucket): void
114
    {
115
        $this->client->delete(sprintf('%s/%s/ssl', $this->getBaseUri(), $bucket));
116
    }
117
118
    public function getObjectStorageTransfer(): int
119
    {
120
        $response = $this->client->get('/object-storage/transfer');
121
        $contents = $response->getBody()->getContents();
122
        $json     = json_decode($contents, true);
123
124
        return $json['used'];
125
    }
126
127
    public function cancelObjectStorage(): void
128
    {
129
        $this->client->post('/object-storage/cancel');
130
    }
131
132
    protected function getBaseUri(): string
133
    {
134
        return sprintf('/object-storage/buckets/%s', $this->clusterId);
135
    }
136
137
    protected function getSupportedFields(): array
138
    {
139
        return [
140
            ObjectStorageBucket::FIELD_CREATED,
141
            ObjectStorageBucket::FIELD_CLUSTER,
142
            ObjectStorageBucket::FIELD_LABEL,
143
            ObjectStorageBucket::FIELD_HOSTNAME,
144
            ObjectStorageBucket::FIELD_SIZE,
145
            ObjectStorageBucket::FIELD_OBJECTS,
146
        ];
147
    }
148
149
    protected function jsonToEntity(array $json): Entity
150
    {
151
        return new ObjectStorageBucket($this->client, $json);
152
    }
153
}
154