Tools   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 30
c 0
b 0
f 0
dl 0
loc 113
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A settings() 0 2 1
A listCollections() 0 12 3
A __construct() 0 8 1
A find() 0 22 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Drupal\mongodb\Install;
6
7
use Drupal\Component\Serialization\SerializationInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Component\Seriali...\SerializationInterface 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 Drupal\mongodb\DatabaseFactory;
10
use Drupal\mongodb\MongoDb;
11
use MongoDB\Exception\InvalidArgumentException;
12
13
/**
14
 * Class MongoDbCommands provides the Drush commands for the mongodb module.
15
 */
16
class Tools {
17
18
  /**
19
   * The mongobb.database_factory service.
20
   *
21
   * @var \Drupal\mongodb\DatabaseFactory
22
   */
23
  protected $dbFactory;
24
25
  /**
26
   * The settings service.
27
   *
28
   * @var \Drupal\Core\Site\Settings
29
   */
30
  protected $settings;
31
32
  /**
33
   * The serialization.yaml service.
34
   *
35
   * @var \Drupal\Component\Serialization\SerializationInterface
36
   */
37
  protected $yaml;
38
39
  /**
40
   * MongoDbCommands constructor.
41
   *
42
   * @param \Drupal\mongodb\DatabaseFactory $databaseFactory
43
   *   The mongobb.database_factory service.
44
   * @param \Drupal\Core\Site\Settings $settings
45
   *   The settings service.
46
   * @param \Drupal\Component\Serialization\SerializationInterface $yaml
47
   *   The serialization.yaml service.
48
   */
49
  public function __construct(
50
    DatabaseFactory $databaseFactory,
51
    Settings $settings,
52
    SerializationInterface $yaml
53
  ) {
54
    $this->dbFactory = $databaseFactory;
55
    $this->settings = $settings;
56
    $this->yaml = $yaml;
57
  }
58
59
  /**
60
   * Command callback for mongodb:mdbf.
61
   *
62
   * @param string $alias
63
   *   The alias of the database in which to perform the query.
64
   * @param string $collection
65
   *   The name of the collection in which to find.
66
   * @param string $selector
67
   *   The selector to apply to the query.
68
   *
69
   * @return array<int,array<scalar,mixed>>
70
   *   The query results.
71
   */
72
  public function find(string $alias, string $collection, string $selector = '{}'): array {
73
    /** @var \MongoDB\Database $database */
74
    $database = $this->dbFactory->get($alias);
75
    $jsonSelector = json_decode($selector);
76
    if ($jsonSelector === NULL) {
77
      throw new InvalidArgumentException("Your JSON selector could not be decoded. Here is how PHP received it: " . var_export($selector, TRUE));
78
    }
79
    $docs1 = $database->selectCollection($collection)
80
      ->find($jsonSelector, [
81
        'typeMap' => [
82
          'root' => 'array',
83
          'document' => 'array',
84
          'array' => 'array',
85
        ],
86
      ]);
87
88
    $docs2 = [];
89
    // Convert objects in result set to hashes.
90
    foreach ($docs1 as $doc1) {
91
      $docs2[] = json_decode(json_encode($doc1), TRUE);
92
    }
93
    return $docs2;
94
  }
95
96
  /**
97
   * List collections matching $regex in database $alias.
98
   *
99
   * @param string $alias
100
   *   The alias in which to list the collections.
101
   * @param string $regex
102
   *   The pattern collection names must match.
103
   *
104
   * @return \MongoDB\Collection[]
105
   *   An array of collection objects.
106
   */
107
  public function listCollections(string $alias, string $regex): array {
108
    /** @var \MongoDB\Database $database */
109
    $database = $this->dbFactory->get($alias);
110
    $collections = [];
111
    foreach ($database->listCollections() as $info) {
112
      $name = $info->getName();
113
      $collection = $database->selectCollection($name);
114
      if (preg_match($regex, $name)) {
115
        $collections[] = $collection;
116
      }
117
    }
118
    return $collections;
119
  }
120
121
  /**
122
   * Command callback for mongodb:mdbs.
123
   *
124
   * @return array{clients: array<string,array<string,mixed>>, databases: array<string,array{0:string,1:string}>}
125
   *   The MongoDB portion of the settings. Refer to example.settings.local.php.
126
   */
127
  public function settings(): array {
128
    return $this->settings->get(MongoDb::MODULE);
129
  }
130
131
}
132