Skip to content

Commit e74b497

Browse files
committed
untange options vars and traits by
- explicitly selecting valid fields - better names for variables - documenting missing options in phpdoc - removing pass-by-reference
1 parent 5887aef commit e74b497

60 files changed

Lines changed: 982 additions & 1026 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Core/src/LongRunning/LongRunningGapicConnection.php renamed to Core/src/LongRunning/LongRunningClientConnection.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,7 @@
2020
use Google\ApiCore\OperationResponse;
2121
use Google\ApiCore\Serializer;
2222
use Google\Cloud\Core\RequestProcessorTrait;
23-
use Google\LongRunning\Operation;
2423
use Google\LongRunning\ListOperationsRequest;
25-
use Google\LongRunning\GetOperationRequest;
26-
use Google\LongRunning\CancelOperationRequest;
27-
use Google\LongRunning\DeleteOperationRequest;
2824
use Google\Protobuf\Any;
2925

3026
/**
@@ -33,7 +29,7 @@
3329
*
3430
* @internal
3531
*/
36-
class LongRunningGapicConnection implements LongRunningConnectionInterface
32+
class LongRunningClientConnection implements LongRunningConnectionInterface
3733
{
3834
use RequestProcessorTrait;
3935

Spanner/src/Backup.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@
1919

2020
use Closure;
2121
use DateTimeInterface;
22-
use Google\Cloud\Core\LongRunning\LongRunningGapicConnection;
23-
use Google\Cloud\Core\LongRunning\LongRunningOperation;
2422
use Google\ApiCore\ValidationException;
2523
use Google\Cloud\Core\Exception\NotFoundException;
2624
use Google\Cloud\Core\Iterator\ItemIterator;
27-
use Google\Cloud\Spanner\Admin\Database\V1\Backup as BackupProto;
25+
use Google\Cloud\Core\LongRunning\LongRunningClientConnection;
26+
use Google\Cloud\Core\LongRunning\LongRunningOperation;
2827
use Google\Cloud\Spanner\Admin\Database\V1\Backup\State;
2928
use Google\Cloud\Spanner\Admin\Database\V1\Client\DatabaseAdminClient;
3029
use Google\Cloud\Spanner\Admin\Database\V1\CopyBackupRequest;
@@ -33,6 +32,7 @@
3332
use Google\Cloud\Spanner\Admin\Database\V1\GetBackupRequest;
3433
use Google\Cloud\Spanner\Admin\Database\V1\UpdateBackupRequest;
3534
use Google\LongRunning\ListOperationsRequest;
35+
use Google\LongRunning\Operation as OperationProto;
3636

3737
/**
3838
* Represents a Cloud Spanner Backup.
@@ -365,7 +365,7 @@ public function updateExpireTime(DateTimeInterface $newTimestamp, array $options
365365
public function resumeOperation($operationName, array $options = []): LongRunningOperation
366366
{
367367
return new LongRunningOperation(
368-
new LongRunningGapicConnection($this->databaseAdminClient, $this->serializer),
368+
new LongRunningClientConnection($this->databaseAdminClient, $this->serializer),
369369
$operationName,
370370
[
371371
[
@@ -408,7 +408,13 @@ public function longRunningOperations(array $options = []): ItemIterator
408408
return $this->buildLongRunningIterator(
409409
[$this->databaseAdminClient->getOperationsClient(), 'listOperations'],
410410
$request,
411-
$callOptions
411+
$callOptions,
412+
function (OperationProto $operation) {
413+
return $this->resumeOperation(
414+
$operation->getName(),
415+
$this->handleResponse($operation)
416+
);
417+
}
412418
);
413419
}
414420

@@ -438,7 +444,7 @@ private function backupResultFunction(): Closure
438444
{
439445
return function (array $backup) {
440446
$name = DatabaseAdminClient::parseName($backup['name']);
441-
return $this->instance->backup($name['name'], $backup);
447+
return $this->instance->backup($name['backup'], $backup);
442448
};
443449
}
444450
}

Spanner/src/Batch/BatchClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public function snapshot(array $options = [])
187187
$transactionOptions = $this->pluck('transactionOptions', $options);
188188
$transactionOptions['returnReadTimestamp'] = true;
189189

190-
$transactionOptions = $this->configureSnapshotOptions($transactionOptions);
190+
$transactionOptions = $this->configureReadOnlyTransactionOptions($transactionOptions);
191191

192192
if ($this->databaseRole !== null) {
193193
$sessionOptions['creator_role'] = $this->databaseRole;

Spanner/src/Database.php

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,12 @@
2727
use Google\Cloud\Core\Exception\ServiceException;
2828
use Google\Cloud\Core\Iam\IamManager;
2929
use Google\Cloud\Core\Iterator\ItemIterator;
30+
use Google\Cloud\Core\LongRunning\LongRunningClientConnection;
31+
use Google\Cloud\Core\LongRunning\LongRunningOperation;
3032
use Google\Cloud\Core\RequestHandler;
3133
use Google\Cloud\Core\Retry;
32-
use Google\Cloud\Core\LongRunning\LongRunningGapicConnection;
33-
use Google\Cloud\Core\LongRunning\LongRunningOperation;
3434
use Google\Cloud\Spanner\Admin\Database\V1\Client\DatabaseAdminClient;
3535
use Google\Cloud\Spanner\Admin\Database\V1\CreateDatabaseRequest;
36-
use Google\Cloud\Spanner\Admin\Database\V1\Database as DatabaseProto;
3736
use Google\Cloud\Spanner\Admin\Database\V1\Database\State;
3837
use Google\Cloud\Spanner\Admin\Database\V1\DatabaseDialect;
3938
use Google\Cloud\Spanner\Admin\Database\V1\DropDatabaseRequest;
@@ -55,6 +54,7 @@
5554
use Google\Cloud\Spanner\V1\Mutation\Write;
5655
use Google\Cloud\Spanner\V1\TypeCode;
5756
use Google\LongRunning\ListOperationsRequest;
57+
use Google\LongRunning\Operation as OperationProto;
5858
use Google\Protobuf\Duration;
5959
use Google\Protobuf\ListValue;
6060
use Google\Protobuf\Struct;
@@ -766,7 +766,19 @@ public function snapshot(array $options = []): Snapshot
766766
'singleUse' => false
767767
];
768768

769-
$options['transactionOptions'] = $this->configureSnapshotOptions($options);
769+
$options['transactionOptions'] = $this->configureReadOnlyTransactionOptions($options);
770+
771+
// For backwards compatibility - remove all PBReadOnly fields
772+
// This was previously being done in configureReadOnlyTransactionOptions
773+
// @TODO: clean this up
774+
unset(
775+
$options['returnReadTimestamp'],
776+
$options['strong'],
777+
$options['readTimestamp'],
778+
$options['exactStaleness'],
779+
$options['minReadTimestamp'],
780+
$options['maxStaleness'],
781+
);
770782

771783
$session = $this->selectSession(
772784
SessionPoolInterface::CONTEXT_READ,
@@ -827,8 +839,7 @@ public function transaction(array $options = []): Transaction
827839
throw new \BadMethodCallException('Nested transactions are not supported by this client.');
828840
}
829841

830-
// There isn't anything configurable here.
831-
$options['transactionOptions'] = $this->configureTransactionOptions();
842+
$options['transactionOptions'] = $this->configureReadWriteTransactionOptions();
832843

833844
$session = $this->selectSession(
834845
SessionPoolInterface::CONTEXT_READWRITE,
@@ -944,7 +955,9 @@ public function runTransaction(callable $operation, array $options = [])
944955
}
945956

946957
// There isn't anything configurable here.
947-
$options['transactionOptions'] = $this->configureTransactionOptions($options['transactionOptions'] ?? []);
958+
$options['transactionOptions'] = $this->configureReadWriteTransactionOptions(
959+
$options['transactionOptions'] ?? []
960+
);
948961

949962
$session = $this->selectSession(
950963
SessionPoolInterface::CONTEXT_READWRITE,
@@ -1933,6 +1946,7 @@ public function batchWrite(array $mutationGroups, array $options = []): \Generat
19331946
* Please note, if using the `priority` setting you may utilize the constants available
19341947
* on {@see \Google\Cloud\Spanner\V1\RequestOptions\Priority} to set a value.
19351948
* Please note, the `transactionTag` setting will be ignored as it is not supported for partitioned DML.
1949+
* @type array $transactionOptions Transaction options ({@see V1\TransactionOptions}).
19361950
* }
19371951
* @return int The number of rows modified.
19381952
*/
@@ -2291,7 +2305,8 @@ public function backupOperations(array $options = []): ItemIterator
22912305
return $this->buildLongRunningIterator(
22922306
[$this->databaseAdminClient, 'listBackupOperations'],
22932307
$request,
2294-
$callOptions + ['resource-prefix' => $this->name]
2308+
$callOptions + ['resource-prefix' => $this->name],
2309+
$this->getResultMapper()
22952310
);
22962311
}
22972312

@@ -2353,7 +2368,8 @@ public function databaseOperations(array $options = []): ItemIterator
23532368
return $this->buildLongRunningIterator(
23542369
[$this->databaseAdminClient, 'listDatabaseOperations'],
23552370
$request,
2356-
$callOptions + ['resource-prefix' => $this->name]
2371+
$callOptions + ['resource-prefix' => $this->name],
2372+
$this->getResultMapper()
23572373
);
23582374
}
23592375

@@ -2371,7 +2387,7 @@ public function databaseOperations(array $options = []): ItemIterator
23712387
public function resumeOperation($operationName, array $options = []): LongRunningOperation
23722388
{
23732389
return new LongRunningOperation(
2374-
new LongRunningGapicConnection($this->databaseAdminClient, $this->serializer),
2390+
new LongRunningClientConnection($this->databaseAdminClient, $this->serializer),
23752391
$operationName,
23762392
[
23772393
[
@@ -2418,7 +2434,8 @@ public function longRunningOperations(array $options = []): ItemIterator
24182434
return $this->buildLongRunningIterator(
24192435
[$this->databaseAdminClient->getOperationsClient(), 'listOperations'],
24202436
$request,
2421-
$callOptions
2437+
$callOptions,
2438+
$this->getResultMapper()
24222439
);
24232440
}
24242441

@@ -2643,6 +2660,16 @@ private function databaseResultFunction(): Closure
26432660
};
26442661
}
26452662

2663+
private function getResultMapper()
2664+
{
2665+
return function (OperationProto $operation) {
2666+
return $this->resumeOperation(
2667+
$operation->getName(),
2668+
$this->handleResponse($operation)
2669+
);
2670+
};
2671+
}
2672+
26462673
/**
26472674
* Represent the class in a more readable and digestable fashion.
26482675
*

Spanner/src/Instance.php

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
namespace Google\Cloud\Spanner;
1919

2020
use Closure;
21-
use Google\Cloud\Core\LongRunning\LongRunningOperation;
22-
use Google\Cloud\Core\LongRunning\LongRunningGapicConnection;
2321
use Google\Cloud\Core\Exception\NotFoundException;
2422
use Google\Cloud\Core\Iam\IamManager;
2523
use Google\Cloud\Core\Iterator\ItemIterator;
24+
use Google\Cloud\Core\LongRunning\LongRunningClientConnection;
25+
use Google\Cloud\Core\LongRunning\LongRunningOperation;
2626
use Google\Cloud\Core\RequestHandler;
2727
use Google\Cloud\Spanner\Admin\Database\V1\Client\DatabaseAdminClient;
2828
use Google\Cloud\Spanner\Admin\Database\V1\ListBackupsRequest;
@@ -31,12 +31,12 @@
3131
use Google\Cloud\Spanner\Admin\Instance\V1\CreateInstanceRequest;
3232
use Google\Cloud\Spanner\Admin\Instance\V1\DeleteInstanceRequest;
3333
use Google\Cloud\Spanner\Admin\Instance\V1\GetInstanceRequest;
34-
use Google\Cloud\Spanner\Admin\Instance\V1\Instance as InstanceProto;
3534
use Google\Cloud\Spanner\Admin\Instance\V1\Instance\State;
3635
use Google\Cloud\Spanner\Admin\Instance\V1\UpdateInstanceRequest;
3736
use Google\Cloud\Spanner\Session\SessionPoolInterface;
3837
use Google\Cloud\Spanner\V1\Client\SpannerClient as GapicSpannerClient;
3938
use Google\LongRunning\ListOperationsRequest;
39+
use Google\LongRunning\Operation as OperationProto;
4040

4141
/**
4242
* Represents a Cloud Spanner instance
@@ -84,6 +84,8 @@ class Instance
8484
*/
8585
private $projectName;
8686

87+
private bool $returnInt64AsObject;
88+
8789
/**
8890
* Create an object representing a Cloud Spanner instance.
8991
*
@@ -95,11 +97,7 @@ class Instance
9597
* @param Serializer $serializer The serializer instance to encode/decode messages.
9698
* @param string $projectId The project ID.
9799
* @param string $name The instance name or ID.
98-
* @param bool $returnInt64AsObject [optional] If true, 64 bit integers will be
99-
* returned as a {@see \Google\Cloud\Core\Int64} object for 32 bit platform
100-
* compatibility. **Defaults to** false.
101-
* @param array $info [optional] A representation of the instance object.
102-
* @param array $options [optional]{
100+
* @param array $options {
103101
* Instance options
104102
*
105103
* @type array $directedReadOptions Directed read options.
@@ -108,7 +106,11 @@ class Instance
108106
* {@see \Google\Cloud\Spanner\V1\DirectedReadOptions\ReplicaSelection\Type} to set a value.
109107
* @type bool $routeToLeader Enable/disable Leader Aware Routing.
110108
* **Defaults to** `true` (enabled).
109+
* @type bool $returnInt64AsObject If true, 64 bit integers will be
110+
* returned as a {@see \Google\Cloud\Core\Int64} object for 32 bit platform
111+
* compatibility. **Defaults to** false.
111112
* }
113+
* @param array $info A representation of the instance object.
112114
*/
113115
public function __construct(
114116
private GapicSpannerClient $spannerClient,
@@ -117,14 +119,14 @@ public function __construct(
117119
private Serializer $serializer,
118120
private string $projectId,
119121
private string $name,
120-
private bool $returnInt64AsObject = false,
122+
array $options = [],
121123
private array $info = [],
122-
array $options = []
123124
) {
124125
$this->name = $this->fullyQualifiedInstanceName($name, $projectId);
125126
$this->directedReadOptions = $options['directedReadOptions'] ?? [];
126127
$this->routeToLeader = $options['routeToLeader'] ?? true;
127128
$this->defaultQueryOptions = $options['defaultQueryOptions'] ?? [];
129+
$this->returnInt64AsObject = $options['returnInt64AsObject'] ?? false;
128130
$this->projectName = InstanceAdminClient::projectName($projectId);
129131
}
130132

@@ -795,7 +797,7 @@ private function fieldMask(array $instanceArray): array
795797
*/
796798
public function createInstanceArray(
797799
array $instanceArray,
798-
InstanceConfiguration $config = null
800+
?InstanceConfiguration $config = null
799801
): array {
800802
return $instanceArray + [
801803
'name' => $this->name,
@@ -810,7 +812,7 @@ public function createInstanceArray(
810812
*
811813
* Example:
812814
* ```
813-
* $operation = $spanner->resumeOperation($operationName);
815+
* $operation = $instance->resumeOperation($operationName);
814816
* ```
815817
*
816818
* @param string $operationName The Long Running Operation name.
@@ -819,7 +821,7 @@ public function createInstanceArray(
819821
public function resumeOperation($operationName, array $options = []): LongRunningOperation
820822
{
821823
return new LongRunningOperation(
822-
new LongRunningGapicConnection($this->instanceAdminClient, $this->serializer),
824+
new LongRunningClientConnection($this->instanceAdminClient, $this->serializer),
823825
$operationName,
824826
[
825827
[
@@ -840,7 +842,7 @@ public function resumeOperation($operationName, array $options = []): LongRunnin
840842
*
841843
* Example:
842844
* ```
843-
* $operations = $backup->longRunningOperations();
845+
* $operations = $instance->longRunningOperations();
844846
* ```
845847
*
846848
* @param array $options [optional] {
@@ -866,7 +868,13 @@ public function longRunningOperations(array $options = []): ItemIterator
866868
return $this->buildLongRunningIterator(
867869
[$this->instanceAdminClient->getOperationsClient(), 'listOperations'],
868870
$request,
869-
$callOptions
871+
$callOptions,
872+
function (OperationProto $operation) {
873+
return $this->resumeOperation(
874+
$operation->getName(),
875+
$this->handleResponse($operation)
876+
);
877+
}
870878
);
871879
}
872880

@@ -881,13 +889,13 @@ private function instanceResultFunction(): Closure
881889
$this->serializer,
882890
$this->projectId,
883891
$name['instance'],
884-
$this->returnInt64AsObject,
885-
$result,
886892
[
887893
'directedReadOptions' => $this->directedReadOptions,
888894
'routeToLeader' => $this->routeToLeader,
889895
'defaultQueryOptions' => $this->defaultQueryOptions,
890-
]
896+
'returnInt64AsObject' => $this->returnInt64AsObject,
897+
],
898+
$result,
891899
);
892900
};
893901
}

Spanner/src/InstanceConfiguration.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919

2020
use Closure;
2121
use Google\ApiCore\ApiException;
22-
use Google\Cloud\Core\LongRunning\LongRunningOperation;
23-
use Google\Cloud\Core\LongRunning\LongRunningGapicConnection;
2422
use Google\ApiCore\ValidationException;
23+
use Google\Cloud\Core\LongRunning\LongRunningClientConnection;
24+
use Google\Cloud\Core\LongRunning\LongRunningOperation;
2525
use Google\Cloud\Spanner\Admin\Instance\V1\Client\InstanceAdminClient;
2626
use Google\Cloud\Spanner\Admin\Instance\V1\CreateInstanceConfigRequest;
2727
use Google\Cloud\Spanner\Admin\Instance\V1\DeleteInstanceConfigRequest;
@@ -280,10 +280,9 @@ public function update(array $options = [])
280280
[$data, $callOptions] = $this->splitOptionalArgs($options);
281281
$validateOnly = $data['validateOnly'] ?? false;
282282
unset($data['validateOnly']);
283-
$data += ['name' => $this->name];
284283

285284
$request = $this->serializer->decodeMessage(new UpdateInstanceConfigRequest(), [
286-
'instanceConfig' => $data,
285+
'instanceConfig' => $data + ['name' => $this->name],
287286
'updateMask' => $this->fieldMask($data),
288287
'validateOnly' => $validateOnly
289288
]);
@@ -338,7 +337,7 @@ public function delete(array $options = [])
338337
public function resumeOperation($operationName, array $options = [])
339338
{
340339
return new LongRunningOperation(
341-
new LongRunningGapicConnection($this->instanceAdminClient, $this->serializer),
340+
new LongRunningClientConnection($this->instanceAdminClient, $this->serializer),
342341
$operationName,
343342
[
344343
[

0 commit comments

Comments
 (0)