DropCollection.php 5.51 KB
Newer Older
1
<?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright 2015-2017 MongoDB, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
17 18 19 20

namespace MongoDB\Operation;

use MongoDB\Driver\Command;
21
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
22
use MongoDB\Driver\Server;
23
use MongoDB\Driver\Session;
24
use MongoDB\Driver\WriteConcern;
25
use MongoDB\Exception\InvalidArgumentException;
26
use MongoDB\Exception\UnsupportedException;
27 28 29
use function current;
use function is_array;
use function MongoDB\server_supports_feature;
30 31 32 33 34

/**
 * Operation for the drop command.
 *
 * @api
35 36
 * @see \MongoDB\Collection::drop()
 * @see \MongoDB\Database::dropCollection()
37 38 39 40
 * @see http://docs.mongodb.org/manual/reference/command/drop/
 */
class DropCollection implements Executable
{
41
    /** @var string */
42
    private static $errorMessageNamespaceNotFound = 'ns not found';
43 44

    /** @var integer */
45
    private static $wireVersionForWriteConcern = 5;
46

47
    /** @var string */
48
    private $databaseName;
49 50

    /** @var string */
51
    private $collectionName;
52 53

    /** @var array */
54
    private $options;
55 56 57 58

    /**
     * Constructs a drop command.
     *
59 60
     * Supported options:
     *
61 62 63 64
     *  * session (MongoDB\Driver\Session): Client session.
     *
     *    Sessions are not supported for server versions < 3.6.
     *
65 66 67
     *  * typeMap (array): Type map for BSON deserialization. This will be used
     *    for the returned command result document.
     *
68 69 70 71 72
     *  * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
     *
     *    This is not supported for server versions < 3.4 and will result in an
     *    exception at execution time if used.
     *
73 74
     * @param string $databaseName   Database name
     * @param string $collectionName Collection name
75
     * @param array  $options        Command options
76
     * @throws InvalidArgumentException for parameter/option parsing errors
77
     */
78
    public function __construct($databaseName, $collectionName, array $options = [])
79
    {
80
        if (isset($options['session']) && ! $options['session'] instanceof Session) {
81
            throw InvalidArgumentException::invalidType('"session" option', $options['session'], Session::class);
82 83
        }

84 85 86 87
        if (isset($options['typeMap']) && ! is_array($options['typeMap'])) {
            throw InvalidArgumentException::invalidType('"typeMap" option', $options['typeMap'], 'array');
        }

88
        if (isset($options['writeConcern']) && ! $options['writeConcern'] instanceof WriteConcern) {
89
            throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], WriteConcern::class);
90 91
        }

92 93 94 95
        if (isset($options['writeConcern']) && $options['writeConcern']->isDefault()) {
            unset($options['writeConcern']);
        }

96 97
        $this->databaseName = (string) $databaseName;
        $this->collectionName = (string) $collectionName;
98
        $this->options = $options;
99 100 101 102 103 104 105
    }

    /**
     * Execute the operation.
     *
     * @see Executable::execute()
     * @param Server $server
106
     * @return array|object Command result document
107
     * @throws UnsupportedException if writeConcern is used and unsupported
108
     * @throws DriverRuntimeException for other driver errors (e.g. connection errors)
109 110 111
     */
    public function execute(Server $server)
    {
112
        if (isset($this->options['writeConcern']) && ! server_supports_feature($server, self::$wireVersionForWriteConcern)) {
113 114 115
            throw UnsupportedException::writeConcernNotSupported();
        }

116 117 118 119 120
        $inTransaction = isset($this->options['session']) && $this->options['session']->isInTransaction();
        if ($inTransaction && isset($this->options['writeConcern'])) {
            throw UnsupportedException::writeConcernNotSupportedInTransaction();
        }

121 122
        $command = new Command(['drop' => $this->collectionName]);

123
        try {
124
            $cursor = $server->executeWriteCommand($this->databaseName, $command, $this->createOptions());
125
        } catch (DriverRuntimeException $e) {
126 127 128 129 130
            /* The server may return an error if the collection does not exist.
             * Check for an error message (unfortunately, there isn't a code)
             * and NOP instead of throwing.
             */
            if ($e->getMessage() === self::$errorMessageNamespaceNotFound) {
131
                return (object) ['ok' => 0, 'errmsg' => self::$errorMessageNamespaceNotFound];
132 133 134 135 136
            }

            throw $e;
        }

137 138 139 140
        if (isset($this->options['typeMap'])) {
            $cursor->setTypeMap($this->options['typeMap']);
        }

141
        return current($cursor->toArray());
142
    }
143 144

    /**
145
     * Create options for executing the command.
146
     *
147 148
     * @see http://php.net/manual/en/mongodb-driver-server.executewritecommand.php
     * @return array
149
     */
150
    private function createOptions()
151
    {
152
        $options = [];
153

154 155 156 157
        if (isset($this->options['session'])) {
            $options['session'] = $this->options['session'];
        }

158
        if (isset($this->options['writeConcern'])) {
159
            $options['writeConcern'] = $this->options['writeConcern'];
160 161
        }

162
        return $options;
163
    }
164
}