Passed
Pull Request — 8.x-2.x (#71)
by Frédéric G.
05:37
created

MongoDb   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 88
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A libraryApiVersion() 0 14 4
A countCollection() 0 9 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Drupal\mongodb;
6
7
use MongoDB\Collection;
8
use MongoDB\Exception\UnexpectedValueException;
9
10
/**
11
 * Class MongoDb contains constants usable by all modules using the driver.
12
 */
13
class MongoDb {
14
15
  const CLIENT_DEFAULT = 'default';
16
17
  const DB_DEFAULT = 'default';
18
19
  const EXTENSION = 'mongodb';
20
  const MODULE = 'mongodb';
21
22
  const SERVICE_CLIENT_FACTORY = 'mongodb.client_factory';
23
  const SERVICE_DB_FACTORY = 'mongodb.database_factory';
24
  const SERVICE_TOOLS = 'mongodb.tools';
25
26
  // A frequent projection to just request the document ID.
27
  const ID_PROJECTION = ['projection' => ['_id' => 1]];
28
29
  /**
30
   * The MongoDB library "API version", a reduced version of the actual version.
31
   *
32
   * @var string
33
   */
34
  protected static string $libraryVersion;
35
36
  /**
37
   * Guess an approximation of the library version, to handle API changes.
38
   *
39
   * - 1.2.0 is the minimum version required from composer.json.
40
   * - 1.3.0 adds Collection::watch().
41
   * - 1.4.0 deprecates Collection::count() and adds countDocuments().
42
   *
43
   * @return string
44
   *   A semantic versioning version string.
45
   *
46
   * @internal
47
   *
48
   * Thanks to jmikola for simplifications to this method.
49
   *
50
   * @see https://github.com/mongodb/mongo-php-library/issues/558
51
   */
52
  public static function libraryApiVersion() : string {
53
    if (!empty(static::$libraryVersion)) {
54
      return static::$libraryVersion;
55
    }
56
57
    if (method_exists(Collection::class, 'countDocuments')) {
58
      return (static::$libraryVersion = '1.4.0');
59
    }
60
61
    if (method_exists(Collection::class, 'watch')) {
62
      return (static::$libraryVersion = '1.3.0');
63
    }
64
65
    return (static::$libraryVersion = '1.2.0');
66
  }
67
68
  /**
69
   * Count items matching a selector in a collection.
70
   *
71
   * This function used to be needed when:
72
   * - library versions below and above 1.4.0 were supported, as in 8.x-2.0.
73
   *   With the minimum version now being 1.5.0, the Collection::count() method
74
   *   is no longer needed and is deprecated.
75
   * - MongoDB PHPLIB-376 was not yet fixed and needed the try/catch around
76
   *   Collection::countDocuments().
77
   *
78
   * Since both issues have been resolved, this method is only used for
79
   * compatibility and will be deprecated after the Drupal 9.0 release. It is
80
   * not marked as deprecated to avoid a Drupal 9 compatibility check.
81
   *
82
   * @param \MongoDB\Collection $collection
83
   *   The collection for which to count items.
84
   * @param array $selector
85
   *   The collection selector.
86
   *
87
   * @return int
88
   *   The number of elements matching the selector in the collection.
89
   *
90
   * @see https://jira.mongodb.org/browse/PHPLIB-376
91
   */
92
  public static function countCollection(Collection $collection, array $selector = []) : int {
93
    try {
94
      $count = $collection->countDocuments($selector);
95
    }
96
    catch (UnexpectedValueException $e) {
97
      $count = 0;
98
    }
99
100
    return $count;
101
  }
102
103
}
104