Skip to content

Commit 2b1c1ec

Browse files
committed
PHPLIB-357: Refactor EstimatedDocumentCount Operation to encapsulate a Count Operation
1 parent a260929 commit 2b1c1ec

File tree

1 file changed

+9
-87
lines changed

1 file changed

+9
-87
lines changed

src/Operation/EstimatedDocumentCount.php

Lines changed: 9 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@
1717

1818
namespace MongoDB\Operation;
1919

20-
use MongoDB\Driver\Command;
21-
use MongoDB\Driver\ReadConcern;
22-
use MongoDB\Driver\ReadPreference;
2320
use MongoDB\Driver\Server;
24-
use MongoDB\Driver\Session;
2521
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
2622
use MongoDB\Exception\InvalidArgumentException;
2723
use MongoDB\Exception\UnexpectedValueException;
@@ -36,12 +32,10 @@
3632
*/
3733
class EstimatedDocumentCount implements Executable, Explainable
3834
{
39-
private static $wireVersionForCollation = 5;
40-
private static $wireVersionForReadConcern = 4;
41-
4235
private $databaseName;
4336
private $collectionName;
4437
private $options;
38+
private $count;
4539

4640
/**
4741
* Constructs a count command.
@@ -69,29 +63,11 @@ class EstimatedDocumentCount implements Executable, Explainable
6963
*/
7064
public function __construct($databaseName, $collectionName, array $options = [])
7165
{
72-
if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) {
73-
throw InvalidArgumentException::invalidType('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
74-
}
75-
76-
if (isset($options['readConcern']) && ! $options['readConcern'] instanceof ReadConcern) {
77-
throw InvalidArgumentException::invalidType('"readConcern" option', $options['readConcern'], ReadConcern::class);
78-
}
79-
80-
if (isset($options['readPreference']) && ! $options['readPreference'] instanceof ReadPreference) {
81-
throw InvalidArgumentException::invalidType('"readPreference" option', $options['readPreference'], ReadPreference::class);
82-
}
83-
84-
if (isset($options['session']) && ! $options['session'] instanceof Session) {
85-
throw InvalidArgumentException::invalidType('"session" option', $options['session'], Session::class);
86-
}
87-
88-
if (isset($options['readConcern']) && $options['readConcern']->isDefault()) {
89-
unset($options['readConcern']);
90-
}
91-
9266
$this->databaseName = (string) $databaseName;
9367
$this->collectionName = (string) $collectionName;
94-
$this->options = $options;
68+
$this->options = array_intersect_key($options, ['maxTimeMS' => 1, 'readConcern' => 1, 'readPreference' => 1, 'session' => 1]);
69+
70+
$this->count = $this->createCount();
9571
}
9672

9773
/**
@@ -106,73 +82,19 @@ public function __construct($databaseName, $collectionName, array $options = [])
10682
*/
10783
public function execute(Server $server)
10884
{
109-
if (isset($this->options['collation']) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForCollation)) {
110-
throw UnsupportedException::collationNotSupported();
111-
}
112-
113-
if (isset($this->options['readConcern']) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForReadConcern)) {
114-
throw UnsupportedException::readConcernNotSupported();
115-
}
116-
117-
$inTransaction = isset($this->options['session']) && $this->options['session']->isInTransaction();
118-
if ($inTransaction && isset($this->options['readConcern'])) {
119-
throw UnsupportedException::readConcernNotSupportedInTransaction();
120-
}
121-
122-
$cursor = $server->executeReadCommand($this->databaseName, new Command($this->createCommandDocument()), $this->createOptions());
123-
$result = current($cursor->toArray());
124-
125-
// Older server versions may return a float
126-
if ( ! isset($result->n) || ! (is_integer($result->n) || is_float($result->n))) {
127-
throw new UnexpectedValueException('count command did not return a numeric "n" value');
128-
}
129-
130-
return (integer) $result->n;
85+
return $this->count->execute($server);
13186
}
13287

13388
public function getCommandDocument(Server $server)
13489
{
135-
return $this->createCommandDocument();
90+
return $this->count->getCommandDocument($server);
13691
}
13792

13893
/**
139-
* Create the count command document.
140-
*
141-
* @return array
94+
* @return Count
14295
*/
143-
private function createCommandDocument()
96+
private function createCount()
14497
{
145-
$cmd = ['count' => $this->collectionName];
146-
147-
if (isset($this->options['maxTimeMS'])) {
148-
$cmd['maxTimeMS'] = $this->options['maxTimeMS'];
149-
}
150-
151-
return $cmd;
152-
}
153-
154-
/**
155-
* Create options for executing the command.
156-
*
157-
* @see http://php.net/manual/en/mongodb-driver-server.executereadcommand.php
158-
* @return array
159-
*/
160-
private function createOptions()
161-
{
162-
$options = [];
163-
164-
if (isset($this->options['readConcern'])) {
165-
$options['readConcern'] = $this->options['readConcern'];
166-
}
167-
168-
if (isset($this->options['readPreference'])) {
169-
$options['readPreference'] = $this->options['readPreference'];
170-
}
171-
172-
if (isset($this->options['session'])) {
173-
$options['session'] = $this->options['session'];
174-
}
175-
176-
return $options;
98+
return new Count($this->databaseName, $this->collectionName, [], $this->options);
17799
}
178100
}

0 commit comments

Comments
 (0)