|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * Copyright 2023-present 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 | + * https://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 | +// phpcs:ignore SlevomatCodingStandard.Namespaces.UnusedUses.UnusedUse |
| 21 | +use MongoDB\BSON\Binary; |
| 22 | +use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException; |
| 23 | +use MongoDB\Driver\Server; |
| 24 | +use MongoDB\Exception\InvalidArgumentException; |
| 25 | + |
| 26 | +use function is_array; |
| 27 | +use function is_object; |
| 28 | + |
| 29 | +/** |
| 30 | + * Drop an encrypted collection. |
| 31 | + * |
| 32 | + * The "encryptedFields" option is required. |
| 33 | + * |
| 34 | + * This operation additionally drops related metadata collections. |
| 35 | + * |
| 36 | + * @internal |
| 37 | + * @see \MongoDB\Database::dropCollection() |
| 38 | + * @see \MongoDB\Collection::drop() |
| 39 | + * @see https://github.com/mongodb/specifications/blob/master/source/client-side-encryption/client-side-encryption.rst#drop-collection-helper |
| 40 | + * @see https://www.mongodb.com/docs/manual/core/queryable-encryption/fundamentals/manage-collections/ |
| 41 | + */ |
| 42 | +class DropEncryptedCollection implements Executable |
| 43 | +{ |
| 44 | + /** @var DropCollection */ |
| 45 | + private $dropCollection; |
| 46 | + |
| 47 | + /** @var DropCollection[] */ |
| 48 | + private $dropMetadataCollections; |
| 49 | + |
| 50 | + /** |
| 51 | + * Constructs an operation to drop an encrypted collection and its related |
| 52 | + * metadata collections. |
| 53 | + * |
| 54 | + * The following option is supported in addition to the options for |
| 55 | + * DropCollection: |
| 56 | + * |
| 57 | + * * encryptedFields (document): Configuration for encrypted fields. |
| 58 | + * See: https://www.mongodb.com/docs/manual/core/queryable-encryption/fundamentals/encrypt-and-query/ |
| 59 | + * |
| 60 | + * @see DropCollection::__construct() for supported options |
| 61 | + * @param string $databaseName Database name |
| 62 | + * @param string $collectionName Collection name |
| 63 | + * @param array $options DropCollection options |
| 64 | + * @throws InvalidArgumentException for parameter/option parsing errors |
| 65 | + */ |
| 66 | + public function __construct(string $databaseName, string $collectionName, array $options) |
| 67 | + { |
| 68 | + if (! isset($options['encryptedFields'])) { |
| 69 | + throw new InvalidArgumentException('"encryptedFields" option is required'); |
| 70 | + } |
| 71 | + |
| 72 | + if (! is_array($options['encryptedFields']) && ! is_object($options['encryptedFields'])) { |
| 73 | + throw InvalidArgumentException::invalidType('"encryptedFields" option', $options['encryptedFields'], ['array', 'object']); |
| 74 | + } |
| 75 | + |
| 76 | + /** @psalm-var array{eccCollection?: ?string, ecocCollection?: ?string, escCollection?: ?string} */ |
| 77 | + $encryptedFields = (array) $options['encryptedFields']; |
| 78 | + |
| 79 | + $this->dropMetadataCollections = [ |
| 80 | + new DropCollection($databaseName, $encryptedFields['escCollection'] ?? 'enxcol_.' . $collectionName . '.esc'), |
| 81 | + new DropCollection($databaseName, $encryptedFields['eccCollection'] ?? 'enxcol_.' . $collectionName . '.ecc'), |
| 82 | + new DropCollection($databaseName, $encryptedFields['ecocCollection'] ?? 'enxcol_.' . $collectionName . '.ecoc'), |
| 83 | + ]; |
| 84 | + |
| 85 | + // DropCollection does not use the "encryptedFields" option |
| 86 | + unset($options['encryptedFields']); |
| 87 | + |
| 88 | + $this->dropCollection = new DropCollection($databaseName, $collectionName, $options); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @see Executable::execute() |
| 93 | + * @return array|object Command result document from dropping the encrypted collection |
| 94 | + * @throws DriverRuntimeException for other driver errors (e.g. connection errors) |
| 95 | + */ |
| 96 | + public function execute(Server $server) |
| 97 | + { |
| 98 | + foreach ($this->dropMetadataCollections as $dropMetadataCollection) { |
| 99 | + $dropMetadataCollection->execute($server); |
| 100 | + } |
| 101 | + |
| 102 | + return $this->dropCollection->execute($server); |
| 103 | + } |
| 104 | +} |
0 commit comments