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__.'\AclAwareAmazonS3 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 | * Makes the AmazonS3 adapter ACL aware. Uses the AWS SDK for PHP v1.x. |
||
12 | * |
||
13 | * See the AwsS3 adapter for using the AWS SDK for PHP v2.x. There is |
||
14 | * no distinction in the AwsS3 adapter between an ACL aware adapter |
||
15 | * and regular adapter. |
||
16 | * |
||
17 | * @author Johannes M. Schmitt <[email protected]> |
||
18 | * |
||
19 | * @deprecated The AclAwareAmazonS3 adapter is deprecated since version 0.4 and will be removed in 1.0. Use the AwsS3 adapter instead. |
||
20 | */ |
||
21 | class AclAwareAmazonS3 implements Adapter, |
||
22 | MetadataSupporter |
||
23 | { |
||
24 | protected $delegate; |
||
25 | protected $s3; |
||
26 | protected $bucketName; |
||
27 | protected $aclConstant = AmazonClient::ACL_PRIVATE; |
||
28 | protected $users = array(); |
||
29 | |||
30 | public function __construct(Adapter $delegate, AmazonClient $s3, $bucketName) |
||
31 | { |
||
32 | $this->delegate = $delegate; |
||
33 | $this->s3 = $s3; |
||
34 | $this->bucketName = $bucketName; |
||
35 | } |
||
36 | |||
37 | public function setAclConstant($constant) |
||
38 | { |
||
39 | if (!defined($constant = 'AmazonS3::ACL_'.strtoupper($constant))) { |
||
40 | throw new \InvalidArgumentException(sprintf('The ACL constant "%s" does not exist on AmazonS3.', $constant)); |
||
41 | } |
||
42 | |||
43 | $this->aclConstant = constant($constant); |
||
44 | } |
||
45 | |||
46 | public function setUsers(array $users) |
||
47 | { |
||
48 | $this->users = array(); |
||
49 | |||
50 | foreach ($users as $user) { |
||
51 | if (!isset($user['permission'])) { |
||
52 | throw new \InvalidArgumentException(sprintf('setUsers() expects an array where each item contains a "permission" key, but got %s.', json_encode($user))); |
||
53 | } |
||
54 | |||
55 | if (!defined($constant = 'AmazonS3::GRANT_'.strtoupper($user['permission']))) { |
||
56 | throw new \InvalidArgumentException('The permission must be the suffix for one of the AmazonS3::GRANT_ constants.'); |
||
57 | } |
||
58 | $user['permission'] = constant($constant); |
||
59 | |||
60 | if (isset($user['group'])) { |
||
61 | if (!defined($constant = 'AmazonS3::USERS_'.strtoupper($user['group']))) { |
||
62 | throw new \InvalidArgumentException('The group must be the suffix for one of the AmazonS3::USERS_ constants.'); |
||
63 | } |
||
64 | $user['id'] = constant($constant); |
||
65 | unset($user['group']); |
||
66 | } elseif (!isset($user['id'])) { |
||
67 | throw new \InvalidArgumentException(sprintf('Either "group", or "id" must be set for each user, but got %s.', json_encode($user))); |
||
68 | } |
||
69 | |||
70 | $this->users[] = $user; |
||
71 | } |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * {@inheritdoc} |
||
76 | */ |
||
77 | public function read($key) |
||
78 | { |
||
79 | return $this->delegate->read($key); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * {@inheritdoc} |
||
84 | */ |
||
85 | View Code Duplication | public function rename($key, $new) |
|
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...
|
|||
86 | { |
||
87 | $rs = $this->delegate->rename($key, $new); |
||
88 | |||
89 | try { |
||
90 | $this->updateAcl($new); |
||
91 | |||
92 | return $rs; |
||
93 | } catch (\Exception $ex) { |
||
94 | $this->delete($new); |
||
95 | |||
96 | return false; |
||
97 | } |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * {@inheritdoc} |
||
102 | */ |
||
103 | View Code Duplication | public function write($key, $content) |
|
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...
|
|||
104 | { |
||
105 | $rs = $this->delegate->write($key, $content); |
||
106 | |||
107 | try { |
||
108 | $this->updateAcl($key); |
||
109 | |||
110 | return $rs; |
||
111 | } catch (\Exception $ex) { |
||
112 | $this->delete($key); |
||
113 | |||
114 | return false; |
||
115 | } |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * {@inheritdoc} |
||
120 | */ |
||
121 | public function exists($key) |
||
122 | { |
||
123 | return $this->delegate->exists($key); |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * {@inheritdoc} |
||
128 | */ |
||
129 | public function mtime($key) |
||
130 | { |
||
131 | return $this->delegate->mtime($key); |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * {@inheritdoc} |
||
136 | */ |
||
137 | public function keys() |
||
138 | { |
||
139 | return $this->delegate->keys(); |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * {@inheritdoc} |
||
144 | */ |
||
145 | public function delete($key) |
||
146 | { |
||
147 | return $this->delegate->delete($key); |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * {@inheritdoc} |
||
152 | */ |
||
153 | public function setMetadata($key, $metadata) |
||
154 | { |
||
155 | if ($this->delegate instanceof MetadataSupporter) { |
||
156 | $this->delegate->setMetadata($key, $metadata); |
||
157 | } |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * {@inheritdoc} |
||
162 | */ |
||
163 | public function getMetadata($key) |
||
164 | { |
||
165 | if ($this->delegate instanceof MetadataSupporter) { |
||
166 | return $this->delegate->getMetadata($key); |
||
167 | } |
||
168 | |||
169 | return array(); |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * {@inheritdoc} |
||
174 | */ |
||
175 | public function isDirectory($key) |
||
176 | { |
||
177 | return $this->delegate->isDirectory($key); |
||
178 | } |
||
179 | |||
180 | protected function getAcl() |
||
181 | { |
||
182 | if (empty($this->users)) { |
||
183 | return $this->aclConstant; |
||
184 | } |
||
185 | |||
186 | return $this->users; |
||
187 | } |
||
188 | |||
189 | private function updateAcl($key) |
||
190 | { |
||
191 | $response = $this->s3->set_object_acl($this->bucketName, $key, $this->getAcl()); |
||
192 | if (200 != $response->status) { |
||
193 | throw new \RuntimeException('S3-ACL change failed: '.print_r($response, true)); |
||
194 | } |
||
195 | } |
||
196 | } |
||
197 |
If you suppress an error, we recommend checking for the error condition explicitly: