These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Gaufrette\Adapter; |
||
4 | |||
5 | use AmazonS3 as AmazonClient; |
||
6 | use Gaufrette\Adapter; |
||
7 | |||
8 | @trigger_error('The '.__NAMESPACE__.'\AmazonS3 adapter is deprecated since version 0.4 and will be removed in 1.0. Use the AwsS3 adapter instead.', E_USER_DEPRECATED); |
||
0 ignored issues
–
show
|
|||
9 | |||
10 | /** |
||
11 | * Amazon S3 adapter using the AWS SDK for PHP v1.x. |
||
12 | * |
||
13 | * See the AwsS3 adapter for using the AWS SDK for PHP v2.x. |
||
14 | * |
||
15 | * @author Antoine Hérault <[email protected]> |
||
16 | * @author Leszek Prabucki <[email protected]> |
||
17 | * |
||
18 | * @deprecated The AmazonS3 adapter is deprecated since version 0.4 and will be removed in 1.0. Use the AwsS3 adapter instead. |
||
19 | */ |
||
20 | class AmazonS3 implements Adapter, |
||
21 | MetadataSupporter |
||
22 | { |
||
23 | protected $service; |
||
24 | protected $bucket; |
||
25 | protected $ensureBucket = false; |
||
26 | protected $metadata; |
||
27 | protected $options; |
||
28 | |||
29 | View Code Duplication | public function __construct(AmazonClient $service, $bucket, $options = array()) |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
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.
Loading history...
|
|||
30 | { |
||
31 | $this->service = $service; |
||
32 | $this->bucket = $bucket; |
||
33 | $this->options = array_replace_recursive( |
||
34 | array('directory' => '', 'create' => false, 'region' => $service->hostname, 'acl' => AmazonClient::ACL_PUBLIC), |
||
35 | $options |
||
36 | ); |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * Set the acl used when writing files. |
||
41 | * |
||
42 | * @param string $acl |
||
43 | */ |
||
44 | public function setAcl($acl) |
||
45 | { |
||
46 | $this->options['acl'] = $acl; |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * Get the acl used when writing files. |
||
51 | * |
||
52 | * @return string |
||
53 | */ |
||
54 | public function getAcl() |
||
55 | { |
||
56 | return $this->options['acl']; |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Set the base directory the user will have access to. |
||
61 | * |
||
62 | * @param string $directory |
||
63 | */ |
||
64 | public function setDirectory($directory) |
||
65 | { |
||
66 | $this->options['directory'] = $directory; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Get the directory the user has access to. |
||
71 | * |
||
72 | * @return string |
||
73 | */ |
||
74 | public function getDirectory() |
||
75 | { |
||
76 | return $this->options['directory']; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * {@inheritdoc} |
||
81 | */ |
||
82 | public function setMetadata($key, $metadata) |
||
83 | { |
||
84 | $path = $this->computePath($key); |
||
85 | |||
86 | $this->metadata[$path] = $metadata; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * {@inheritdoc} |
||
91 | */ |
||
92 | public function getMetadata($key) |
||
93 | { |
||
94 | $path = $this->computePath($key); |
||
95 | |||
96 | return isset($this->metadata[$path]) ? $this->metadata[$path] : array(); |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * {@inheritdoc} |
||
101 | */ |
||
102 | View Code Duplication | public function read($key) |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
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.
Loading history...
|
|||
103 | { |
||
104 | $this->ensureBucketExists(); |
||
105 | |||
106 | $response = $this->service->get_object( |
||
107 | $this->bucket, |
||
108 | $this->computePath($key), |
||
109 | $this->getMetadata($key) |
||
110 | ); |
||
111 | |||
112 | if (!$response->isOK()) { |
||
113 | return false; |
||
114 | } |
||
115 | |||
116 | return $response->body; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * {@inheritdoc} |
||
121 | */ |
||
122 | public function rename($sourceKey, $targetKey) |
||
123 | { |
||
124 | $this->ensureBucketExists(); |
||
125 | |||
126 | $response = $this->service->copy_object( |
||
127 | array( // source |
||
128 | 'bucket' => $this->bucket, |
||
129 | 'filename' => $this->computePath($sourceKey), |
||
130 | ), |
||
131 | array( // target |
||
132 | 'bucket' => $this->bucket, |
||
133 | 'filename' => $this->computePath($targetKey), |
||
134 | ), |
||
135 | $this->getMetadata($sourceKey) |
||
136 | ); |
||
137 | |||
138 | return $response->isOK() && $this->delete($sourceKey); |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * {@inheritdoc} |
||
143 | */ |
||
144 | public function write($key, $content) |
||
145 | { |
||
146 | $this->ensureBucketExists(); |
||
147 | |||
148 | $opt = array_replace_recursive( |
||
149 | array('acl' => $this->options['acl']), |
||
150 | $this->getMetadata($key), |
||
151 | array('body' => $content) |
||
152 | ); |
||
153 | |||
154 | $response = $this->service->create_object( |
||
155 | $this->bucket, |
||
156 | $this->computePath($key), |
||
157 | $opt |
||
158 | ); |
||
159 | |||
160 | if (!$response->isOK()) { |
||
161 | return false; |
||
162 | }; |
||
163 | |||
164 | return intval($response->header['x-aws-requestheaders']['Content-Length']); |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * {@inheritdoc} |
||
169 | */ |
||
170 | public function exists($key) |
||
171 | { |
||
172 | $this->ensureBucketExists(); |
||
173 | |||
174 | return $this->service->if_object_exists( |
||
175 | $this->bucket, |
||
176 | $this->computePath($key) |
||
177 | ); |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * {@inheritdoc} |
||
182 | */ |
||
183 | public function mtime($key) |
||
184 | { |
||
185 | $this->ensureBucketExists(); |
||
186 | |||
187 | $response = $this->service->get_object_metadata( |
||
188 | $this->bucket, |
||
189 | $this->computePath($key), |
||
190 | $this->getMetadata($key) |
||
191 | ); |
||
192 | |||
193 | return isset($response['Headers']['last-modified']) ? strtotime($response['Headers']['last-modified']) : false; |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * {@inheritdoc} |
||
198 | */ |
||
199 | public function keys() |
||
200 | { |
||
201 | $this->ensureBucketExists(); |
||
202 | |||
203 | $list = $this->service->get_object_list($this->bucket); |
||
204 | |||
205 | $keys = array(); |
||
206 | foreach ($list as $file) { |
||
207 | if ('.' !== $dirname = \Gaufrette\Util\Path::dirname($file)) { |
||
208 | $keys[] = $dirname; |
||
209 | } |
||
210 | $keys[] = $file; |
||
211 | } |
||
212 | sort($keys); |
||
213 | |||
214 | return $keys; |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * {@inheritdoc} |
||
219 | */ |
||
220 | View Code Duplication | public function delete($key) |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
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.
Loading history...
|
|||
221 | { |
||
222 | $this->ensureBucketExists(); |
||
223 | |||
224 | $response = $this->service->delete_object( |
||
225 | $this->bucket, |
||
226 | $this->computePath($key), |
||
227 | $this->getMetadata($key) |
||
228 | ); |
||
229 | |||
230 | return $response->isOK(); |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * {@inheritdoc} |
||
235 | */ |
||
236 | public function isDirectory($key) |
||
237 | { |
||
238 | if ($this->exists($key.'/')) { |
||
239 | return true; |
||
240 | } |
||
241 | |||
242 | return false; |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * Ensures the specified bucket exists. If the bucket does not exists |
||
247 | * and the create parameter is set to true, it will try to create the |
||
248 | * bucket. |
||
249 | * |
||
250 | * @throws \RuntimeException if the bucket does not exists or could not be |
||
251 | * created |
||
252 | */ |
||
253 | private function ensureBucketExists() |
||
254 | { |
||
255 | if ($this->ensureBucket) { |
||
256 | return; |
||
257 | } |
||
258 | |||
259 | if (isset($this->options['region'])) { |
||
260 | $this->service->set_region($this->options['region']); |
||
261 | } |
||
262 | |||
263 | if ($this->service->if_bucket_exists($this->bucket)) { |
||
264 | $this->ensureBucket = true; |
||
265 | |||
266 | return; |
||
267 | } |
||
268 | |||
269 | if (!$this->options['create']) { |
||
270 | throw new \RuntimeException(sprintf( |
||
271 | 'The configured bucket "%s" does not exist.', |
||
272 | $this->bucket |
||
273 | )); |
||
274 | } |
||
275 | |||
276 | $response = $this->service->create_bucket( |
||
277 | $this->bucket, |
||
278 | $this->options['region'] |
||
279 | ); |
||
280 | |||
281 | if (!$response->isOK()) { |
||
282 | throw new \RuntimeException(sprintf( |
||
283 | 'Failed to create the configured bucket "%s".', |
||
284 | $this->bucket |
||
285 | )); |
||
286 | } |
||
287 | |||
288 | $this->ensureBucket = true; |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * Computes the path for the specified key taking the bucket in account. |
||
293 | * |
||
294 | * @param string $key The key for which to compute the path |
||
295 | * |
||
296 | * @return string |
||
297 | */ |
||
298 | private function computePath($key) |
||
299 | { |
||
300 | $directory = $this->getDirectory(); |
||
301 | if (null === $directory || '' === $directory) { |
||
302 | return $key; |
||
303 | } |
||
304 | |||
305 | return sprintf('%s/%s', $directory, $key); |
||
306 | } |
||
307 | } |
||
308 |
If you suppress an error, we recommend checking for the error condition explicitly: