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 / Notification.php

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

201 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Copyright 2017 Google Inc. All Rights Reserved.
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 use Google\Cloud\Core\ArrayTrait;
21 use Google\Cloud\Core\Exception\NotFoundException;
22 use Google\Cloud\Storage\Connection\ConnectionInterface;
23
24 /**
25 * Cloud Pub/Sub Notifications sends information about changes to objects in
26 * your buckets to Google Cloud Pub/Sub, where the information is added to a
27 * Cloud Pub/Sub topic of your choice in the form of messages. For example,
28 * you can track objects that are created and deleted in your bucket. Each
29 * notification contains information describing both the event that triggered it
30 * and the object that changed.
31 *
32 * Example:
33 * ```
34 * use Google\Cloud\Storage\StorageClient;
35 *
36 * $storage = new StorageClient();
37 *
38 * $bucket = $storage->bucket('my-bucket');
39 * $notification = $bucket->notification('2482');
40 * ```
41 *
42 * @see https://cloud.google.com/storage/docs/pubsub-notifications
43 * @experimental The experimental flag means that while we believe this method
44 * or class is ready for use, it may change before release in backwards-
45 * incompatible ways. Please use with caution, and test thoroughly when
46 * upgrading.
47 */
48 class Notification
49 {
50 use ArrayTrait;
51
52 /**
53 * @var ConnectionInterface Represents a connection to Cloud Storage.
54 */
55 private $connection;
56
57 /**
58 * @var array The notification's identity.
59 */
60 private $identity;
61
62 /**
63 * @var array The notification's metadata.
64 */
65 private $info;
66
67 /**
68 * @param ConnectionInterface $connection Represents a connection to Cloud
69 * Storage.
70 * @param string $id The notification's ID.
71 * @param string $bucket The name of the bucket associated with this
72 * notification.
73 * @param array $info [optional] The notification's metadata.
74 */
75 public function __construct(ConnectionInterface $connection, $id, $bucket, array $info = [])
76 {
77 $this->connection = $connection;
78 $this->identity = [
79 'bucket' => $bucket,
80 'notification' => $id,
81 'userProject' => $this->pluck('requesterProjectId', $info, false)
82 ];
83 $this->info = $info;
84 }
85
86 /**
87 * Check whether or not the notification exists.
88 *
89 * Example:
90 * ```
91 * if ($notification->exists()) {
92 * echo 'Notification exists!';
93 * }
94 * ```
95 *
96 * @return bool
97 */
98 public function exists()
99 {
100 try {
101 $this->connection->getNotification($this->identity + ['fields' => 'id']);
102 } catch (NotFoundException $ex) {
103 return false;
104 }
105
106 return true;
107 }
108
109 /**
110 * Delete the notification.
111 *
112 * Example:
113 * ```
114 * $notification->delete();
115 * ```
116 *
117 * @codingStandardsIgnoreStart
118 * @see https://cloud.google.com/storage/docs/json_api/v1/notifications/delete Notifications delete API documentation.
119 * @codingStandardsIgnoreEnd
120 *
121 * @param array $options [optional]
122 * @return void
123 */
124 public function delete(array $options = [])
125 {
126 $this->connection->deleteNotification($options + $this->identity);
127 }
128
129 /**
130 * Retrieves the notification's details. If no notification data is cached a
131 * network request will be made to retrieve it.
132 *
133 * Example:
134 * ```
135 * $info = $notification->info();
136 * echo $info['topic'];
137 * ```
138 *
139 * @see https://cloud.google.com/storage/docs/json_api/v1/notifications/get Notifications get API documentation.
140 *
141 * @param array $options [optional]
142 * @return array
143 */
144 public function info(array $options = [])
145 {
146 return $this->info ?: $this->reload($options);
147 }
148
149 /**
150 * Triggers a network request to reload the notification's details.
151 *
152 * Example:
153 * ```
154 * $notification->reload();
155 * $info = $notification->info();
156 * echo $info['topic'];
157 * ```
158 *
159 * @see https://cloud.google.com/storage/docs/json_api/v1/notifications/get Notifications get API documentation.
160 *
161 * @param array $options [optional]
162 * @return array
163 */
164 public function reload(array $options = [])
165 {
166 return $this->info = $this->connection->getNotification(
167 $options + $this->identity
168 );
169 }
170
171 /**
172 * Retrieves the notification's ID.
173 *
174 * Example:
175 * ```
176 * echo $notification->id();
177 * ```
178 *
179 * @return string
180 */
181 public function id()
182 {
183 return $this->identity['notification'];
184 }
185
186 /**
187 * Retrieves the notification's identity.
188 *
189 * Example:
190 * ```
191 * echo $notification->identity()['bucket'];
192 * ```
193 *
194 * @return array
195 */
196 public function identity()
197 {
198 return $this->identity;
199 }
200 }
201