Skip to content

Commit b90062c

Browse files
committed
Extract Client::listDatabases() to an operation class
1 parent 53075f0 commit b90062c

File tree

2 files changed

+83
-21
lines changed

2 files changed

+83
-21
lines changed

src/Client.php

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
use MongoDB\Driver\Manager;
88
use MongoDB\Driver\ReadPreference;
99
use MongoDB\Driver\WriteConcern;
10-
use MongoDB\Exception\UnexpectedValueException;
1110
use MongoDB\Model\DatabaseInfoIterator;
12-
use MongoDB\Model\DatabaseInfoLegacyIterator;
11+
use MongoDB\Operation\ListDatabases;
1312

1413
class Client
1514
{
@@ -53,29 +52,15 @@ public function dropDatabase($databaseName)
5352
/**
5453
* List databases.
5554
*
56-
* @see http://docs.mongodb.org/manual/reference/command/listDatabases/
55+
* @see ListDatabases::__construct() for supported options
5756
* @return DatabaseInfoIterator
58-
* @throws UnexpectedValueException if the command result is malformed
5957
*/
60-
public function listDatabases()
58+
public function listDatabases(array $options = array())
6159
{
62-
$command = new Command(array('listDatabases' => 1));
60+
$operation = new ListDatabases($options);
61+
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
6362

64-
$cursor = $this->manager->executeCommand('admin', $command);
65-
$cursor->setTypeMap(array('document' => 'array'));
66-
$result = current($cursor->toArray());
67-
68-
if ( ! isset($result['databases']) || ! is_array($result['databases'])) {
69-
throw new UnexpectedValueException('listDatabases command did not return a "databases" array');
70-
}
71-
72-
/* Return an Iterator instead of an array in case listDatabases is
73-
* eventually changed to return a command cursor, like the collection
74-
* and index enumeration commands. This makes the "totalSize" command
75-
* field inaccessible, but users can manually invoke the command if they
76-
* need that value.
77-
*/
78-
return new DatabaseInfoLegacyIterator($result['databases']);
63+
return $operation->execute($server);
7964
}
8065

8166
/**

src/Operation/ListDatabases.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace MongoDB\Operation;
4+
5+
use MongoDB\Driver\Command;
6+
use MongoDB\Driver\Server;
7+
use MongoDB\Exception\RuntimeException;
8+
use MongoDB\Exception\UnexpectedValueException;
9+
use MongoDB\Model\DatabaseInfoIterator;
10+
use MongoDB\Model\DatabaseInfoLegacyIterator;
11+
12+
/**
13+
* Operation for the ListDatabases command.
14+
*
15+
* @api
16+
* @see MongoDB\Client::listDatabases()
17+
* @see http://docs.mongodb.org/manual/reference/command/ListDatabases/
18+
*/
19+
class ListDatabases implements Executable
20+
{
21+
private $options;
22+
23+
/**
24+
* Constructs a listDatabases command.
25+
*
26+
* Supported options:
27+
*
28+
* * maxTimeMS (integer): The maximum amount of time to allow the query to
29+
* run.
30+
*
31+
* @param array $options Command options
32+
*/
33+
public function __construct(array $options = array())
34+
{
35+
if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) {
36+
throw new InvalidArgumentTypeException('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
37+
}
38+
39+
$this->options = $options;
40+
}
41+
42+
/**
43+
* Execute the operation.
44+
*
45+
* @see Executable::execute()
46+
* @param Server $server
47+
* @return DatabaseInfoIterator
48+
*/
49+
public function execute(Server $server)
50+
{
51+
$cmd = array('listDatabases' => 1);
52+
53+
if (isset($this->options['maxTimeMS'])) {
54+
$cmd['maxTimeMS'] = $this->options['maxTimeMS'];
55+
}
56+
57+
$cursor = $server->executeCommand('admin', new Command($cmd));
58+
$cursor->setTypeMap(array('document' => 'array'));
59+
$result = current($cursor->toArray());
60+
61+
if (empty($result['ok'])) {
62+
throw new RuntimeException(isset($result['errmsg']) ? $result['errmsg'] : 'Unknown error');
63+
}
64+
65+
if ( ! isset($result['databases']) || ! is_array($result['databases'])) {
66+
throw new UnexpectedValueException('listDatabases command did not return a "databases" array');
67+
}
68+
69+
/* Return an Iterator instead of an array in case listDatabases is
70+
* eventually changed to return a command cursor, like the collection
71+
* and index enumeration commands. This makes the "totalSize" command
72+
* field inaccessible, but users can manually invoke the command if they
73+
* need that value.
74+
*/
75+
return new DatabaseInfoLegacyIterator($result['databases']);
76+
}
77+
}

0 commit comments

Comments
 (0)