1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OpenStack\ObjectStore\v1\Models; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\ResponseInterface; |
6
|
|
|
use OpenStack\Common\Error\BadResponseError; |
7
|
|
|
use OpenStack\Common\Resource\AbstractResource; |
8
|
|
|
use OpenStack\Common\Resource\Creatable; |
9
|
|
|
use OpenStack\Common\Resource\Deletable; |
10
|
|
|
use OpenStack\Common\Resource\HasMetadata; |
11
|
|
|
use OpenStack\Common\Resource\Listable; |
12
|
|
|
use OpenStack\Common\Resource\Retrievable; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @property \OpenStack\ObjectStore\v1\Api $api |
16
|
|
|
*/ |
17
|
|
|
class Container extends AbstractResource implements Creatable, Deletable, Retrievable, Listable, HasMetadata |
18
|
|
|
{ |
19
|
|
|
use MetadataTrait; |
20
|
|
|
|
21
|
|
|
const METADATA_PREFIX = 'X-Container-Meta-'; |
22
|
|
|
|
23
|
|
|
/** @var int */ |
24
|
|
|
public $objectCount; |
25
|
|
|
|
26
|
|
|
/** @var int */ |
27
|
|
|
public $bytesUsed; |
28
|
|
|
|
29
|
|
|
/** @var string */ |
30
|
|
|
public $name; |
31
|
|
|
|
32
|
|
|
/** @var array */ |
33
|
|
|
public $metadata; |
34
|
|
|
|
35
|
|
|
protected $markerKey = 'name'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
4 |
|
public function populateFromResponse(ResponseInterface $response) |
41
|
|
|
{ |
42
|
4 |
|
parent::populateFromResponse($response); |
43
|
|
|
|
44
|
4 |
|
$this->objectCount = $response->getHeaderLine('X-Container-Object-Count'); |
|
|
|
|
45
|
4 |
|
$this->bytesUsed = $response->getHeaderLine('X-Container-Bytes-Used'); |
|
|
|
|
46
|
4 |
|
$this->metadata = $this->parseMetadata($response); |
47
|
4 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Retrieves a collection of object resources in the form of a generator. |
51
|
|
|
* |
52
|
|
|
* @param array $options {@see \OpenStack\ObjectStore\v1\Api::getContainer} |
53
|
|
|
* @param callable|null $mapFn Allows a function to be mapped over each element. |
54
|
|
|
* |
55
|
|
|
* @return \Generator |
56
|
|
|
*/ |
57
|
1 |
|
public function listObjects(array $options = [], callable $mapFn = null) |
58
|
|
|
{ |
59
|
1 |
|
$options = array_merge($options, ['name' => $this->name, 'format' => 'json']); |
60
|
1 |
|
return $this->model(Object::class)->enumerate($this->api->getContainer(), $options, $mapFn); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
1 |
|
public function retrieve() |
67
|
|
|
{ |
68
|
1 |
|
$response = $this->executeWithState($this->api->headContainer()); |
69
|
1 |
|
$this->populateFromResponse($response); |
|
|
|
|
70
|
1 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param array $data {@see \OpenStack\ObjectStore\v1\Api::putContainer} |
74
|
|
|
* |
75
|
|
|
* @return $this |
76
|
|
|
*/ |
77
|
2 |
|
public function create(array $data) |
78
|
|
|
{ |
79
|
2 |
|
$response = $this->execute($this->api->putContainer(), $data); |
80
|
|
|
|
81
|
2 |
|
$this->populateFromResponse($response); |
|
|
|
|
82
|
2 |
|
$this->name = $data['name']; |
83
|
|
|
|
84
|
2 |
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
1 |
|
public function delete() |
91
|
|
|
{ |
92
|
1 |
|
$this->executeWithState($this->api->deleteContainer()); |
93
|
1 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritdoc} |
97
|
|
|
*/ |
98
|
1 |
|
public function mergeMetadata(array $metadata) |
99
|
|
|
{ |
100
|
1 |
|
$response = $this->execute($this->api->postContainer(), ['name' => $this->name, 'metadata' => $metadata]); |
101
|
1 |
|
return $this->parseMetadata($response); |
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritdoc} |
106
|
|
|
*/ |
107
|
1 |
View Code Duplication |
public function resetMetadata(array $metadata) |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
$options = [ |
110
|
1 |
|
'name' => $this->name, |
111
|
1 |
|
'removeMetadata' => [], |
112
|
1 |
|
'metadata' => $metadata, |
113
|
1 |
|
]; |
114
|
|
|
|
115
|
1 |
|
foreach ($this->getMetadata() as $key => $val) { |
116
|
1 |
|
if (!array_key_exists($key, $metadata)) { |
117
|
1 |
|
$options['removeMetadata'][$key] = 'True'; |
118
|
1 |
|
} |
119
|
1 |
|
} |
120
|
|
|
|
121
|
1 |
|
$response = $this->execute($this->api->postContainer(), $options); |
122
|
1 |
|
return $this->parseMetadata($response); |
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* {@inheritdoc} |
127
|
|
|
*/ |
128
|
2 |
|
public function getMetadata() |
129
|
|
|
{ |
130
|
2 |
|
$response = $this->executeWithState($this->api->headContainer()); |
131
|
2 |
|
return $this->parseMetadata($response); |
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Retrieves an Object and populates its `name` and `containerName` properties according to the name provided and |
136
|
|
|
* the name of this container. A HTTP call will not be executed by default - you need to call |
137
|
|
|
* {@see Object::retrieve} or {@see Object::download} on the returned Object object to do that. |
138
|
|
|
* |
139
|
|
|
* @param string $name The name of the object |
140
|
|
|
* |
141
|
|
|
* @return Object |
142
|
|
|
*/ |
143
|
4 |
|
public function getObject($name) |
144
|
|
|
{ |
145
|
4 |
|
return $this->model(Object::class, ['containerName' => $this->name, 'name' => $name]); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Identifies whether an object exists in this container. |
150
|
|
|
* |
151
|
|
|
* @param string $name The name of the object. |
152
|
|
|
* |
153
|
|
|
* @return bool TRUE if the object exists, FALSE if it does not. |
154
|
|
|
* |
155
|
|
|
* @throws BadResponseError For any other HTTP error which does not have a 404 Not Found status. |
156
|
|
|
* @throws \Exception For any other type of fatal error. |
157
|
|
|
*/ |
158
|
3 |
|
public function objectExists($name) |
159
|
|
|
{ |
160
|
|
|
try { |
161
|
3 |
|
$this->getObject($name)->retrieve(); |
|
|
|
|
162
|
1 |
|
return true; |
163
|
2 |
|
} catch (BadResponseError $e) { |
164
|
2 |
|
if ($e->getResponse()->getStatusCode() === 404) { |
165
|
1 |
|
return false; |
166
|
|
|
} |
167
|
1 |
|
throw $e; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Creates a single object according to the values provided. |
173
|
|
|
* |
174
|
|
|
* @param array $data {@see \OpenStack\ObjectStore\v1\Api::putObject} |
175
|
|
|
* |
176
|
|
|
* @return Object |
177
|
|
|
*/ |
178
|
1 |
|
public function createObject(array $data) |
179
|
|
|
{ |
180
|
1 |
|
return $this->model(Object::class)->create($data + ['containerName' => $this->name]); |
|
|
|
|
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.