Skip to content

Commit a006402

Browse files
committed
Extract DropIndexes operation class
1 parent c167653 commit a006402

File tree

2 files changed

+77
-17
lines changed

2 files changed

+77
-17
lines changed

src/Collection.php

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use MongoDB\Operation\Count;
2020
use MongoDB\Operation\Distinct;
2121
use MongoDB\Operation\DropCollection;
22+
use MongoDB\Operation\DropIndexes;
2223
use MongoDB\Operation\FindOneAndDelete;
2324
use MongoDB\Operation\FindOneAndReplace;
2425
use MongoDB\Operation\FindOneAndUpdate;
@@ -366,43 +367,35 @@ public function drop()
366367
/**
367368
* Drop a single index in the collection.
368369
*
369-
* @see http://docs.mongodb.org/manual/reference/command/dropIndexes/
370-
* @see http://docs.mongodb.org/manual/reference/method/db.collection.dropIndex/
371-
* @param string $indexName
372-
* @return Cursor
370+
* @param string $indexName Index name
371+
* @return object Command result document
373372
* @throws InvalidArgumentException if $indexName is an empty string or "*"
374373
*/
375374
public function dropIndex($indexName)
376375
{
377376
$indexName = (string) $indexName;
378377

379-
if ($indexName === '') {
380-
throw new InvalidArgumentException('Index name cannot be empty');
381-
}
382-
383378
if ($indexName === '*') {
384379
throw new InvalidArgumentException('dropIndexes() must be used to drop multiple indexes');
385380
}
386381

387-
$command = new Command(array('dropIndexes' => $this->collname, 'index' => $indexName));
388-
$readPreference = new ReadPreference(ReadPreference::RP_PRIMARY);
382+
$operation = new DropIndexes($this->dbname, $this->collname, $indexName);
383+
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
389384

390-
return $this->manager->executeCommand($this->dbname, $command, $readPreference);
385+
return $operation->execute($server);
391386
}
392387

393388
/**
394389
* Drop all indexes in the collection.
395390
*
396-
* @see http://docs.mongodb.org/manual/reference/command/dropIndexes/
397-
* @see http://docs.mongodb.org/manual/reference/method/db.collection.dropIndexes/
398-
* @return Cursor
391+
* @return object Command result document
399392
*/
400393
public function dropIndexes()
401394
{
402-
$command = new Command(array('dropIndexes' => $this->collname, 'index' => '*'));
403-
$readPreference = new ReadPreference(ReadPreference::RP_PRIMARY);
395+
$operation = new DropIndexes($this->dbname, $this->collname, '*');
396+
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
404397

405-
return $this->manager->executeCommand($this->dbname, $command, $readPreference);
398+
return $operation->execute($server);
406399
}
407400

408401
/**

src/Operation/DropIndexes.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace MongoDB\Operation;
4+
5+
use MongoDB\Driver\Command;
6+
use MongoDB\Driver\Server;
7+
use MongoDB\Exception\InvalidArgumentException;
8+
use MongoDB\Exception\RuntimeException;
9+
10+
/**
11+
* Operation for the dropIndexes command.
12+
*
13+
* @api
14+
* @see MongoDB\Collection::dropIndexes()
15+
* @see http://docs.mongodb.org/manual/reference/command/dropIndexes/
16+
*/
17+
class DropIndexes implements Executable
18+
{
19+
private $databaseName;
20+
private $collectionName;
21+
private $indexName;
22+
23+
/**
24+
* Constructs a dropIndexes command.
25+
*
26+
* @param string $databaseName Database name
27+
* @param string $collectionName Collection name
28+
* @param string $indexName Index name (use "*" to drop all indexes)
29+
* @throws InvalidArgumentException
30+
*/
31+
public function __construct($databaseName, $collectionName, $indexName)
32+
{
33+
$indexName = (string) $indexName;
34+
35+
if ($indexName === '') {
36+
throw new InvalidArgumentException('$indexName cannot be empty');
37+
}
38+
39+
$this->databaseName = (string) $databaseName;
40+
$this->collectionName = (string) $collectionName;
41+
$this->indexName = $indexName;
42+
}
43+
44+
/**
45+
* Execute the operation.
46+
*
47+
* @see Executable::execute()
48+
* @param Server $server
49+
* @return object Command result document
50+
*/
51+
public function execute(Server $server)
52+
{
53+
$cmd = array(
54+
'dropIndexes' => $this->collectionName,
55+
'index' => $this->indexName,
56+
);
57+
58+
$cursor = $server->executeCommand($this->databaseName, new Command($cmd));
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+
return $result;
66+
}
67+
}

0 commit comments

Comments
 (0)