ClientFactory::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Drupal\mongodb;
6
7
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...
8
use MongoDB\Client;
9
10
/**
11
 * Helper class to construct a MongoDB client with Drupal specific config.
12
 */
13
class ClientFactory {
14
15
  /**
16
   * The 'mongodb' client settings.
17
   *
18
   * @var string[][]
19
   */
20
  protected $settings;
21
22
  /**
23
   * A hash of connections per alias.
24
   *
25
   * @var \MongoDB\Client[]
26
   */
27
  protected $clients;
28
29
  /**
30
   * Constructor.
31
   *
32
   * @param \Drupal\Core\Site\Settings $settings
33
   *   The system settings.
34
   */
35
  public function __construct(Settings $settings) {
36
    $this->settings = $settings->get(MongoDb::MODULE)['clients'];
37
  }
38
39
  /**
40
   * Return a Client instance for a given alias.
41
   *
42
   * @param string $alias
43
   *   The alias defined in settings for a Client.
44
   *
45
   * @return \MongoDB\Client
46
   *   A Client instance for the chosen server.
47
   */
48
  public function get($alias) {
49
    if (!isset($this->clients[$alias]) || !$this->clients[$alias] instanceof Client) {
50
      $info = $this->settings[$alias] ?? [];
51
      $info += [
52
        'uri' => 'mongodb://localhost:27017',
53
        'uriOptions' => [],
54
        'driverOptions' => [],
55
      ];
56
57
      // Don't use ...$info: keys can be out of order.
58
      $this->clients[$alias] = new Client($info['uri'], $info['uriOptions'], $info['driverOptions']);
59
    }
60
61
    return $this->clients[$alias];
62
  }
63
64
}
65