Skip to content

Commit 2e064dd

Browse files
committed
Extract DropDatabase operation class
1 parent 9d7656e commit 2e064dd

File tree

3 files changed

+60
-11
lines changed

3 files changed

+60
-11
lines changed

src/Client.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use MongoDB\Driver\ReadPreference;
99
use MongoDB\Driver\WriteConcern;
1010
use MongoDB\Model\DatabaseInfoIterator;
11+
use MongoDB\Operation\DropDatabase;
1112
use MongoDB\Operation\ListDatabases;
1213

1314
class Client
@@ -36,17 +37,15 @@ public function __construct($uri, array $options = array(), array $driverOptions
3637
/**
3738
* Drop a database.
3839
*
39-
* @see http://docs.mongodb.org/manual/reference/command/dropDatabase/
4040
* @param string $databaseName
41-
* @return Cursor
41+
* @return object Command result document
4242
*/
4343
public function dropDatabase($databaseName)
4444
{
45-
$databaseName = (string) $databaseName;
46-
$command = new Command(array('dropDatabase' => 1));
47-
$readPreference = new ReadPreference(ReadPreference::RP_PRIMARY);
45+
$operation = new DropDatabase($databaseName);
46+
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
4847

49-
return $this->manager->executeCommand($databaseName, $command, $readPreference);
48+
return $operation->execute($server);
5049
}
5150

5251
/**

src/Database.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use MongoDB\Driver\WriteConcern;
1313
use MongoDB\Exception\InvalidArgumentException;
1414
use MongoDB\Model\CollectionInfoIterator;
15+
use MongoDB\Operation\DropDatabase;
1516
use MongoDB\Operation\ListCollections;
1617

1718
class Database
@@ -71,15 +72,14 @@ public function createCollection($collectionName, array $options = array())
7172
/**
7273
* Drop this database.
7374
*
74-
* @see http://docs.mongodb.org/manual/reference/command/dropDatabase/
75-
* @return Cursor
75+
* @return object Command result document
7676
*/
7777
public function drop()
7878
{
79-
$command = new Command(array('dropDatabase' => 1));
80-
$readPreference = new ReadPreference(ReadPreference::RP_PRIMARY);
79+
$operation = new DropDatabase($this->databaseName);
80+
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
8181

82-
return $this->manager->executeCommand($this->databaseName, $command, $readPreference);
82+
return $operation->execute($server);
8383
}
8484

8585
/**

src/Operation/DropDatabase.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace MongoDB\Operation;
4+
5+
use MongoDB\Driver\Command;
6+
use MongoDB\Driver\Server;
7+
use MongoDB\Exception\RuntimeException;
8+
9+
/**
10+
* Operation for the dropDatabase command.
11+
*
12+
* @api
13+
* @see MongoDB\Client::dropDatabase()
14+
* @see MongoDB\Database::drop()
15+
* @see http://docs.mongodb.org/manual/reference/command/dropDatabase/
16+
*/
17+
class DropDatabase implements Executable
18+
{
19+
private $databaseName;
20+
21+
/**
22+
* Constructs a dropDatabase command.
23+
*
24+
* @param string $databaseName Database name
25+
* @param string $collectionName Collection name
26+
*/
27+
public function __construct($databaseName)
28+
{
29+
$this->databaseName = (string) $databaseName;
30+
}
31+
32+
/**
33+
* Execute the operation.
34+
*
35+
* @see Executable::execute()
36+
* @param Server $server
37+
* @return object Command result document
38+
*/
39+
public function execute(Server $server)
40+
{
41+
$cursor = $server->executeCommand($this->databaseName, new Command(array('dropDatabase' => 1)));
42+
$result = current($cursor->toArray());
43+
44+
if (empty($result['ok'])) {
45+
throw new RuntimeException(isset($result['errmsg']) ? $result['errmsg'] : 'Unknown error');
46+
}
47+
48+
return $result;
49+
}
50+
}

0 commit comments

Comments
 (0)