Skip to content

Commit c3a3164

Browse files
committed
Split inheritReadOptions method
1 parent afed4bd commit c3a3164

File tree

1 file changed

+27
-31
lines changed

1 file changed

+27
-31
lines changed

src/Collection.php

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -213,25 +213,17 @@ public function aggregate(array $pipeline, array $options = [])
213213
{
214214
$hasWriteStage = is_last_pipeline_operator_write($pipeline);
215215

216-
if (! isset($options['readPreference']) && ! is_in_transaction($options)) {
217-
$options['readPreference'] = $this->readPreference;
218-
}
216+
$options = $this->inheritReadPreference($options);
219217

220218
$server = $hasWriteStage
221219
? select_server_for_aggregate_write_stage($this->manager, $options)
222220
: select_server($this->manager, $options);
223221

224222
/* MongoDB 4.2 and later supports a read concern when an $out stage is
225223
* being used, but earlier versions do not.
226-
*
227-
* A read concern is also not compatible with transactions.
228224
*/
229-
if (
230-
! isset($options['readConcern']) &&
231-
! is_in_transaction($options) &&
232-
( ! $hasWriteStage || server_supports_feature($server, self::WIRE_VERSION_FOR_READ_CONCERN_WITH_WRITE_STAGE))
233-
) {
234-
$options['readConcern'] = $this->readConcern;
225+
if (! $hasWriteStage || server_supports_feature($server, self::WIRE_VERSION_FOR_READ_CONCERN_WITH_WRITE_STAGE)) {
226+
$options = $this->inheritReadConcern($options);
235227
}
236228

237229
$options = $this->inheritCodecOrTypeMap($options);
@@ -543,10 +535,7 @@ public function estimatedDocumentCount(array $options = [])
543535
*/
544536
public function explain(Explainable $explainable, array $options = [])
545537
{
546-
if (! isset($options['readPreference']) && ! is_in_transaction($options)) {
547-
$options['readPreference'] = $this->readPreference;
548-
}
549-
538+
$options = $this->inheritReadPreference($options);
550539
$options = $this->inheritTypeMap($options);
551540

552541
$operation = new Explain($this->databaseName, $explainable, $options);
@@ -842,22 +831,18 @@ public function mapReduce(JavascriptInterface $map, JavascriptInterface $reduce,
842831
{
843832
$hasOutputCollection = ! is_mapreduce_output_inline($out);
844833

845-
if (! isset($options['readPreference']) && ! is_in_transaction($options)) {
846-
$options['readPreference'] = $this->readPreference;
847-
}
848-
849834
// Check if the out option is inline because we will want to coerce a primary read preference if not
850835
if ($hasOutputCollection) {
851836
$options['readPreference'] = new ReadPreference(ReadPreference::PRIMARY);
837+
} else {
838+
$options = $this->inheritReadPreference($options);
852839
}
853840

854841
/* A "majority" read concern is not compatible with inline output, so
855842
* avoid providing the Collection's read concern if it would conflict.
856-
*
857-
* A read concern is also not compatible with transactions.
858843
*/
859-
if (! isset($options['readConcern']) && ! ($hasOutputCollection && $this->readConcern->getLevel() === ReadConcern::MAJORITY) && ! is_in_transaction($options)) {
860-
$options['readConcern'] = $this->readConcern;
844+
if (! $hasOutputCollection || $this->readConcern->getLevel() !== ReadConcern::MAJORITY) {
845+
$options = $this->inheritReadConcern($options);
861846
}
862847

863848
$options = $this->inheritWriteOptions($options);
@@ -1035,17 +1020,28 @@ private function inheritCodecOrTypeMap(array $options): array
10351020
return $options;
10361021
}
10371022

1038-
private function inheritReadOptions(array $options): array
1023+
private function inheritReadConcern(array $options): array
10391024
{
10401025
// ReadConcern and ReadPreference may not change within a transaction
1041-
if (! is_in_transaction($options)) {
1042-
if (! isset($options['readConcern'])) {
1043-
$options['readConcern'] = $this->readConcern;
1044-
}
1026+
if (! isset($options['readConcern']) && ! is_in_transaction($options)) {
1027+
$options['readConcern'] = $this->readConcern;
1028+
}
10451029

1046-
if (! isset($options['readPreference'])) {
1047-
$options['readPreference'] = $this->readPreference;
1048-
}
1030+
return $options;
1031+
}
1032+
1033+
private function inheritReadOptions(array $options): array
1034+
{
1035+
$options = $this->inheritReadConcern($options);
1036+
1037+
return $this->inheritReadPreference($options);
1038+
}
1039+
1040+
private function inheritReadPreference(array $options): array
1041+
{
1042+
// ReadConcern and ReadPreference may not change within a transaction
1043+
if (! isset($options['readPreference']) && ! is_in_transaction($options)) {
1044+
$options['readPreference'] = $this->readPreference;
10491045
}
10501046

10511047
return $options;

0 commit comments

Comments
 (0)