Completed
Push — watchdog-config ( 593fab )
by Frédéric G.
8s
created

mongodb_install.php ➔ mongodb_requirements()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 40
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 5
eloc 25
nc 5
nop 0
dl 0
loc 40
rs 8.439
c 3
b 0
f 0
1
<?php
2
3
/**
4
 * @file
5
 * Install file for the MongoDB module.
6
 */
7
8
use Drupal\Core\Site\Settings;
9
10
/**
11
 * Requirements check: MongoDB extension.
12
 *
13
 * @param array $ret
14
 *   The running requirements array
0 ignored issues
show
introduced by
Parameter comment must end with a full stop
Loading history...
15
 * @param string $extension_name
16
 *   The name of the extension to check.
17
 *
18
 * @return bool
19
 *   Did requirements check succeed ?
20
 */
21
function _mongodb_requirements_extension(array &$ret, $extension_name) {
22
  $success = extension_loaded($extension_name);
23 View Code Duplication
  if (!$success) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
24
    $ret['mongodb'] += array(
25
      'value' => t('Extension not loaded'),
26
      'description' => t('Mongodb requires the non-legacy PHP MongoDB extension (@name) to be installed.', [
27
        '@name' => $extension_name,
28
      ]),
29
      'severity' => REQUIREMENT_ERROR,
30
    );
31
  }
32
  return $success;
33
}
34
35
/**
36
 * Requirements check: extension version.
37
 *
38
 * @param array $ret
39
 *   The running requirements array.
40
 * @param array $description
41
 *   The running description array.
42
 * @param string $extension_name
43
 *   The name of the extension to check.
44
 *
45
 * @return bool
46
 *   Did requirements check succeed ?
47
 */
48
function _mongodb_requirements_extension_version(array &$ret, array &$description, $extension_name) {
49
  $minimum_version = '1.1.7';
50
  $extension_version = phpversion($extension_name);
51
  $version_status = version_compare($extension_version, $minimum_version);
52
  $success = $version_status >= 0;
53
  if ($success) {
54
    $description[] = t('Extension version @version found.', ['@version' => $extension_version]);
55
  }
56 View Code Duplication
  else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
    $ret['mongodb'] += [
58
      'severity' => REQUIREMENT_ERROR,
59
      'description' => t('Module needs extension @name @minimum_version or later, found @version.', [
60
        '@name' => $extension_name,
61
        '@minimum_version' => $minimum_version,
62
        '@version' => $extension_version,
63
      ]),
64
    ];
65
  }
66
67
  return $success;
68
}
69
70
/**
71
 * Requirements check: existence of the client aliases.
72
 *
73
 * @param array $ret
74
 *   The running requirements array.
75
 * @param array $description
76
 *   The running description array.
77
 * @param array $databases
78
 *   The databases array, sanitized from settings.
79
 *
80
 * @return bool
81
 *   Did requirements check succeed ?
82
 */
83
function _mongodb_requirements_aliases(array &$ret, array &$description, array $databases) {
84
  $success = !empty($databases);
85
  if (!$success) {
86
    $ret['mongodb'] += [
87
      'severity' => REQUIREMENT_WARNING,
88
      'value' => t('No database aliases found in settings. Did you actually configure your settings ?'),
89
      'description' => [
90
        '#theme' => 'item_list',
91
        '#items' => $description,
92
      ],
93
    ];
94
  }
95
96
  return $success;
97
}
98
99
/**
100
 * Requirements check: database vs clients consistency.
101
 *
102
 * @param array $settings
103
 *   The mongodb settings.
104
 * @param array $databases
105
 *   The databases, sanitized from settings.
106
 * @param array $description
107
 *   The running description array.
108
 *
109
 * @return bool
110
 *   Did requirements check succeed ?
111
 */
112
function _mongodb_requirements_databases(array $settings, array $databases, array &$description) {
113
  $client_aliases = $settings['clients'] ?? [];
1 ignored issue
show
introduced by
Expected 1 space after "?"; 0 found
Loading history...
114
  $warnings = [];
115
  $success = TRUE;
116
  foreach ($databases as $database => list($client, $name)) {
0 ignored issues
show
introduced by
Variable $client is undefined.
Loading history...
introduced by
Variable $name is undefined.
Loading history...
117
    if (empty($client_aliases[$client])) {
0 ignored issues
show
introduced by
Variable $client is undefined.
Loading history...
118
      $success = FALSE;
119
      $warnings[] = t('Database "@db" references undefined client "@client".', [
120
        '@db' => $database,
121
        '@client' => $client,
0 ignored issues
show
introduced by
Variable $client is undefined.
Loading history...
122
      ]);
123
    }
124
  }
125
126
  if ($success) {
127
    $warnings = [t('Databases and clients are consistent.')];
128
  }
129
130
  $description = [
131
    '#theme' => 'item_list',
132
    '#items' => array_merge($description, $warnings),
133
  ];
134
135
  return $success;
136
}
137
138
/**
139
 * Implements hook_requirements().
140
 */
141
function mongodb_requirements() {
142
  $extension_name = 'mongodb';
143
144
  $ret = [];
145
  $ret['mongodb'] = [
146
    'title' => t('Mongodb'),
147
    'severity' => REQUIREMENT_OK,
148
  ];
149
  $description = [];
150
151
  if (!_mongodb_requirements_extension($ret, $extension_name)) {
152
    return $ret;
153
  }
154
155
  if (!_mongodb_requirements_extension_version($ret, $description, $extension_name)) {
156
    return $ret;
157
  }
158
159
  $settings = Settings::get('mongodb');
160
  $databases = $settings['databases'] ?? [];
1 ignored issue
show
introduced by
Expected 1 space after "?"; 0 found
Loading history...
161
162
  if (!_mongodb_requirements_aliases($ret, $description, $databases)) {
163
    return $ret;
164
  }
165
166
  if (!_mongodb_requirements_databases($settings, $databases, $description)) {
167
    $ret['mongodb'] += [
168
      'value' => t('Inconsistent database/client settings.'),
169
      'severity' => REQUIREMENT_ERROR,
170
      'description' => $description,
171
    ];
172
    return $ret;
173
  }
174
175
  $ret['mongodb'] += [
176
    'value' => t('Valid configuration'),
177
    'description' => $description,
178
  ];
179
  return $ret;
180
}
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...
181