|
17 | 17 |
|
18 | 18 | namespace Google\Cloud\Firestore; |
19 | 19 |
|
20 | | -use Google\Cloud\Firestore\Connection\ConnectionInterface; |
| 20 | +use Google\ApiCore\ApiException; |
| 21 | +use Google\ApiCore\Options\CallOptions; |
| 22 | +use Google\Cloud\Core\ApiHelperTrait; |
| 23 | +use Google\Cloud\Core\OptionsValidator; |
| 24 | +use Google\Cloud\Core\RequestProcessorTrait; |
| 25 | +use Google\Cloud\Firestore\V1\Client\FirestoreClient; |
21 | 26 | use Google\Cloud\Firestore\V1\ExplainOptions; |
| 27 | +use Google\Cloud\Firestore\V1\RunAggregationQueryRequest; |
22 | 28 | use InvalidArgumentException; |
23 | 29 |
|
24 | 30 | /** |
|
36 | 42 | */ |
37 | 43 | class AggregateQuery |
38 | 44 | { |
| 45 | + use ApiHelperTrait; |
39 | 46 | use QueryTrait; |
| 47 | + use RequestProcessorTrait; |
40 | 48 |
|
41 | | - /** |
42 | | - * @var ConnectionInterface |
43 | | - * @internal |
44 | | - */ |
45 | | - private $connection; |
46 | | - |
47 | | - /** |
48 | | - * @var array |
49 | | - */ |
50 | | - private $query; |
51 | | - |
52 | | - /** |
53 | | - * @var string |
54 | | - */ |
55 | | - private $parentName; |
56 | | - |
57 | | - /** |
58 | | - * @var array |
59 | | - */ |
60 | | - private $aggregates = []; |
| 49 | + private FirestoreClient $gapicClient; |
| 50 | + private array $query; |
| 51 | + private string $parentName; |
| 52 | + private array $aggregates = []; |
| 53 | + private Serializer $serializer; |
| 54 | + private OptionsValidator $optionsValidator; |
61 | 55 |
|
62 | 56 | /** |
63 | 57 | * Create an aggregation query. |
64 | 58 | * |
65 | | - * @param ConnectionInterface $connection A Connection to Cloud Firestore. |
66 | | - * This object is created by FirestoreClient, |
67 | | - * and should not be instantiated outside of this client. |
| 59 | + * @param FirestoreClient $firestoreClient A FirestoreClient instance. |
68 | 60 | * @param string $parent The parent of the query. |
69 | 61 | * @param array $query Represents the underlying structured query. |
70 | 62 | * @param Aggregate $aggregate Aggregation over the provided query. |
71 | 63 | */ |
72 | 64 | public function __construct( |
73 | | - ConnectionInterface $connection, |
| 65 | + FirestoreClient $firestoreClient, |
74 | 66 | $parent, |
75 | 67 | array $query, |
76 | 68 | Aggregate $aggregate |
77 | 69 | ) { |
78 | | - $this->connection = $connection; |
| 70 | + $this->gapicClient = $firestoreClient; |
79 | 71 | $this->parentName = $parent; |
80 | 72 | $this->query = $query; |
81 | 73 | $this->aggregates[] = $aggregate; |
| 74 | + $this->serializer = new Serializer(); |
| 75 | + $this->optionsValidator = new OptionsValidator($this->serializer); |
82 | 76 | } |
83 | 77 |
|
84 | 78 | /** |
@@ -115,17 +109,37 @@ public function getSnapshot($options = []) |
115 | 109 | ); |
116 | 110 | } |
117 | 111 |
|
| 112 | + /** @var Aggregate $aggregate */ |
118 | 113 | foreach ($this->aggregates as $aggregate) { |
119 | 114 | $parsedAggregates[] = $aggregate->getProps(); |
120 | 115 | } |
121 | | - $snapshot = $this->connection->runAggregationQuery([ |
122 | | - 'parent' => $this->parentName, |
123 | | - 'structuredAggregationQuery' => $this->aggregateQueryPrepare([ |
124 | | - 'aggregates' => $this->aggregates |
125 | | - ] + $this->query), |
126 | | - ] + $options)->current(); |
127 | 116 |
|
128 | | - return new AggregateQuerySnapshot($snapshot); |
| 117 | + $jsonStructuredAggregationQuery = $this->aggregateQueryPrepare([ |
| 118 | + 'aggregates' => $this->aggregates |
| 119 | + ] + $this->query); |
| 120 | + |
| 121 | + $options += [ |
| 122 | + 'structuredAggregationQuery' => $jsonStructuredAggregationQuery, |
| 123 | + 'parent' => $this->parentName |
| 124 | + ]; |
| 125 | + |
| 126 | + /** |
| 127 | + * @var RunAggregationQueryRequest $request |
| 128 | + * @var CallOptions $callOptions |
| 129 | + */ |
| 130 | + [$request, $callOptions] = $this->validateOptions( |
| 131 | + $options, |
| 132 | + new RunAggregationQueryRequest(), |
| 133 | + CallOptions::class |
| 134 | + ); |
| 135 | + |
| 136 | + try { |
| 137 | + $snapshot = $this->gapicClient->runAggregationQuery($request, $callOptions)->readAll()->current(); |
| 138 | + } catch (ApiException $ex) { |
| 139 | + throw $this->convertToGoogleException($ex); |
| 140 | + } |
| 141 | + |
| 142 | + return new AggregateQuerySnapshot($this->serializer->encodeMessage($snapshot)); |
129 | 143 | } |
130 | 144 |
|
131 | 145 | /** |
|
0 commit comments