Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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

   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\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  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 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       * Constructs a drop command.
  58       *
  59       * Supported options:
  60       *
  61       *  * session (MongoDB\Driver\Session): Client session.
  62       *
  63       *    Sessions are not supported for server versions < 3.6.
  64       *
  65       *  * typeMap (array): Type map for BSON deserialization. This will be used
  66       *    for the returned command result document.
  67       *
  68       *  * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
  69       *
  70       *    This is not supported for server versions < 3.4 and will result in an
  71       *    exception at execution time if used.
  72       *
  73       * @param string $databaseName   Database name
  74       * @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          if (isset($options['typeMap']) && ! is_array($options['typeMap'])) {
  85              throw InvalidArgumentException::invalidType('"typeMap" option', $options['typeMap'], 'array');
  86          }
  87  
  88          if (isset($options['writeConcern']) && ! $options['writeConcern'] instanceof WriteConcern) {
  89              throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], WriteConcern::class);
  90          }
  91  
  92          if (isset($options['writeConcern']) && $options['writeConcern']->isDefault()) {
  93              unset($options['writeConcern']);
  94          }
  95  
  96          $this->databaseName = (string) $databaseName;
  97          $this->collectionName = (string) $collectionName;
  98          $this->options = $options;
  99      }
 100  
 101      /**
 102       * Execute the operation.
 103       *
 104       * @see Executable::execute()
 105       * @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      public function execute(Server $server)
 111      {
 112          if (isset($this->options['writeConcern']) && ! server_supports_feature($server, self::$wireVersionForWriteConcern)) {
 113              throw UnsupportedException::writeConcernNotSupported();
 114          }
 115  
 116          $inTransaction = isset($this->options['session']) && $this->options['session']->isInTransaction();
 117          if ($inTransaction && isset($this->options['writeConcern'])) {
 118              throw UnsupportedException::writeConcernNotSupportedInTransaction();
 119          }
 120  
 121          $command = new Command(['drop' => $this->collectionName]);
 122  
 123          try {
 124              $cursor = $server->executeWriteCommand($this->databaseName, $command, $this->createOptions());
 125          } catch (DriverRuntimeException $e) {
 126              /* The server may return an error if the collection does not exist.
 127               * Check for an error message (unfortunately, there isn't a code)
 128               * and NOP instead of throwing.
 129               */
 130              if ($e->getMessage() === self::$errorMessageNamespaceNotFound) {
 131                  return (object) ['ok' => 0, 'errmsg' => self::$errorMessageNamespaceNotFound];
 132              }
 133  
 134              throw $e;
 135          }
 136  
 137          if (isset($this->options['typeMap'])) {
 138              $cursor->setTypeMap($this->options['typeMap']);
 139          }
 140  
 141          return current($cursor->toArray());
 142      }
 143  
 144      /**
 145       * Create options for executing the command.
 146       *
 147       * @see http://php.net/manual/en/mongodb-driver-server.executewritecommand.php
 148       * @return array
 149       */
 150      private function createOptions()
 151      {
 152          $options = [];
 153  
 154          if (isset($this->options['session'])) {
 155              $options['session'] = $this->options['session'];
 156          }
 157  
 158          if (isset($this->options['writeConcern'])) {
 159              $options['writeConcern'] = $this->options['writeConcern'];
 160          }
 161  
 162          return $options;
 163      }
 164  }