-
Notifications
You must be signed in to change notification settings - Fork 460
Expand file tree
/
Copy pathSnapshotTrait.php
More file actions
342 lines (304 loc) · 10.8 KB
/
SnapshotTrait.php
File metadata and controls
342 lines (304 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<?php
/**
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\Cloud\Firestore;
use Google\ApiCore\ApiException;
use Google\ApiCore\Options\CallOptions;
use Google\Cloud\Core\ApiHelperTrait;
use Google\Cloud\Core\Exception\NotFoundException;
use Google\Cloud\Core\Exception\ServiceException;
use Google\Cloud\Core\RequestProcessorTrait;
use Google\Cloud\Core\Timestamp;
use Google\Cloud\Core\TimestampTrait;
use Google\Cloud\Core\TimeTrait;
use Google\Cloud\Firestore\V1\BatchGetDocumentsRequest;
use Google\Cloud\Firestore\V1\BatchGetDocumentsResponse;
use Google\Cloud\Firestore\V1\Client\FirestoreClient;
/**
* Methods common to representing Document Snapshots.
*/
trait SnapshotTrait
{
use ApiHelperTrait;
use PathTrait;
use TimeTrait;
use TimestampTrait;
use RequestProcessorTrait;
/**
* Execute a service request to retrieve a document snapshot.
*
* @param FirestoreClient $gapicClient A FirestoreClient instance.
* @param ValueMapper $valueMapper A Firestore Value Mapper.
* @param DocumentReference $reference The parent document.
* @param array $options {
* Configuration Options
*
* @type string $transaction The transaction ID to fetch the snapshot.
* }
* @return DocumentSnapshot
*/
private function createSnapshot(
FirestoreClient $gapicClient,
ValueMapper $valueMapper,
DocumentReference $reference,
array $options = []
) {
$document = [];
$fields = [];
$exists = true;
try {
$document = $this->getSnapshot($gapicClient, $reference->name(), $options);
} catch (NotFoundException $e) {
$exists = false;
}
return $this->createSnapshotWithData($valueMapper, $reference, $document, $exists);
}
/**
* Create a document snapshot by providing a dataset.
*
* This method will not perform a service request.
*
* @codingStandardsIgnoreStart
* @param ValueMapper $valueMapper A Firestore Value Mapper.
* @param DocumentReference $reference The parent document.
* @param array $document [Document](https://cloud.google.com/firestore/docs/reference/rpc/google.firestore.v1beta1#google.firestore.v1beta1.Document)
* @param bool $exists Whether the document exists. **Defaults to** `true`.
* @codingStandardsIgnoreEnd
*/
private function createSnapshotWithData(
ValueMapper $valueMapper,
DocumentReference $reference,
array $document,
$exists = true
) {
$fields = $exists
? $valueMapper->decodeValues($this->pluck('fields', $document))
: [];
$document = $this->transformSnapshotTimestamps($document);
return new DocumentSnapshot($reference, $valueMapper, $document, $fields, $exists);
}
/**
* Send a service request for a snapshot, and return the raw data
*
* @param FirestoreClient $gapicClient A firestore client instance.
* @param string $name The document name.
* @param array $options Configuration options.
* @return array
* @throws \InvalidArgumentException if an invalid `$options.readTime` is
* specified.
* @throws NotFoundException If the document does not exist.
* @throws ServiceException
*/
private function getSnapshot(FirestoreClient $gapicClient, $name, array $options = []): array
{
$options = $this->formatReadTimeOption($options);
/**
* @var BatchGetDocumentsRequest $request
* @var CallOptions $callOptions
*/
[$request, $callOptions] = $this->validateOptions(
[
'database' => $this->databaseFromName($name),
'documents' => [$name]
] + $options,
new BatchGetDocumentsRequest(),
CallOptions::class,
);
try {
$stream = $gapicClient->batchGetDocuments($request, $callOptions);
} catch (ApiException $ex) {
throw $this->convertToGoogleException($ex);
}
/** @var BatchGetDocumentsResponse */
$response = $stream->readAll()->current();
if (!$response->hasFound()) {
throw new NotFoundException(sprintf(
'Document %s does not exist',
$name
));
}
return $this->serializer->encodeMessage($response->getFound());
}
/**
* Fetches a list of documents by their paths, orders them to match the
* input order, creates a list of snapshots (whether the document exists or
* not), and returns.
*
* @param FirestoreClient $gapicClient An instance of the Firestore client.
* @param ValueMapper $mapper A Firestore value mapper.
* @param string $projectId The current project id.
* @param string $database The database id.
* @param string[]|DocumentReference[] $paths A list of fully-qualified
* firestore document paths or DocumentReference instances.
* @param array $options Configuration options.
* @return DocumentSnapshot[]
* @throws ServiceException
*/
private function getDocumentsByPaths(
FirestoreClient $gapicClient,
ValueMapper $mapper,
$projectId,
$database,
array $paths,
array $options
) {
$documentNames = [];
foreach ($paths as $path) {
if ($path instanceof DocumentReference) {
$path = $path->name();
}
if (!is_string($path)) {
throw new \InvalidArgumentException(
'All members of $paths must be strings or instances of DocumentReference.'
);
}
$path = $this->isRelative($path)
? $this->fullName($projectId, $database, $path)
: $path;
$documentNames[] = $path;
}
/**
* @var BatchGetDocumentsRequest $request
* @var CallOptions $callOptions
*/
[$request, $callOptions] = $this->validateOptions(
[
'database' => $this->databaseName($projectId, $database),
'documents' => $documentNames
] + $options,
new BatchGetDocumentsRequest(),
CallOptions::class
);
try {
$stream = $gapicClient->batchGetDocuments($request, $callOptions);
} catch (ApiException $ex) {
throw $this->convertToGoogleException($ex);
}
$res = [];
/** @var BatchGetDocumentsResponse $response*/
foreach ($stream->readAll() as $response) {
$document = $this->serializer->encodeMessage($response);
$exists = $response->hasFound();
$data = $exists
? $document['found'] + ['readTime' => $document['readTime']]
: ['readTime' => $document['readTime']];
$name = $exists
? $document['found']['name']
: $document['missing'];
$ref = $this->getDocumentReference(
$gapicClient,
$mapper,
$projectId,
$database,
$name
);
$res[$name] = $this->createSnapshotWithData(
$mapper,
$ref,
$data,
$exists
);
}
$out = [];
foreach ($documentNames as $path) {
$out[] = $res[$path];
}
return $out;
}
/**
* Creates a DocumentReference object.
*
* @param FirestoreClient $gapicClient An instance of the Firestore client.
* @param ValueMapper $mapper A Firestore value mapper.
* @param string $projectId The current project id.
* @param string $database The database id.
* @param string $name The document name, in absolute form, or relative to the database.
* @return DocumentReference
* @throws \InvalidArgumentException if an invalid path is provided.
*/
private function getDocumentReference(
FirestoreClient $gapicClient,
ValueMapper $mapper,
$projectId,
$database,
$name
) {
if ($this->isRelative($name)) {
$name = $this->fullName($projectId, $database, $name);
}
if (!$this->isDocument($name)) {
throw new \InvalidArgumentException('Given path is not a valid document path.');
}
return new DocumentReference(
$gapicClient,
$mapper,
$this->getCollectionReference(
$gapicClient,
$mapper,
$projectId,
$database,
$this->parentPath($name)
),
$name
);
}
/**
* Creates a CollectionReference object.
*
* @param FirestoreClient $gapicClient A FirestoreClient instance.
* @param ValueMapper $mapper A Firestore value mapper.
* @param string $projectId The current project id.
* @param string $database The database id.
* @param string $name The collection name, in absolute form, or relative to the database.
* @return CollectionReference
* @throws \InvalidArgumentException if an invalid path is provided.
*/
private function getCollectionReference(
FirestoreClient $gapicClient,
ValueMapper $mapper,
$projectId,
$database,
$name
) {
if ($this->isRelative($name)) {
$name = $this->fullName($projectId, $database, $name);
}
if (!$this->isCollection($name)) {
throw new \InvalidArgumentException(sprintf(
'Given path `%s` is not a valid collection path.',
$name
));
}
return new CollectionReference($gapicClient, $mapper, $name);
}
/**
* Convert snapshot timestamps to Google Cloud PHP types.
*
* @param array $data The snapshot data.
* @return array
*/
private function transformSnapshotTimestamps(array $data)
{
foreach (['createTime', 'updateTime', 'readTime'] as $timestampField) {
if (!isset($data[$timestampField])) {
continue;
}
list($dt, $nanos) = $this->parseTimeString($data[$timestampField]);
$data[$timestampField] = new Timestamp($dt, $nanos);
}
return $data;
}
}