Completed
Push — 8.x-2.x ( 8af97d...4c7eae )
by Frédéric G.
03:11
created

ClientFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 42
rs 10
wmc 5
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A get() 0 15 4
1
<?php
2
/**
3
 * @file
4
 * Contains \Drupal\mongodb\ClientFactory.
5
 */
6
7
namespace Drupal\mongodb;
8
9
use Drupal\Core\Site\Settings;
10
use MongoDB\Client;
11
12
/**
13
 * Class ClientFactory.
14
 *
15
 * @package Drupal\mongodb
16
 */
17
class ClientFactory {
18
19
  /**
20
   * The 'mongodb' client settings.
21
   *
22
   * @var string[][]
23
   */
24
  protected $settings;
25
26
  /**
27
   * A hash of connections per alias.
28
   *
29
   * @var \MongoDB\Client[]
30
   */
31
  protected $clients;
32
33
  /**
34
   * Constructor.
35
   *
36
   * @param \Drupal\Core\Site\Settings $settings
37
   *   The system settings.
38
   */
39
  public function __construct(Settings $settings) {
40
    $this->settings = $settings->get('mongodb')['clients'];
41
  }
42
43
  public function get($alias) {
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
44
    if (!isset($this->clients[$alias]) || !$this->clients[$alias] instanceof \MongoDB\Client) {
45
      $info = isset($this->settings[$alias]) ? $this->settings[$alias] : [];
1 ignored issue
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
46
      $info += [
47
        'uri' => 'mongodb://localhost:27017',
48
        'uriOptions' => [],
49
        'driverOptions' => [],
50
      ];
51
52
      // Don't use ...$info: keys can be out of order.
53
      $this->clients[$alias] = new Client($info['uri'], $info['uriOptions'], $info['driverOptions']);
54
    }
55
56
    return $this->clients[$alias];
57
  }
58
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
59