DatabaseFactory   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A get() 0 18 3
A nextId() 0 27 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Drupal\mongodb;
6
7
use Drupal\Component\Render\FormattableMarkup;
0 ignored issues
show
Bug introduced by
The type Drupal\Component\Render\FormattableMarkup was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Drupal\Core\Site\Settings;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Site\Settings was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use MongoDB\Operation\FindOneAndUpdate;
10
11
/**
12
 * Helper class to construct a MongoDB Database with Drupal specific config.
13
 *
14
 * @package Drupal\mongodb
15
 */
16
class DatabaseFactory {
17
18
  /**
19
   * The Client factory service.
20
   *
21
   * @var \Drupal\mongodb\ClientFactory
22
   */
23
  protected $clientFactory;
24
25
  /**
26
   * The 'mongodb' database settings array.
27
   *
28
   * @var string[][]
29
   */
30
  protected $settings;
31
32
  /**
33
   * Constructor.
34
   *
35
   * @param \Drupal\mongodb\ClientFactory $clientFactory
36
   *   The Client factory service.
37
   * @param \Drupal\Core\Site\Settings $settings
38
   *   The settings service.
39
   */
40
  public function __construct(ClientFactory $clientFactory, Settings $settings) {
41
    $this->clientFactory = $clientFactory;
42
    $this->settings = $settings->get('mongodb')['databases'];
43
  }
44
45
  /**
46
   * Return the MongoDB database matching an alias.
47
   *
48
   * @param string $dbAlias
49
   *   The alias string, like "default".
50
   *
51
   * @return \MongoDB\Database|null
52
   *   The selected database, or NULL if an error occurred.
53
   */
54
  public function get($dbAlias) {
55
    if (!isset($this->settings[$dbAlias])) {
56
57
      throw new \InvalidArgumentException((new FormattableMarkup('Nonexistent database alias: @alias', [
58
        '@alias' => $dbAlias,
59
      ]))->__toString());
60
    }
61
    try {
62
      [$clientAlias, $database] = $this->settings[$dbAlias];
63
      $client = $this->clientFactory->get($clientAlias);
64
      $result = $client->selectDatabase($database);
65
    }
66
    // Includes its descendant \MongoDb\Exception\InvalidArgumentException.
67
    catch (\InvalidArgumentException $e) {
68
      $result = NULL;
69
    }
70
71
    return $result;
72
  }
73
74
  /**
75
   * Return the next integer ID in a sequence. For numeric ids in collections.
76
   *
77
   * @param string $sequenceId
78
   *   The name of the sequence, typically a collection name in the current
79
   *   database.
80
   * @param int $value
81
   *   Optional. If given, the result will be at least 1 more that this.
82
   *
83
   * @return int
84
   *   The next id. It will be greater than $value, possibly by more than 1.
85
   */
86
  public function nextId($sequenceId = 'sequences', $value = 0) {
87
    $collection = $this->get('default')
88
      ->selectCollection('sequences');
89
    $sequenceSelector = ['_id' => $sequenceId];
90
91
    // Force the minimum if given.
92
    if ($value) {
93
      $selector = $sequenceSelector + [
94
        'value' => ['$lt' => $value],
95
      ];
96
      $update = [
97
        '$set' => ['value' => $value],
98
      ];
99
      $collection->updateOne($selector, $update);
100
    }
101
102
    // Then increment it.
103
    $update = [
104
      '$inc' => ['value' => 1],
105
    ];
106
    $options = [
107
      'upsert' => TRUE,
108
      'returnDocument' => FindOneAndUpdate::RETURN_DOCUMENT_AFTER,
109
    ];
110
    $document = $collection->findOneAndUpdate($sequenceSelector, $update, $options);
111
    $result = $document->value ?? 1;
112
    return $result;
113
  }
114
115
}
116