Passed
Push — 7.x-1.x ( 3b772a...549ee4 )
by Frédéric G.
03:20 queued 10s
created

mongodb_cache_flush_caches()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 39
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 23
dl 0
loc 39
rs 9.2408
c 1
b 1
f 0
cc 5
nc 3
nop 0
1
<?php
2
3
/**
4
 * @file
5
 * The MongoDB cache module.
6
 *
7
 * This module only needed:
8
 * - to ensure system_cron triggers expires on cache bins not declared by their
9
 *   owner modules in hook_flush_caches().
10
 * - to ensure a 503 status on exceptions.
11
 */
12
13
use Drupal\mongodb_cache\Cache;
14
15
/**
16
 * Implements hook_exit().
17
 */
18
function mongodb_cache_exit() {
19
  if (Cache::hasException()) {
20
    drupal_add_http_header('Status', '503 Service Unavailable');
0 ignored issues
show
Bug introduced by
The function drupal_add_http_header was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

20
    /** @scrutinizer ignore-call */ 
21
    drupal_add_http_header('Status', '503 Service Unavailable');
Loading history...
21
  }
22
}
23
24
/**
25
 * Implements hook_flush_caches().
26
 *
27
 * Support triggering expiration in modules not declaring their cache bins,
28
 * either by taking them from an explicit variable, or by performing a discovery
29
 * assuming the cache bin names to start by 'cache_'.
30
 */
31
function mongodb_cache_flush_caches() {
32
  // Recursion protection, not static caching, so no drupal_static().
33
  static $reentry = FALSE;
34
35
  if ($reentry) {
36
    return [];
37
  }
38
39
  // Hardcoded in drupal_flush_all_caches() or system_cron(), but not declared
40
  // in system_flush_caches().
41
  $system_bins = [
42
    'cache',
43
    'cache_bootstrap',
44
    'cache_filter',
45
    'cache_form',
46
    'cache_menu',
47
    'cache_page',
48
    'cache_path',
49
  ];
50
51
  $reentry = TRUE;
52
  $owned_bins = module_invoke_all('flush_caches');
0 ignored issues
show
Bug introduced by
The function module_invoke_all was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

52
  $owned_bins = /** @scrutinizer ignore-call */ module_invoke_all('flush_caches');
Loading history...
53
  $reentry = FALSE;
0 ignored issues
show
Unused Code introduced by
The assignment to $reentry is dead and can be removed.
Loading history...
54
55
  $detected_bins = variable_get('mongodb_cache_extra_bins', NULL);
0 ignored issues
show
Bug introduced by
The function variable_get was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

55
  $detected_bins = /** @scrutinizer ignore-call */ variable_get('mongodb_cache_extra_bins', NULL);
Loading history...
56
57
  // As with databases, NULL means unknown, so perform a discovery.
58
  if (!isset($detected_bins)) {
59
    $detected_bins = [];
60
    $names = mongodb()->getCollectionNames();
61
    foreach ($names as $name) {
62
      if (strpos($name, 'cache_') === 0) {
63
        $detected_bins[] = $name;
64
      }
65
    }
66
  }
67
68
  $adopted_bins = array_diff($detected_bins, $system_bins, $owned_bins);
69
  return $adopted_bins;
70
}
71
72
/**
73
 * Implements hook_module_implements_alter().
74
 */
75
function mongodb_cache_module_implements_alter(&$implementations, $hook) {
76
  if ($hook !== ('flush_caches')) {
77
    return;
78
  }
79
  $module = 'mongodb_cache';
80
  unset($implementations[$module]);
81
  $implementations[$module] = FALSE;
82
}
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...
83