Passed
Push — master ( 851c5d...3501a4 )
by Artem
01:43
created

ObjectStorageBucketRepository   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 25
c 1
b 0
f 0
dl 0
loc 71
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A cancelObjectStorage() 0 3 1
A getBaseUri() 0 3 1
A createObjectStorageBucket() 0 7 1
A createObjectStorageObjectURL() 0 7 1
A __construct() 0 3 1
A jsonToEntity() 0 3 1
A deleteObjectStorageBucket() 0 3 1
A modifyObjectStorageBucketAccess() 0 3 1
A getSupportedFields() 0 8 1
A getObjectStorageBucketContent() 0 7 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\ObjectStorage\Repository;
13
14
use Linode\Entity;
15
use Linode\Internal\AbstractRepository;
16
use Linode\LinodeClient;
17
use Linode\ObjectStorage\ObjectStorageBucket;
18
use Linode\ObjectStorage\ObjectStorageBucketRepositoryInterface;
19
use Linode\ObjectStorage\ObjectStorageObject;
20
21
/**
22
 * @codeCoverageIgnore This class was autogenerated.
23
 */
24
class ObjectStorageBucketRepository extends AbstractRepository implements ObjectStorageBucketRepositoryInterface
25
{
26
    /**
27
     * @param string $clusterId The ID of the cluster this bucket exists in.
28
     */
29
    public function __construct(LinodeClient $client, protected string $clusterId)
30
    {
31
        parent::__construct($client);
32
    }
33
34
    public function createObjectStorageBucket(array $parameters = []): ObjectStorageBucket
35
    {
36
        $response = $this->client->post('/object-storage/buckets', $parameters);
37
        $contents = $response->getBody()->getContents();
38
        $json     = json_decode($contents, true);
39
40
        return new ObjectStorageBucket($this->client, $json);
41
    }
42
43
    public function deleteObjectStorageBucket(string $bucket): void
44
    {
45
        $this->client->delete(sprintf('%s/%s', $this->getBaseUri(), $bucket));
46
    }
47
48
    public function modifyObjectStorageBucketAccess(string $bucket, array $parameters = []): void
49
    {
50
        $this->client->post(sprintf('%s/%s/access', $this->getBaseUri(), $bucket), $parameters);
51
    }
52
53
    public function getObjectStorageBucketContent(string $bucket): array
54
    {
55
        $response = $this->client->get(sprintf('%s/%s/object-list', $this->getBaseUri(), $bucket));
56
        $contents = $response->getBody()->getContents();
57
        $json     = json_decode($contents, true);
58
59
        return array_map(fn ($data) => new ObjectStorageObject($this->client, $data), $json['data']);
60
    }
61
62
    public function createObjectStorageObjectURL(string $bucket, array $parameters = []): string
63
    {
64
        $response = $this->client->post(sprintf('%s/%s/object-url', $this->getBaseUri(), $bucket), $parameters);
65
        $contents = $response->getBody()->getContents();
66
        $json     = json_decode($contents, true);
67
68
        return $json['url'];
69
    }
70
71
    public function cancelObjectStorage(): void
72
    {
73
        $this->client->post('/object-storage/cancel');
74
    }
75
76
    protected function getBaseUri(): string
77
    {
78
        return sprintf('/object-storage/buckets/%s', $this->clusterId);
79
    }
80
81
    protected function getSupportedFields(): array
82
    {
83
        return [
84
            ObjectStorageBucket::FIELD_CREATED,
85
            ObjectStorageBucket::FIELD_CLUSTER,
86
            ObjectStorageBucket::FIELD_LABEL,
87
            ObjectStorageBucket::FIELD_HOSTNAME,
88
            ObjectStorageBucket::FIELD_SIZE,
89
        ];
90
    }
91
92
    protected function jsonToEntity(array $json): Entity
93
    {
94
        return new ObjectStorageBucket($this->client, $json);
95
    }
96
}
97