Skip to content

Commit a793f15

Browse files
committed
Extra DropCollection operation class
1 parent ac11c54 commit a793f15

File tree

3 files changed

+62
-11
lines changed

3 files changed

+62
-11
lines changed

src/Collection.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use MongoDB\Operation\CreateIndexes;
1919
use MongoDB\Operation\Count;
2020
use MongoDB\Operation\Distinct;
21+
use MongoDB\Operation\DropCollection;
2122
use MongoDB\Operation\FindOneAndDelete;
2223
use MongoDB\Operation\FindOneAndReplace;
2324
use MongoDB\Operation\FindOneAndUpdate;
@@ -352,15 +353,14 @@ public function distinct($fieldName, array $filter = array(), array $options = a
352353
/**
353354
* Drop this collection.
354355
*
355-
* @see http://docs.mongodb.org/manual/reference/command/drop/
356-
* @return Cursor
356+
* @return object Command result document
357357
*/
358358
public function drop()
359359
{
360-
$command = new Command(array('drop' => $this->collname));
361-
$readPreference = new ReadPreference(ReadPreference::RP_PRIMARY);
360+
$operation = new DropCollection($this->dbname, $this->collname);
361+
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
362362

363-
return $this->manager->executeCommand($this->dbname, $command, $readPreference);
363+
return $operation->execute($server);
364364
}
365365

366366
/**

src/Database.php

Lines changed: 5 additions & 6 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\DropCollection;
1516
use MongoDB\Operation\DropDatabase;
1617
use MongoDB\Operation\ListCollections;
1718

@@ -85,17 +86,15 @@ public function drop()
8586
/**
8687
* Drop a collection within this database.
8788
*
88-
* @see http://docs.mongodb.org/manual/reference/command/drop/
8989
* @param string $collectionName
90-
* @return Cursor
90+
* @return object Command result document
9191
*/
9292
public function dropCollection($collectionName)
9393
{
94-
$collectionName = (string) $collectionName;
95-
$command = new Command(array('drop' => $collectionName));
96-
$readPreference = new ReadPreference(ReadPreference::RP_PRIMARY);
94+
$operation = new DropCollection($this->databaseName, $collectionName);
95+
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
9796

98-
return $this->manager->executeCommand($this->databaseName, $command, $readPreference);
97+
return $operation->execute($server);
9998
}
10099

101100
/**

src/Operation/DropCollection.php

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

0 commit comments

Comments
 (0)