Skip to content

Commit 5c26696

Browse files
committed
PHPLIB-68: Throw when accessing fields in unacknowledged write result
1 parent 42683dc commit 5c26696

11 files changed

+369
-44
lines changed

src/BulkWriteResult.php

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace MongoDB;
44

5-
use MongoDB\BSON\ObjectId;
65
use MongoDB\Driver\WriteResult;
6+
use MongoDB\Exception\BadMethodCallException;
77

88
/**
99
* Result class for a bulk write operation.
@@ -12,6 +12,7 @@ class BulkWriteResult
1212
{
1313
private $writeResult;
1414
private $insertedIds;
15+
private $isAcknowledged;
1516

1617
/**
1718
* Constructor.
@@ -23,41 +24,53 @@ public function __construct(WriteResult $writeResult, array $insertedIds)
2324
{
2425
$this->writeResult = $writeResult;
2526
$this->insertedIds = $insertedIds;
27+
$this->isAcknowledged = $writeResult->isAcknowledged();
2628
}
2729

2830
/**
2931
* Return the number of documents that were deleted.
3032
*
31-
* This value is undefined if the write was not acknowledged.
33+
* This method should only be called if the write was acknowledged.
3234
*
3335
* @see BulkWriteResult::isAcknowledged()
3436
* @return integer
37+
* @throws BadMethodCallException is the write result is unacknowledged
3538
*/
3639
public function getDeletedCount()
3740
{
38-
return $this->writeResult->getDeletedCount();
41+
if ($this->isAcknowledged) {
42+
return $this->writeResult->getDeletedCount();
43+
}
44+
45+
throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
3946
}
4047

4148
/**
4249
* Return the number of documents that were inserted.
4350
*
44-
* This value is undefined if the write was not acknowledged.
51+
* This method should only be called if the write was acknowledged.
4552
*
4653
* @see BulkWriteResult::isAcknowledged()
4754
* @return integer
55+
* @throws BadMethodCallException is the write result is unacknowledged
4856
*/
4957
public function getInsertedCount()
5058
{
51-
return $this->writeResult->getInsertedCount();
59+
if ($this->isAcknowledged) {
60+
return $this->writeResult->getInsertedCount();
61+
}
62+
63+
throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
5264
}
5365

5466
/**
5567
* Return a map of the inserted documents' IDs.
5668
*
57-
* The index of each ID in the map corresponds to the document's position
58-
* in bulk operation. If the document had an ID prior to insertion (i.e. the
59-
* driver did not generate an ID), this will contain its "_id" field value.
60-
* Any driver-generated ID will be an MongoDB\Driver\ObjectID instance.
69+
* The index of each ID in the map corresponds to the document's position in
70+
* the bulk operation. If the document had an ID prior to insertion (i.e.
71+
* the driver did not generate an ID), this will contain its "_id" field
72+
* value. Any driver-generated ID will be an MongoDB\Driver\ObjectID
73+
* instance.
6174
*
6275
* @return mixed[]
6376
*/
@@ -69,41 +82,58 @@ public function getInsertedIds()
6982
/**
7083
* Return the number of documents that were matched by the filter.
7184
*
72-
* This value is undefined if the write was not acknowledged.
85+
* This method should only be called if the write was acknowledged.
7386
*
7487
* @see BulkWriteResult::isAcknowledged()
7588
* @return integer
89+
* @throws BadMethodCallException is the write result is unacknowledged
7690
*/
7791
public function getMatchedCount()
7892
{
79-
return $this->writeResult->getMatchedCount();
93+
if ($this->isAcknowledged) {
94+
return $this->writeResult->getMatchedCount();
95+
}
96+
97+
throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
8098
}
8199

82100
/**
83101
* Return the number of documents that were modified.
84102
*
85-
* This value is undefined if the write was not acknowledged or if the write
86-
* executed as a legacy operation instead of write command.
103+
* This value is undefined (i.e. null) if the write executed as a legacy
104+
* operation instead of command.
105+
*
106+
* This method should only be called if the write was acknowledged.
87107
*
88108
* @see BulkWriteResult::isAcknowledged()
89109
* @return integer|null
110+
* @throws BadMethodCallException is the write result is unacknowledged
90111
*/
91112
public function getModifiedCount()
92113
{
93-
return $this->writeResult->getModifiedCount();
114+
if ($this->isAcknowledged) {
115+
return $this->writeResult->getModifiedCount();
116+
}
117+
118+
throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
94119
}
95120

96121
/**
97122
* Return the number of documents that were upserted.
98123
*
99-
* This value is undefined if the write was not acknowledged.
124+
* This method should only be called if the write was acknowledged.
100125
*
101126
* @see BulkWriteResult::isAcknowledged()
102127
* @return integer
128+
* @throws BadMethodCallException is the write result is unacknowledged
103129
*/
104130
public function getUpsertedCount()
105131
{
106-
return $this->writeResult->getUpsertedCount();
132+
if ($this->isAcknowledged) {
133+
return $this->writeResult->getUpsertedCount();
134+
}
135+
136+
throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
107137
}
108138

109139
/**
@@ -114,11 +144,19 @@ public function getUpsertedCount()
114144
* server did not need to generate an ID), this will contain its "_id". Any
115145
* server-generated ID will be an MongoDB\Driver\ObjectID instance.
116146
*
147+
* This method should only be called if the write was acknowledged.
148+
*
149+
* @see BulkWriteResult::isAcknowledged()
117150
* @return mixed[]
151+
* @throws BadMethodCallException is the write result is unacknowledged
118152
*/
119153
public function getUpsertedIds()
120154
{
121-
return $this->writeResult->getUpsertedIds();
155+
if ($this->isAcknowledged) {
156+
return $this->writeResult->getUpsertedIds();
157+
}
158+
159+
throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
122160
}
123161

124162
/**
@@ -131,6 +169,6 @@ public function getUpsertedIds()
131169
*/
132170
public function isAcknowledged()
133171
{
134-
return $this->writeResult->isAcknowledged();
172+
return $this->isAcknowledged;
135173
}
136174
}

src/DeleteResult.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
namespace MongoDB;
44

55
use MongoDB\Driver\WriteResult;
6+
use MongoDB\Exception\BadMethodCallException;
67

78
/**
89
* Result class for a delete operation.
910
*/
1011
class DeleteResult
1112
{
1213
private $writeResult;
14+
private $isAcknowledged;
1315

1416
/**
1517
* Constructor.
@@ -19,19 +21,25 @@ class DeleteResult
1921
public function __construct(WriteResult $writeResult)
2022
{
2123
$this->writeResult = $writeResult;
24+
$this->isAcknowledged = $writeResult->isAcknowledged();
2225
}
2326

2427
/**
2528
* Return the number of documents that were deleted.
2629
*
27-
* This value is undefined if the write was not acknowledged.
30+
* This method should only be called if the write was acknowledged.
2831
*
29-
* @see UpdateResult::isAcknowledged()
32+
* @see DeleteResult::isAcknowledged()
3033
* @return integer
34+
* @throws BadMethodCallException is the write result is unacknowledged
3135
*/
3236
public function getDeletedCount()
3337
{
34-
return $this->writeResult->getDeletedCount();
38+
if ($this->isAcknowledged) {
39+
return $this->writeResult->getDeletedCount();
40+
}
41+
42+
throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
3543
}
3644

3745
/**
@@ -44,6 +52,6 @@ public function getDeletedCount()
4452
*/
4553
public function isAcknowledged()
4654
{
47-
return $this->writeResult->isAcknowledged();
55+
return $this->isAcknowledged;
4856
}
4957
}

src/Exception/BadMethodCallException.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,11 @@
44

55
class BadMethodCallException extends \BadMethodCallException implements Exception
66
{
7+
/**
8+
* Thrown when accessing a result field on an unacknowledged write result.
9+
*/
10+
public static function unacknowledgedWriteResultAccess($method)
11+
{
12+
return new static(sprintf('%s should not be called for an unacknowledged write result', $method));
13+
}
714
}

src/InsertManyResult.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
namespace MongoDB;
44

55
use MongoDB\Driver\WriteResult;
6+
use MongoDB\Exception\BadMethodCallException;
67

78
/**
8-
* Result class for a multi-document write operation.
9+
* Result class for a multi-document insert operation.
910
*/
1011
class InsertManyResult
1112
{
1213
private $writeResult;
1314
private $insertedIds;
15+
private $isAcknowledged;
1416

1517
/**
1618
* Constructor.
@@ -22,28 +24,35 @@ public function __construct(WriteResult $writeResult, array $insertedIds)
2224
{
2325
$this->writeResult = $writeResult;
2426
$this->insertedIds = $insertedIds;
27+
$this->isAcknowledged = $writeResult->isAcknowledged();
2528
}
2629

2730
/**
2831
* Return the number of documents that were inserted.
2932
*
30-
* This value is undefined if the write was not acknowledged.
33+
* This method should only be called if the write was acknowledged.
3134
*
3235
* @see InsertManyResult::isAcknowledged()
3336
* @return integer
37+
* @throws BadMethodCallException is the write result is unacknowledged
3438
*/
3539
public function getInsertedCount()
3640
{
37-
return $this->writeResult->getInsertedCount();
41+
if ($this->isAcknowledged) {
42+
return $this->writeResult->getInsertedCount();
43+
}
44+
45+
throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
3846
}
3947

4048
/**
4149
* Return a map of the inserted documents' IDs.
4250
*
43-
* The index of each ID in the map corresponds to the document's position
44-
* in bulk operation. If the document had an ID prior to insertion (i.e. the
45-
* driver did not generate an ID), this will contain its "_id" field value.
46-
* Any driver-generated ID will be an MongoDB\Driver\ObjectID instance.
51+
* The index of each ID in the map corresponds to the document's position in
52+
* the bulk operation. If the document had an ID prior to insertion (i.e.
53+
* the driver did not generate an ID), this will contain its "_id" field
54+
* value. Any driver-generated ID will be an MongoDB\Driver\ObjectID
55+
* instance.
4756
*
4857
* @return mixed[]
4958
*/

src/InsertOneResult.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace MongoDB;
44

55
use MongoDB\Driver\WriteResult;
6+
use MongoDB\Exception\BadMethodCallException;
67

78
/**
89
* Result class for a single-document insert operation.
@@ -11,6 +12,7 @@ class InsertOneResult
1112
{
1213
private $writeResult;
1314
private $insertedId;
15+
private $isAcknowledged;
1416

1517
/**
1618
* Constructor.
@@ -22,19 +24,25 @@ public function __construct(WriteResult $writeResult, $insertedId)
2224
{
2325
$this->writeResult = $writeResult;
2426
$this->insertedId = $insertedId;
27+
$this->isAcknowledged = $writeResult->isAcknowledged();
2528
}
2629

2730
/**
2831
* Return the number of documents that were inserted.
2932
*
30-
* This value is undefined if the write was not acknowledged.
33+
* This method should only be called if the write was acknowledged.
3134
*
3235
* @see InsertOneResult::isAcknowledged()
3336
* @return integer
37+
* @throws BadMethodCallException is the write result is unacknowledged
3438
*/
3539
public function getInsertedCount()
3640
{
37-
return $this->writeResult->getInsertedCount();
41+
if ($this->isAcknowledged) {
42+
return $this->writeResult->getInsertedCount();
43+
}
44+
45+
throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
3846
}
3947

4048
/**
@@ -57,6 +65,10 @@ public function getInsertedId()
5765
* If the insert was not acknowledged, other fields from the WriteResult
5866
* (e.g. insertedCount) will be undefined.
5967
*
68+
* If the insert was not acknowledged, other fields from the WriteResult
69+
* (e.g. insertedCount) will be undefined and their getter methods should
70+
* not be invoked.
71+
*
6072
* @return boolean
6173
*/
6274
public function isAcknowledged()

0 commit comments

Comments
 (0)