PluginProbe
WP-Stateless – Google Cloud Storage / 3.0.3
WP-Stateless – Google Cloud Storage v3.0.3
4.4.3 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.3.0 2.3.1 2.3.2 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 All 62 releases
wp-stateless / lib / Google / vendor / google / cloud-storage / src / Lifecycle.php

Lifecycle.php in WP-Stateless – Google Cloud Storage 3.0.3, at lib/Google/vendor/google/cloud-storage/src/Lifecycle.php

303 lines 9.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Copyright 2018 Google LLC
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 namespace Google\Cloud\Storage;
19
20 /**
21 * Object Lifecycle Management supports common use cases like setting a Time to
22 * Live (TTL) for objects, archiving older versions of objects, or "downgrading"
23 * storage classes of objects to help manage costs.
24 *
25 * This builder does not execute any network requests and is intended to be used
26 * in combination with either
27 * {@see Google\Cloud\Storage\StorageClient::createBucket()}
28 * or {@see Google\Cloud\Storage\Bucket::update()}.
29 *
30 * Example:
31 * ```
32 * // Access a builder preconfigured with rules already existing on a given
33 * // bucket.
34 * use Google\Cloud\Storage\StorageClient;
35 *
36 * $storage = new StorageClient();
37 * $bucket = $storage->bucket('my-bucket');
38 * $lifecycle = $bucket->currentLifecycle();
39 * ```
40 *
41 * ```
42 * // Or get a fresh builder by using the static factory method.
43 * use Google\Cloud\Storage\Bucket;
44 *
45 * $lifecycle = Bucket::lifecycle();
46 * ```
47 *
48 * @see https://cloud.google.com/storage/docs/lifecycle Object Lifecycle Management API Documentation
49 */
50 class Lifecycle implements \ArrayAccess, \IteratorAggregate
51 {
52 /**
53 * @var array
54 */
55 private $lifecycle;
56
57 /**
58 * @param array $lifecycle [optional] A lifecycle configuration. Please see
59 * [here](https://cloud.google.com/storage/docs/json_api/v1/buckets#lifecycle)
60 * for the expected structure.
61 */
62 public function __construct(array $lifecycle = [])
63 {
64 $this->lifecycle = $lifecycle;
65 }
66
67 /**
68 * Adds an Object Lifecycle Delete Rule.
69 *
70 * Example:
71 * ```
72 * $lifecycle->addDeleteRule([
73 * 'age' => 50,
74 * 'isLive' => true
75 * ]);
76 * ```
77 *
78 * @param array $condition {
79 * The condition(s) where the rule will apply.
80 *
81 * @type int $age Age of an object (in days). This condition is
82 * satisfied when an object reaches the specified age.
83 * @type string $createdBefore A date in RFC 3339 format with only the
84 * date part (for instance, "2013-01-15"). This condition is
85 * satisfied when an object is created before midnight of the
86 * specified date in UTC.
87 * @type bool $isLive Relevant only for versioned objects. If the value
88 * is `true`, this condition matches live objects; if the value is
89 * `false`, it matches archived objects.
90 * @type string[] $matchesStorageClass Objects having any of the storage
91 * classes specified by this condition will be matched. Values
92 * include `"MULTI_REGIONAL"`, `"REGIONAL"`, `"NEARLINE"`,
93 * `"ARCHIVE"`, `"COLDLINE"`, `"STANDARD"`, and
94 * `"DURABLE_REDUCED_AVAILABILITY"`.
95 * @type int $numNewerVersions Relevant only for versioned objects. If
96 * the value is N, this condition is satisfied when there are at
97 * least N versions (including the live version) newer than this
98 * version of the object.
99 * }
100 * @return Lifecycle
101 */
102 public function addDeleteRule(array $condition)
103 {
104 $this->lifecycle['rule'][] = [
105 'action' => [
106 'type' => 'Delete'
107 ],
108 'condition' => $condition
109 ];
110
111 return $this;
112 }
113
114 /**
115 * Adds an Object Lifecycle Set Storage Class Rule.
116 *
117 * Example:
118 * ```
119 * $lifecycle->addSetStorageClassRule('COLDLINE', [
120 * 'age' => 50,
121 * 'isLive' => true
122 * ]);
123 * ```
124 *
125 * @param string $storageClass The target storage class. Values include
126 * `"MULTI_REGIONAL"`, `"REGIONAL"`, `"NEARLINE"`, `"COLDLINE"`,
127 * `"STANDARD"`, and `"DURABLE_REDUCED_AVAILABILITY"`.
128 * @param array $condition {
129 * The condition(s) where the rule will apply.
130 *
131 * @type int $age Age of an object (in days). This condition is
132 * satisfied when an object reaches the specified age.
133 * @type string $createdBefore A date in RFC 3339 format with only the
134 * date part (for instance, "2013-01-15"). This condition is
135 * satisfied when an object is created before midnight of the
136 * specified date in UTC.
137 * @type bool $isLive Relevant only for versioned objects. If the value
138 * is `true`, this condition matches live objects; if the value is
139 * `false`, it matches archived objects.
140 * @type string[] $matchesStorageClass Objects having any of the storage
141 * classes specified by this condition will be matched. Values
142 * include `"MULTI_REGIONAL"`, `"REGIONAL"`, `"NEARLINE"`,
143 * `"COLDLINE"`, `"STANDARD"`, and `"DURABLE_REDUCED_AVAILABILITY"`.
144 * @type int $numNewerVersions Relevant only for versioned objects. If
145 * the value is N, this condition is satisfied when there are at
146 * least N versions (including the live version) newer than this
147 * version of the object.
148 * }
149 * @return Lifecycle
150 */
151 public function addSetStorageClassRule($storageClass, array $condition)
152 {
153 $this->lifecycle['rule'][] = [
154 'action' => [
155 'type' => 'SetStorageClass',
156 'storageClass' => $storageClass
157 ],
158 'condition' => $condition
159 ];
160
161 return $this;
162 }
163
164 /**
165 * Clear all Object Lifecycle rules or rules of a certain action type.
166 *
167 * Example:
168 * ```
169 * // Remove all rules.
170 * $lifecycle->clearRules();
171 * ```
172 *
173 * ```
174 * // Remove all "Delete" based rules.
175 * $lifecycle->clearRules('Delete');
176 * ```
177 *
178 * ```
179 * // Clear any rules which have an age equal to 50.
180 * $lifecycle->clearRules(function (array $rule) {
181 * return $rule['condition']['age'] === 50
182 * ? false
183 * : true;
184 * });
185 * ```
186 *
187 * @param string|callable $action [optional] If a string is provided, it
188 * must be the name of the type of rule to remove (`SetStorageClass`
189 * or `Delete`). All rules of this type will then be cleared. When
190 * providing a callable you may define a custom route for how you
191 * would like to remove rules. The provided callable will be run
192 * through
193 * [array_filter](http://php.net/manual/en/function.array-filter.php).
194 * The callable's argument will be a single lifecycle rule as an
195 * associative array. When returning true from the callable the rule
196 * will be preserved, and if false it will be removed.
197 * **Defaults to** `null`, clearing all assigned rules.
198 * @return Lifecycle
199 * @throws \InvalidArgumentException If a type other than a string or
200 * callabe is provided.
201 */
202 public function clearRules($action = null)
203 {
204 if (!$action) {
205 $this->lifecycle = [];
206 return $this;
207 }
208
209 if (!is_string($action) && !is_callable($action)) {
210 throw new \InvalidArgumentException(
211 sprintf(
212 'Expected either a string or callable, instead got \'%s\'.',
213 gettype($action)
214 )
215 );
216 }
217
218 if (isset($this->lifecycle['rule'])) {
219 if (is_string($action)) {
220 $action = function ($rule) use ($action) {
221 return $rule['action']['type'] !== $action;
222 };
223 }
224
225 $this->lifecycle['rule'] = array_filter(
226 $this->lifecycle['rule'],
227 $action
228 );
229
230 if (!$this->lifecycle['rule']) {
231 $this->lifecycle = [];
232 }
233 }
234
235 return $this;
236 }
237
238 /**
239 * @access private
240 * @return \Generator
241 */
242 public function getIterator()
243 {
244 if (!isset($this->lifecycle['rule'])) {
245 return;
246 }
247
248 foreach ($this->lifecycle['rule'] as $rule) {
249 yield $rule;
250 }
251 }
252
253 /**
254 * @access private
255 * @return array
256 */
257 public function toArray()
258 {
259 return $this->lifecycle;
260 }
261
262 /**
263 * @access private
264 * @param string $offset
265 * @param mixed $value
266 */
267 public function offsetSet($offset, $value)
268 {
269 $this->lifecycle['rule'][$offset] = $value;
270 }
271
272 /**
273 * @access private
274 * @param string $offset
275 * @return bool
276 */
277 public function offsetExists($offset)
278 {
279 return isset($this->lifecycle['rule'][$offset]);
280 }
281
282 /**
283 * @access private
284 * @param string $offset
285 */
286 public function offsetUnset($offset)
287 {
288 unset($this->lifecycle['rule'][$offset]);
289 }
290
291 /**
292 * @access private
293 * @param string $offset
294 * @return mixed
295 */
296 public function offsetGet($offset)
297 {
298 return isset($this->lifecycle['rule'][$offset])
299 ? $this->lifecycle['rule'][$offset]
300 : null;
301 }
302 }
303