Passed
Push — 8.x-2.x ( 9af07d...3011f1 )
by Frédéric G.
05:35
created

MongodbCommands   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A mongodbSettings() 0 2 1
A mongodbFind() 0 7 1
A mongodbCleanTests() 0 11 3
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;
0 ignored issues
show
Bug introduced by
The type Drush\Commands\DrushCommands 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
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
  public function mongodbCleanTests() {
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