diff --git a/Datastore/src/Operation.php b/Datastore/src/Operation.php index 872e834a6cee..2dfa38e66f99 100644 --- a/Datastore/src/Operation.php +++ b/Datastore/src/Operation.php @@ -689,32 +689,8 @@ public function commit(array $mutations, array $options = []) 'databaseId' => $this->databaseId, ]; - foreach ($mutations as &$mutation) { - $mutationType = array_keys($mutation)[0]; - $data = $mutation[$mutationType]; - if (isset($data['properties'])) { - foreach ($data['properties'] as &$property) { - list($type, $val) = $this->toGrpcValue($property); - - $property[$type] = $val; - } - } - - $mutation[$mutationType] = $data; - - $mutation = $this->serializer->decodeMessage(new Mutation(), $mutation); - } - - $options += [ - 'mode' => ($options['transaction']) ? Mode::TRANSACTIONAL : Mode::NON_TRANSACTIONAL, - 'mutations' => $mutations, - 'projectId' => $this->projectId, - ]; + $options = $this->parseCommitOptions($mutations, $options); - if (is_null($options['transaction'])) { - // Remove 'transaction' if set to `null` to avoid serialization error - unset($options['transaction']); - } list($data, $optionalArgs) = $this->splitOptionalArgs($options); // Remove redundant keys for request. @@ -1235,4 +1211,36 @@ private function parseRunAggregationQueryArgs($runQueryObj, $options) return $result; } + + private function parseCommitOptions($mutations, $options) + { + foreach ($mutations as &$mutation) { + $mutationType = array_keys($mutation)[0]; + $data = $mutation[$mutationType]; + if (isset($data['properties'])) { + foreach ($data['properties'] as &$property) { + list($type, $val) = $this->toGrpcValue($property); + + $property[$type] = $val; + } + } + + $mutation[$mutationType] = $data; + + $mutation = $this->serializer->decodeMessage(new Mutation(), $mutation); + } + + $options += [ + 'mode' => ($options['transaction']) ? Mode::TRANSACTIONAL : Mode::NON_TRANSACTIONAL, + 'mutations' => $mutations, + 'projectId' => $this->projectId, + ]; + + if (is_null($options['transaction'])) { + // Remove 'transaction' if set to `null` to avoid serialization error + unset($options['transaction']); + } + + return $options; + } }