Passed
Pull Request — 8.x-2.x (#58)
by Frédéric G.
03:34
created

MongodbCommands   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 97
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A mongodbCleanTests() 0 14 3
A mongodbFind() 0 8 1
A mongodbSettings() 0 3 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Drupal\mongodb\Commands;
6
7
use Drupal\mongodb\Install\Tools;
8
use Drush\Commands\DrushCommands;
9
10
/**
11
 * Drush 9 commands service for the mongodb module.
12
 */
13
class MongodbCommands extends DrushCommands {
14
15
  /**
16
   * The mongodb.tools service.
17
   *
18
   * @var \Drupal\mongodb\Install\Tools
19
   */
20
  protected $tools;
21
22
  /**
23
   * MongodbCommands constructor.
24
   *
25
   * @param \Drupal\mongodb\Install\Tools $tools
26
   *   The mongodb.tools service.
27
   */
28
  public function __construct(Tools $tools) {
29
    $this->tools = $tools;
30
  }
31
32
  /**
33
   * Drop the Simpletest leftover collections.
34
   *
35
   * @command mongodb:clean-tests
36
   * @aliases mdct,mo-clean
37
   *
38
   * @usage drush mongodb:clean-tests
39
   *   Clean test results after "bash tests.bash".
40
   */
41
  function mongodbCleanTests() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
42
    $dbs = array_keys($this->tools->settings()['databases']);
43
    foreach ($dbs as $dbAlias) {
44
      /** @var \MongoDB\Collection[] $collections */
45
      $collections = $this->tools->listCollections($dbAlias,
46
        "/^simpletest/");
47
      foreach ($collections as $collection) {
48
        $this->logger()->notice("Dropping {collectionName}", [
49
          'collectionName' => $collection->getCollectionName(),
50
        ]);
51
        $collection->drop();
52
      }
53
    }
54
  }
55
56
  /**
57
   * Execute a find() query against a collection.
58
   *
59
   * @param string $alias
60
   *   The database alias.
61
   * @param string $collection
62
   *   The collection name in the database.
63
   * @param string $selector
64
   *   A MongoDB find() selector in JSON format. Defaults to '{}'.
65
   * @param array $options
66
   *   A Drush-magic parameter enabling Drush to choose the output format.
67
   *
68
   * @return array
69
   *   The matching documents, in array format.
70
   *
71
   * @usage drush mongodb:find <collection> <query>...
72
   *   <query> is a single JSON selector in single string format. Quote it.
73
   * @usage drush mongodb:find logger watchdog
74
   *   Get the logger/watchdog error-level templates
75
   * @usage drush mo-find logger watchdog '{ "severity": 3 }'
76
   *   Get all the logger/watchdog entries tracking rows.
77
   * @usage drush mdbf keyvalue kvp_state '{ "_id": "system.theme_engine.files" }'
78
   *   Get a specific State entry. Note how escaping needs to be performed in
79
   *   the shell.
80
   *
81
   * @command mongodb:find
82
   * @aliases mdbf,mo-find
83
   */
84
  public function mongodbFind(
85
    string $alias,
86
    string $collection,
87
    string $selector = '{}',
88
    array $options = ['format' => 'yaml']
89
  ) {
90
    return $this->tools->find($alias, $collection, $selector);
91
  }
92
93
  /**
94
   * Print MongoDB settings in Yaml format.
95
   *
96
   * @usage mongodb:settings
97
   *   Report on the settings as seen by the MongoDB module suite.
98
   *
99
   * The "unused" $options allows Drush to know the command should support the
100
   * --format option, with the chosen default.
101
   *
102
   * @command mongodb:settings
103
   * @aliases mdbs,mo-set
104
   */
105
  public function mongodbSettings($options = ['format' => 'yaml']) {
106
    return $this->tools->settings();
107
  }
108
109
}
110