|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ArgentCrusade\Selectel\CloudStorage; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
use ArgentCrusade\Selectel\CloudStorage\Collections\Collection; |
|
7
|
|
|
use ArgentCrusade\Selectel\CloudStorage\Contracts\CloudStorageContract; |
|
8
|
|
|
use ArgentCrusade\Selectel\CloudStorage\Contracts\Api\ApiClientContract; |
|
9
|
|
|
use ArgentCrusade\Selectel\CloudStorage\Exceptions\ApiRequestFailedException; |
|
10
|
|
|
|
|
11
|
|
|
class CloudStorage implements CloudStorageContract |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* API Client instance. |
|
15
|
|
|
* |
|
16
|
|
|
* @var \ArgentCrusade\Selectel\CloudStorage\Contracts\Api\ApiClientContract |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $api; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* File uploader. |
|
22
|
|
|
* |
|
23
|
|
|
* @var \ArgentCrusade\Selectel\CloudStorage\FileUploader |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $uploader; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Creates new instance. |
|
29
|
|
|
* |
|
30
|
|
|
* @param \ArgentCrusade\Selectel\CloudStorage\Contracts\Api\ApiClientContract $api |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct(ApiClientContract $api) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->api = $api; |
|
35
|
|
|
$this->uploader = new FileUploader(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Available containers. |
|
40
|
|
|
* |
|
41
|
|
|
* @param int $limit = 10000 |
|
42
|
|
|
* @param string $marker = '' |
|
43
|
|
|
* |
|
44
|
|
|
* @throws \ArgentCrusade\Selectel\CloudStorage\Exceptions\ApiRequestFailedException |
|
45
|
|
|
* |
|
46
|
|
|
* @return \ArgentCrusade\Selectel\CloudStorage\Contracts\Collections\CollectionContract |
|
47
|
|
|
*/ |
|
48
|
|
|
public function containers($limit = 10000, $marker = '') |
|
49
|
|
|
{ |
|
50
|
|
|
$response = $this->api->request('GET', '/', [ |
|
51
|
|
|
'query' => [ |
|
52
|
|
|
'limit' => intval($limit), |
|
53
|
|
|
'marker' => $marker, |
|
54
|
|
|
], |
|
55
|
|
|
]); |
|
56
|
|
|
|
|
57
|
|
|
if ($response->getStatusCode() !== 200) { |
|
58
|
|
|
throw new ApiRequestFailedException('Unable to list containers.', $response->getStatusCode()); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$containers = json_decode($response->getBody(), true); |
|
62
|
|
|
|
|
63
|
|
|
return new Collection($this->transformContainers($containers)); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Retrieves single container from cloud storage. |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $name |
|
70
|
|
|
* |
|
71
|
|
|
* @return \ArgentCrusade\Selectel\CloudStorage\Contracts\ContainerContract |
|
72
|
|
|
*/ |
|
73
|
|
|
public function getContainer($name) |
|
74
|
|
|
{ |
|
75
|
|
|
return new Container($this->api, $this->uploader, $name); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Creates new container. |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $name |
|
82
|
|
|
* @param string $type |
|
83
|
|
|
* |
|
84
|
|
|
* @throws \InvalidArgumentException |
|
85
|
|
|
* @throws \ArgentCrusade\Selectel\CloudStorage\Exceptions\ApiRequestFailedException |
|
86
|
|
|
* |
|
87
|
|
|
* @return \ArgentCrusade\Selectel\CloudStorage\Contracts\ContainerContract |
|
88
|
|
|
*/ |
|
89
|
|
|
public function createContainer($name, $type = 'public') |
|
90
|
|
|
{ |
|
91
|
|
|
if (!in_array($type, ['public', 'private', 'gallery'])) { |
|
92
|
|
|
throw new InvalidArgumentException('Unknown type "'.$type.'" provided.'); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$response = $this->api->request('PUT', '/'.trim($name, '/'), [ |
|
96
|
|
|
'headers' => [ |
|
97
|
|
|
'X-Container-Meta-Type' => $type, |
|
98
|
|
|
], |
|
99
|
|
|
]); |
|
100
|
|
|
|
|
101
|
|
|
switch ($response->getStatusCode()) { |
|
102
|
|
|
case 201: |
|
103
|
|
|
return $this->getContainer(trim($name, '/')); |
|
104
|
|
|
case 202: |
|
105
|
|
|
throw new ApiRequestFailedException('Container "'.$name.'" already exists.'); |
|
106
|
|
|
default: |
|
107
|
|
|
throw new ApiRequestFailedException('Unable to create container "'.$name.'".', $response->getStatusCode()); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Transforms containers response to Container objects. |
|
113
|
|
|
* |
|
114
|
|
|
* @param array $items |
|
115
|
|
|
* |
|
116
|
|
|
* @return array |
|
117
|
|
|
*/ |
|
118
|
|
|
protected function transformContainers(array $items) |
|
119
|
|
|
{ |
|
120
|
|
|
if (!count($items)) { |
|
121
|
|
|
return []; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
$containers = []; |
|
125
|
|
|
|
|
126
|
|
|
foreach ($items as $item) { |
|
127
|
|
|
$container = new Container($this->api, $this->uploader, $item['name'], $item); |
|
128
|
|
|
$containers[$container->name()] = $container; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
return $containers; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|