1
|
|
|
<?php declare (strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace OpenStack\ObjectStore\v1\Models; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Psr7\Uri; |
6
|
|
|
use OpenStack\Common\Transport\Utils; |
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
use Psr\Http\Message\StreamInterface; |
9
|
|
|
use OpenStack\Common\Resource\OperatorResource; |
10
|
|
|
use OpenStack\Common\Resource\Creatable; |
11
|
|
|
use OpenStack\Common\Resource\Deletable; |
12
|
|
|
use OpenStack\Common\Resource\HasMetadata; |
13
|
|
|
use OpenStack\Common\Resource\ResourceInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @property \OpenStack\ObjectStore\v1\Api $api |
17
|
|
|
*/ |
18
|
|
|
class ObjectEntity extends OperatorResource implements Creatable, Deletable, HasMetadata |
19
|
|
|
{ |
20
|
|
|
use MetadataTrait; |
21
|
|
|
|
22
|
|
|
const METADATA_PREFIX = 'X-Object-Meta-'; |
23
|
|
|
|
24
|
|
|
/** @var string */ |
25
|
|
|
public $containerName; |
26
|
|
|
|
27
|
|
|
/** @var string */ |
28
|
|
|
public $name; |
29
|
|
|
|
30
|
|
|
/** @var string */ |
31
|
|
|
public $hash; |
32
|
|
|
|
33
|
|
|
/** @var string */ |
34
|
|
|
public $contentType; |
35
|
|
|
|
36
|
|
|
/** @var int */ |
37
|
|
|
public $contentLength; |
38
|
|
|
|
39
|
|
|
/** @var string */ |
40
|
|
|
public $lastModified; |
41
|
|
|
|
42
|
|
|
/** @var array */ |
43
|
|
|
public $metadata; |
44
|
|
|
|
45
|
|
|
protected $markerKey = 'name'; |
46
|
|
|
protected $aliases = ['bytes' => 'contentLength']; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
|
|
public function populateFromResponse(ResponseInterface $response): ResourceInterface |
52
|
|
|
{ |
53
|
|
|
parent::populateFromResponse($response); |
54
|
|
|
|
55
|
|
|
$this->populateHeaders($response); |
56
|
|
|
|
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param ResponseInterface $response |
62
|
|
|
* |
63
|
|
|
* @return $this |
64
|
|
|
*/ |
65
|
|
View Code Duplication |
private function populateHeaders(ResponseInterface $response): self |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
$this->hash = $response->getHeaderLine('ETag'); |
68
|
|
|
$this->contentLength = $response->getHeaderLine('Content-Length'); |
|
|
|
|
69
|
|
|
$this->lastModified = $response->getHeaderLine('Last-Modified'); |
70
|
|
|
$this->contentType = $response->getHeaderLine('Content-Type'); |
71
|
|
|
$this->metadata = $this->parseMetadata($response); |
72
|
|
|
|
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Retrieves the public URI for this resource. |
78
|
|
|
* |
79
|
|
|
* @return \GuzzleHttp\Psr7\Uri |
80
|
|
|
*/ |
81
|
|
|
public function getPublicUri(): Uri |
82
|
|
|
{ |
83
|
|
|
return Utils::addPaths($this->getHttpBaseUrl(), $this->containerName, $this->name); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param array $data {@see \OpenStack\ObjectStore\v1\Api::putObject} |
88
|
|
|
* |
89
|
|
|
* @return $this |
90
|
|
|
*/ |
91
|
|
|
public function create(array $data): Creatable |
92
|
|
|
{ |
93
|
|
|
$response = $this->execute($this->api->putObject(), $data + ['containerName' => $this->containerName]); |
94
|
|
|
return $this->populateFromResponse($response); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@inheritdoc} |
99
|
|
|
*/ |
100
|
|
|
public function retrieve() |
101
|
|
|
{ |
102
|
|
|
$response = $this->executeWithState($this->api->headObject()); |
103
|
|
|
$this->populateFromResponse($response); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* This call will perform a `GET` HTTP request for the given object and return back its content in the form of a |
108
|
|
|
* Guzzle Stream object. Downloading an object will transfer all of the content for an object, and is therefore |
109
|
|
|
* distinct from fetching its metadata (a `HEAD` request). The body of an object is not fetched by default to |
110
|
|
|
* improve performance when handling large objects. |
111
|
|
|
* |
112
|
|
|
* @param array $data {@see \OpenStack\ObjectStore\v1\Api::getObject} |
113
|
|
|
* |
114
|
|
|
* @return StreamInterface |
115
|
|
|
*/ |
116
|
|
|
|
117
|
|
|
public function download(array $data = []): StreamInterface |
118
|
|
|
{ |
119
|
|
|
$data += ['name' => $this->name, 'containerName' => $this->containerName]; |
120
|
|
|
|
121
|
|
|
/** @var ResponseInterface $response */ |
122
|
|
|
$response = $this->execute($this->api->getObject(), $data); |
123
|
|
|
$this->populateHeaders($response); |
124
|
|
|
|
125
|
|
|
return $response->getBody(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* {@inheritdoc} |
130
|
|
|
*/ |
131
|
|
|
public function delete() |
132
|
|
|
{ |
133
|
|
|
$this->executeWithState($this->api->deleteObject()); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param array $options {@see \OpenStack\ObjectStore\v1\Api::copyObject} |
138
|
|
|
*/ |
139
|
|
|
public function copy(array $options) |
140
|
|
|
{ |
141
|
|
|
$options += ['name' => $this->name, 'containerName' => $this->containerName]; |
142
|
|
|
$this->execute($this->api->copyObject(), $options); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* {@inheritdoc} |
147
|
|
|
*/ |
148
|
|
View Code Duplication |
public function mergeMetadata(array $metadata) |
|
|
|
|
149
|
|
|
{ |
150
|
|
|
$options = [ |
151
|
|
|
'containerName' => $this->containerName, |
152
|
|
|
'name' => $this->name, |
153
|
|
|
'metadata' => array_merge($metadata, $this->getMetadata()), |
154
|
|
|
]; |
155
|
|
|
|
156
|
|
|
$response = $this->execute($this->api->postObject(), $options); |
157
|
|
|
$this->metadata = $this->parseMetadata($response); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* {@inheritdoc} |
162
|
|
|
*/ |
163
|
|
View Code Duplication |
public function resetMetadata(array $metadata) |
|
|
|
|
164
|
|
|
{ |
165
|
|
|
$options = [ |
166
|
|
|
'containerName' => $this->containerName, |
167
|
|
|
'name' => $this->name, |
168
|
|
|
'metadata' => $metadata, |
169
|
|
|
]; |
170
|
|
|
|
171
|
|
|
$response = $this->execute($this->api->postObject(), $options); |
172
|
|
|
$this->metadata = $this->parseMetadata($response); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* {@inheritdoc} |
177
|
|
|
*/ |
178
|
|
|
public function getMetadata(): array |
179
|
|
|
{ |
180
|
|
|
$response = $this->executeWithState($this->api->headObject()); |
181
|
|
|
return $this->parseMetadata($response); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.