Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 401] [Versions 39 and 311]

   1  <?php
   2  /*
   3   * Copyright 2015-2017 MongoDB, Inc.
   4   *
   5   * Licensed under the Apache License, Version 2.0 (the "License");
   6   * you may not use this file except in compliance with the License.
   7   * You may obtain a copy of the License at
   8   *
   9   *   http://www.apache.org/licenses/LICENSE-2.0
  10   *
  11   * Unless required by applicable law or agreed to in writing, software
  12   * distributed under the License is distributed on an "AS IS" BASIS,
  13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14   * See the License for the specific language governing permissions and
  15   * limitations under the License.
  16   */
  17  
  18  namespace MongoDB\Operation;
  19  
  20  use MongoDB\Driver\Command;
  21  use MongoDB\Driver\Exception\CommandException;
  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  use function current;
  28  use function is_array;
  29  use function MongoDB\server_supports_feature;
  30  
  31  /**
  32   * Operation for the drop command.
  33   *
  34   * @api
  35   * @see \MongoDB\Collection::drop()
  36   * @see \MongoDB\Database::dropCollection()
  37   * @see http://docs.mongodb.org/manual/reference/command/drop/
  38   */
  39  class DropCollection implements Executable
  40  {
  41      /** @var integer */
  42      private static $errorCodeNamespaceNotFound = 26;
  43  
  44      /** @var string */
  45      private static $errorMessageNamespaceNotFound = 'ns not found';
  46  
  47      /** @var integer */
  48      private static $wireVersionForWriteConcern = 5;
  49  
  50      /** @var string */
  51      private $databaseName;
  52  
  53      /** @var string */
  54      private $collectionName;
  55  
  56      /** @var array */
  57      private $options;
  58  
  59      /**
  60       * Constructs a drop command.
  61       *
  62       * Supported options:
  63       *
  64       *  * session (MongoDB\Driver\Session): Client session.
  65       *
  66       *    Sessions are not supported for server versions < 3.6.
  67       *
  68       *  * typeMap (array): Type map for BSON deserialization. This will be used
  69       *    for the returned command result document.
  70       *
  71       *  * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
  72       *
  73       *    This is not supported for server versions < 3.4 and will result in an
  74       *    exception at execution time if used.
  75       *
  76       * @param string $databaseName   Database name
  77       * @param string $collectionName Collection name
  78       * @param array  $options        Command options
  79       * @throws InvalidArgumentException for parameter/option parsing errors
  80       */
  81      public function __construct($databaseName, $collectionName, array $options = [])
  82      {
  83          if (isset($options['session']) && ! $options['session'] instanceof Session) {
  84              throw InvalidArgumentException::invalidType('"session" option', $options['session'], Session::class);
  85          }
  86  
  87          if (isset($options['typeMap']) && ! is_array($options['typeMap'])) {
  88              throw InvalidArgumentException::invalidType('"typeMap" option', $options['typeMap'], 'array');
  89          }
  90  
  91          if (isset($options['writeConcern']) && ! $options['writeConcern'] instanceof WriteConcern) {
  92              throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], WriteConcern::class);
  93          }
  94  
  95          if (isset($options['writeConcern']) && $options['writeConcern']->isDefault()) {
  96              unset($options['writeConcern']);
  97          }
  98  
  99          $this->databaseName = (string) $databaseName;
 100          $this->collectionName = (string) $collectionName;
 101          $this->options = $options;
 102      }
 103  
 104      /**
 105       * Execute the operation.
 106       *
 107       * @see Executable::execute()
 108       * @param Server $server
 109       * @return array|object Command result document
 110       * @throws UnsupportedException if writeConcern is used and unsupported
 111       * @throws DriverRuntimeException for other driver errors (e.g. connection errors)
 112       */
 113      public function execute(Server $server)
 114      {
 115          if (isset($this->options['writeConcern']) && ! server_supports_feature($server, self::$wireVersionForWriteConcern)) {
 116              throw UnsupportedException::writeConcernNotSupported();
 117          }
 118  
 119          $inTransaction = isset($this->options['session']) && $this->options['session']->isInTransaction();
 120          if ($inTransaction && isset($this->options['writeConcern'])) {
 121              throw UnsupportedException::writeConcernNotSupportedInTransaction();
 122          }
 123  
 124          $command = new Command(['drop' => $this->collectionName]);
 125  
 126          try {
 127              $cursor = $server->executeWriteCommand($this->databaseName, $command, $this->createOptions());
 128          } catch (CommandException $e) {
 129              /* The server may return an error if the collection does not exist.
 130               * Check for an error code (or message for pre-3.2 servers) and
 131               * return the command reply instead of throwing. */
 132              if ($e->getCode() === self::$errorCodeNamespaceNotFound ||
 133                  $e->getMessage() === self::$errorMessageNamespaceNotFound) {
 134                  return $e->getResultDocument();
 135              }
 136  
 137              throw $e;
 138          }
 139  
 140          if (isset($this->options['typeMap'])) {
 141              $cursor->setTypeMap($this->options['typeMap']);
 142          }
 143  
 144          return current($cursor->toArray());
 145      }
 146  
 147      /**
 148       * Create options for executing the command.
 149       *
 150       * @see http://php.net/manual/en/mongodb-driver-server.executewritecommand.php
 151       * @return array
 152       */
 153      private function createOptions()
 154      {
 155          $options = [];
 156  
 157          if (isset($this->options['session'])) {
 158              $options['session'] = $this->options['session'];
 159          }
 160  
 161          if (isset($this->options['writeConcern'])) {
 162              $options['writeConcern'] = $this->options['writeConcern'];
 163          }
 164  
 165          return $options;
 166      }
 167  }