PHPLIB-357: Refactor EstimatedDocumentCount Operation to encapsulate a Count Operation

parent a2609296
...@@ -17,11 +17,7 @@ ...@@ -17,11 +17,7 @@
namespace MongoDB\Operation; namespace MongoDB\Operation;
use MongoDB\Driver\Command;
use MongoDB\Driver\ReadConcern;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\Server; use MongoDB\Driver\Server;
use MongoDB\Driver\Session;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException; use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Exception\InvalidArgumentException; use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnexpectedValueException; use MongoDB\Exception\UnexpectedValueException;
...@@ -36,12 +32,10 @@ use MongoDB\Exception\UnsupportedException; ...@@ -36,12 +32,10 @@ use MongoDB\Exception\UnsupportedException;
*/ */
class EstimatedDocumentCount implements Executable, Explainable class EstimatedDocumentCount implements Executable, Explainable
{ {
private static $wireVersionForCollation = 5;
private static $wireVersionForReadConcern = 4;
private $databaseName; private $databaseName;
private $collectionName; private $collectionName;
private $options; private $options;
private $count;
/** /**
* Constructs a count command. * Constructs a count command.
...@@ -69,29 +63,11 @@ class EstimatedDocumentCount implements Executable, Explainable ...@@ -69,29 +63,11 @@ class EstimatedDocumentCount implements Executable, Explainable
*/ */
public function __construct($databaseName, $collectionName, array $options = []) public function __construct($databaseName, $collectionName, array $options = [])
{ {
if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) {
throw InvalidArgumentException::invalidType('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
}
if (isset($options['readConcern']) && ! $options['readConcern'] instanceof ReadConcern) {
throw InvalidArgumentException::invalidType('"readConcern" option', $options['readConcern'], ReadConcern::class);
}
if (isset($options['readPreference']) && ! $options['readPreference'] instanceof ReadPreference) {
throw InvalidArgumentException::invalidType('"readPreference" option', $options['readPreference'], ReadPreference::class);
}
if (isset($options['session']) && ! $options['session'] instanceof Session) {
throw InvalidArgumentException::invalidType('"session" option', $options['session'], Session::class);
}
if (isset($options['readConcern']) && $options['readConcern']->isDefault()) {
unset($options['readConcern']);
}
$this->databaseName = (string) $databaseName; $this->databaseName = (string) $databaseName;
$this->collectionName = (string) $collectionName; $this->collectionName = (string) $collectionName;
$this->options = $options; $this->options = array_intersect_key($options, ['maxTimeMS' => 1, 'readConcern' => 1, 'readPreference' => 1, 'session' => 1]);
$this->count = $this->createCount();
} }
/** /**
...@@ -106,73 +82,19 @@ class EstimatedDocumentCount implements Executable, Explainable ...@@ -106,73 +82,19 @@ class EstimatedDocumentCount implements Executable, Explainable
*/ */
public function execute(Server $server) public function execute(Server $server)
{ {
if (isset($this->options['collation']) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForCollation)) { return $this->count->execute($server);
throw UnsupportedException::collationNotSupported();
}
if (isset($this->options['readConcern']) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForReadConcern)) {
throw UnsupportedException::readConcernNotSupported();
}
$inTransaction = isset($this->options['session']) && $this->options['session']->isInTransaction();
if ($inTransaction && isset($this->options['readConcern'])) {
throw UnsupportedException::readConcernNotSupportedInTransaction();
}
$cursor = $server->executeReadCommand($this->databaseName, new Command($this->createCommandDocument()), $this->createOptions());
$result = current($cursor->toArray());
// Older server versions may return a float
if ( ! isset($result->n) || ! (is_integer($result->n) || is_float($result->n))) {
throw new UnexpectedValueException('count command did not return a numeric "n" value');
}
return (integer) $result->n;
} }
public function getCommandDocument(Server $server) public function getCommandDocument(Server $server)
{ {
return $this->createCommandDocument(); return $this->count->getCommandDocument($server);
} }
/** /**
* Create the count command document. * @return Count
*
* @return array
*/ */
private function createCommandDocument() private function createCount()
{ {
$cmd = ['count' => $this->collectionName]; return new Count($this->databaseName, $this->collectionName, [], $this->options);
if (isset($this->options['maxTimeMS'])) {
$cmd['maxTimeMS'] = $this->options['maxTimeMS'];
}
return $cmd;
}
/**
* Create options for executing the command.
*
* @see http://php.net/manual/en/mongodb-driver-server.executereadcommand.php
* @return array
*/
private function createOptions()
{
$options = [];
if (isset($this->options['readConcern'])) {
$options['readConcern'] = $this->options['readConcern'];
}
if (isset($this->options['readPreference'])) {
$options['readPreference'] = $this->options['readPreference'];
}
if (isset($this->options['session'])) {
$options['session'] = $this->options['session'];
}
return $options;
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment