Skip to content

Commit ed59ff7

Browse files
committed
Sanity check Manager association in BulkWriteCommandBuilder
1 parent 2aad228 commit ed59ff7

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/BulkWriteCommandBuilder.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use MongoDB\Codec\DocumentCodec;
2121
use MongoDB\Codec\Encoder;
2222
use MongoDB\Driver\BulkWriteCommand;
23+
use MongoDB\Driver\Manager;
2324
use MongoDB\Exception\InvalidArgumentException;
2425

2526
use function is_array;
@@ -30,6 +31,7 @@
3031
{
3132
private function __construct(
3233
public BulkWriteCommand $bulkWriteCommand,
34+
private Manager $manager,
3335
private string $namespace,
3436
private Encoder $builderEncoder,
3537
private ?DocumentCodec $codec,
@@ -58,6 +60,7 @@ public static function createWithCollection(Collection $collection, array $optio
5860

5961
return new self(
6062
new BulkWriteCommand($options),
63+
$collection->getManager(),
6164
$collection->getNamespace(),
6265
$collection->getBuilderEncoder(),
6366
$collection->getCodec(),
@@ -66,8 +69,18 @@ public static function createWithCollection(Collection $collection, array $optio
6669

6770
public function withCollection(Collection $collection): self
6871
{
72+
/* Prohibit mixing Collections associated with different Manager
73+
* objects. This is not technically necessary, since the Collection is
74+
* only used to derive a namespace and encoding options; however, it
75+
* may prevent a user from inadvertently mixing writes destined for
76+
* different deployments. */
77+
if ($this->manager !== $collection->getManager()) {
78+
throw new InvalidArgumentException('$collection is associated with a different MongoDB\Driver\Manager');
79+
}
80+
6981
return new self(
7082
$this->bulkWriteCommand,
83+
$this->manager,
7184
$collection->getNamespace(),
7285
$collection->getBuilderEncoder(),
7386
$collection->getCodec(),

0 commit comments

Comments
 (0)