Completed
Branch master (9dcfc4)
by Daniel
24:32
created

SolrIndexCheck   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B check() 0 31 5
1
<?php
2
3
namespace SilverStripe\EnvironmentCheck\Checks;
4
5
use SilverStripe\EnvironmentCheck\EnvironmentCheck;
6
7
/**
8
 * Check the availability of all Solr indexes of given class.
9
 *
10
 * If there are no indexes of given class found, the returned status will still be "OK".
11
 *
12
 * @package environmentcheck
13
 */
14
class SolrIndexCheck implements EnvironmentCheck
15
{
16
    /**
17
     * @var null|string
18
     */
19
    protected $indexClass;
20
21
    /**
22
     * @param string $indexClass Limit the index checks to the specified class and all its subclasses.
23
     */
24
    public function __construct($indexClass = null)
25
    {
26
        $this->indexClass = $indexClass;
27
    }
28
29
    /**
30
     * {@inheritDoc}
31
     *
32
     * @return array
33
     */
34
    public function check()
35
    {
36
        $brokenCores = [];
37
38
        /**
39
         * @todo Revisit this when silverstripe/fulltextsearch has 4.x compat
40
         */
41
        if (!class_exists('\\Solr')) {
42
            return [
43
                EnvironmentCheck::ERROR,
44
                'Class `Solr` not found. Is the fulltextsearch module installed?'
45
            ];
46
        }
47
48
        $service = \Solr::service();
49
        foreach (\Solr::get_indexes($this->indexClass) as $index) {
50
            $core = $index->getIndexName();
51
            if (!$service->coreIsActive($core)) {
52
                $brokenCores[] = $core;
53
            }
54
        }
55
56
        if (!empty($brokenCores)) {
57
            return [
58
                EnvironmentCheck::ERROR,
59
                'The following indexes are unavailable: ' . implode($brokenCores, ', ')
60
            ];
61
        }
62
63
        return [EnvironmentCheck::OK, 'Expected indexes are available.'];
64
    }
65
}
66