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

SanityCheck::store()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 8
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 11
rs 9.6111
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Drupal\mongodb_watchdog\Install;
6
7
use Drupal\Core\Config\ConfigFactoryInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Config\ConfigFactoryInterface 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\mongodb\DatabaseFactory;
9
use Drupal\mongodb_watchdog\Logger;
10
11
/**
12
 * Class SanityCheck provides some reasonableness checks for MongoDB contents.
13
 *
14
 * @see \Drupal\mongodb_watchdog\Command\SanityCheckCommand
15
 * @see \Drupal\mongodb_watchdog\Commands\MongoDbWatchdogCommands::sanityCheck()
16
 */
17
class SanityCheck {
18
19
  /**
20
   * The per-collection-size statistics buckets.
21
   *
22
   * @var array
23
   */
24
  protected $buckets;
25
26
  /**
27
   * The module configuration.
28
   *
29
   * @var \Drupal\Core\Config\ImmutableConfig
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Config\ImmutableConfig 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...
30
   */
31
  protected $config;
32
33
  /**
34
   * The mongodb.database_factory service.
35
   *
36
   * @var \Drupal\mongodb\DatabaseFactory
37
   */
38
  protected $dbFactory;
39
40
  /**
41
   * The bucket size values.
42
   *
43
   * @var array
44
   */
45
  protected $items;
46
47
  /**
48
   * SanityCheck constructor.
49
   *
50
   * @param \Drupal\mongodb\DatabaseFactory $dbFactory
51
   *   The mongodb.database_factory service.
52
   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
53
   *   The config.factory service.
54
   */
55
  public function __construct(
56
    DatabaseFactory $dbFactory,
57
    ConfigFactoryInterface $configFactory
58
  ) {
59
    $this->dbFactory = $dbFactory;
60
61
    $this->config = $configFactory->get(Logger::CONFIG_NAME);
62
    $this->items = $this->config->get(Logger::CONFIG_ITEMS);
63
  }
64
65
  /**
66
   * Build a list of the number of entries per collection in the default DB.
67
   */
68
  public function buildCollectionstats(): array {
69
    /** @var \Drupal\mongodb\DatabaseFactory $databaseFactory */
70
    $database = $this->dbFactory->get(Logger::DB_LOGGER);
71
    $this->initBucketsList();
72
73
    $collections = $database->listCollections();
74
    foreach ($collections as $collectionInfo) {
75
      $name = $collectionInfo->getName();
76
      $collection = $database->selectCollection($name);
77
      $count = $collection->countDocuments();
78
      if (preg_match('/' . Logger::EVENT_COLLECTIONS_PATTERN . '/', $name)) {
79
        $this->store($count);
80
      }
81
    }
82
83
    return $this->buckets;
84
  }
85
86
  /**
87
   * Prepare a table of bucket to hold the statistics.
88
   */
89
  protected function initBucketsList(): void {
90
    $barCount = 10;
91
    $barWidth = $this->items / $barCount;
92
    $buckets = [
93
      0 => 0,
94
      1 => 0,
95
      $this->items - 1 => 0,
96
      $this->items => 0,
97
    ];
98
99
    // Values 0, 1 and the value of $items are reserved.
100
    for ($i = 1; $i < $barCount; $i++) {
101
      $buckets[$i * $barWidth] = 0;
102
    }
103
    ksort($buckets);
104
    $this->buckets = $buckets;
105
  }
106
107
  /**
108
   * Store a collection document count in its statistics bucket.
109
   *
110
   * @param int $value
111
   *   The value to store.
112
   */
113
  protected function store(int $value): void {
114
    if ($value <= 1 || $value >= $this->items - 1) {
115
      $this->buckets[$value]++;
116
      return;
117
    }
118
    $keys = array_slice(array_keys($this->buckets), 1, -1);
119
120
    foreach ($keys as $key) {
121
      if ($value < $key) {
122
        $this->buckets[$key]++;
123
        return;
124
      }
125
    }
126
  }
127
128
}
129