| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Copyright 2017 Google Inc. All Rights Reserved. |
| 5 |
* |
| 6 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 |
* you may not use this file except in compliance with the License. |
| 8 |
* You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, software |
| 13 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
* See the License for the specific language governing permissions and |
| 16 |
* limitations under the License. |
| 17 |
*/ |
| 18 |
namespace Dudlewebs\WPMCS\Google\Cloud\Storage; |
| 19 |
|
| 20 |
use Dudlewebs\WPMCS\Google\Cloud\Core\ArrayTrait; |
| 21 |
use Dudlewebs\WPMCS\Google\Cloud\Core\Exception\NotFoundException; |
| 22 |
use Dudlewebs\WPMCS\Google\Cloud\Storage\Connection\ConnectionInterface; |
| 23 |
/** |
| 24 |
* Cloud Pub/Sub Notifications sends information about changes to objects in |
| 25 |
* your buckets to Google Cloud Pub/Sub, where the information is added to a |
| 26 |
* Cloud Pub/Sub topic of your choice in the form of messages. For example, |
| 27 |
* you can track objects that are created and deleted in your bucket. Each |
| 28 |
* notification contains information describing both the event that triggered it |
| 29 |
* and the object that changed. |
| 30 |
* |
| 31 |
* To utilize this class and see more examples, please see the relevant |
| 32 |
* notifications based methods exposed on {@see Bucket}. |
| 33 |
* |
| 34 |
* Example: |
| 35 |
* ``` |
| 36 |
* use Google\Cloud\Storage\StorageClient; |
| 37 |
* |
| 38 |
* $storage = new StorageClient(); |
| 39 |
* |
| 40 |
* // Fetch an existing notification by ID. |
| 41 |
* $bucket = $storage->bucket('my-bucket'); |
| 42 |
* $notification = $bucket->notification('2482'); |
| 43 |
* ``` |
| 44 |
* |
| 45 |
* @see https://cloud.google.com/storage/docs/pubsub-notifications |
| 46 |
* @experimental The experimental flag means that while we believe this method |
| 47 |
* or class is ready for use, it may change before release in backwards- |
| 48 |
* incompatible ways. Please use with caution, and test thoroughly when |
| 49 |
* upgrading. |
| 50 |
*/ |
| 51 |
class Notification |
| 52 |
{ |
| 53 |
use ArrayTrait; |
| 54 |
/** |
| 55 |
* @var ConnectionInterface Represents a connection to Cloud Storage. |
| 56 |
* @internal |
| 57 |
*/ |
| 58 |
private $connection; |
| 59 |
/** |
| 60 |
* @var array The notification's identity. |
| 61 |
*/ |
| 62 |
private $identity; |
| 63 |
/** |
| 64 |
* @var array The notification's metadata. |
| 65 |
*/ |
| 66 |
private $info; |
| 67 |
/** |
| 68 |
* @param ConnectionInterface $connection Represents a connection to Cloud |
| 69 |
* Storage. This object is created by StorageClient, |
| 70 |
* and should not be instantiated outside of this client. |
| 71 |
* @param string $id The notification's ID. |
| 72 |
* @param string $bucket The name of the bucket associated with this |
| 73 |
* notification. |
| 74 |
* @param array $info [optional] The notification's metadata. |
| 75 |
*/ |
| 76 |
public function __construct(ConnectionInterface $connection, $id, $bucket, array $info = []) |
| 77 |
{ |
| 78 |
$this->connection = $connection; |
| 79 |
$this->identity = ['bucket' => $bucket, 'notification' => $id, 'userProject' => $this->pluck('requesterProjectId', $info, \false)]; |
| 80 |
$this->info = $info; |
| 81 |
} |
| 82 |
/** |
| 83 |
* Check whether or not the notification exists. |
| 84 |
* |
| 85 |
* Example: |
| 86 |
* ``` |
| 87 |
* if ($notification->exists()) { |
| 88 |
* echo 'Notification exists!'; |
| 89 |
* } |
| 90 |
* ``` |
| 91 |
* @param array $options [optional] { |
| 92 |
* Configuration options. |
| 93 |
* } |
| 94 |
* @return bool |
| 95 |
*/ |
| 96 |
public function exists(array $options = []) |
| 97 |
{ |
| 98 |
try { |
| 99 |
$this->connection->getNotification($options + $this->identity + ['fields' => 'id']); |
| 100 |
} catch (NotFoundException $ex) { |
| 101 |
return \false; |
| 102 |
} |
| 103 |
return \true; |
| 104 |
} |
| 105 |
/** |
| 106 |
* Delete the notification. |
| 107 |
* |
| 108 |
* Example: |
| 109 |
* ``` |
| 110 |
* $notification->delete(); |
| 111 |
* ``` |
| 112 |
* |
| 113 |
* @codingStandardsIgnoreStart |
| 114 |
* @see https://cloud.google.com/storage/docs/json_api/v1/notifications/delete Notifications delete API documentation. |
| 115 |
* @codingStandardsIgnoreEnd |
| 116 |
* |
| 117 |
* @param array $options [optional] |
| 118 |
* @return void |
| 119 |
*/ |
| 120 |
public function delete(array $options = []) |
| 121 |
{ |
| 122 |
$this->connection->deleteNotification($options + $this->identity); |
| 123 |
} |
| 124 |
/** |
| 125 |
* Retrieves the notification's details. If no notification data is cached a |
| 126 |
* network request will be made to retrieve it. |
| 127 |
* |
| 128 |
* Example: |
| 129 |
* ``` |
| 130 |
* $info = $notification->info(); |
| 131 |
* echo $info['topic']; |
| 132 |
* ``` |
| 133 |
* |
| 134 |
* @see https://cloud.google.com/storage/docs/json_api/v1/notifications/get Notifications get API documentation. |
| 135 |
* |
| 136 |
* @param array $options [optional] |
| 137 |
* @return array |
| 138 |
*/ |
| 139 |
public function info(array $options = []) |
| 140 |
{ |
| 141 |
return $this->info ?: $this->reload($options); |
| 142 |
} |
| 143 |
/** |
| 144 |
* Triggers a network request to reload the notification's details. |
| 145 |
* |
| 146 |
* Example: |
| 147 |
* ``` |
| 148 |
* $notification->reload(); |
| 149 |
* $info = $notification->info(); |
| 150 |
* echo $info['topic']; |
| 151 |
* ``` |
| 152 |
* |
| 153 |
* @see https://cloud.google.com/storage/docs/json_api/v1/notifications/get Notifications get API documentation. |
| 154 |
* |
| 155 |
* @param array $options [optional] |
| 156 |
* @return array |
| 157 |
*/ |
| 158 |
public function reload(array $options = []) |
| 159 |
{ |
| 160 |
return $this->info = $this->connection->getNotification($options + $this->identity); |
| 161 |
} |
| 162 |
/** |
| 163 |
* Retrieves the notification's ID. |
| 164 |
* |
| 165 |
* Example: |
| 166 |
* ``` |
| 167 |
* echo $notification->id(); |
| 168 |
* ``` |
| 169 |
* |
| 170 |
* @return string |
| 171 |
*/ |
| 172 |
public function id() |
| 173 |
{ |
| 174 |
return $this->identity['notification']; |
| 175 |
} |
| 176 |
/** |
| 177 |
* Retrieves the notification's identity. |
| 178 |
* |
| 179 |
* Example: |
| 180 |
* ``` |
| 181 |
* echo $notification->identity()['bucket']; |
| 182 |
* ``` |
| 183 |
* |
| 184 |
* @return array |
| 185 |
*/ |
| 186 |
public function identity() |
| 187 |
{ |
| 188 |
return $this->identity; |
| 189 |
} |
| 190 |
} |
| 191 |
|