DatabaseCheck   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 35
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A check() 0 13 3
A __construct() 0 3 1
1
<?php
2
3
namespace SilverStripe\EnvironmentCheck\Checks;
4
5
use SilverStripe\EnvironmentCheck\EnvironmentCheck;
6
use SilverStripe\ORM\DB;
7
8
/**
9
 * Check that the connection to the database is working, by ensuring that the table exists and that
10
 * the table contains some records.
11
 *
12
 * @package environmentcheck
13
 */
14
class DatabaseCheck implements EnvironmentCheck
15
{
16
    /**
17
     * @var string
18
     */
19
    protected $checkTable;
20
21
    /**
22
     * By default, Member will be checked.
23
     *
24
     * @param string $checkTable
25
     */
26
    public function __construct($checkTable = 'Member')
27
    {
28
        $this->checkTable = $checkTable;
29
    }
30
31
    /**
32
     * {@inheritDoc}
33
     *
34
     * @return array
35
     */
36
    public function check()
37
    {
38
        if (!DB::get_schema()->hasTable($this->checkTable)) {
39
            return [EnvironmentCheck::ERROR, "$this->checkTable not present in the database"];
40
        }
41
42
        $count = DB::query("SELECT COUNT(*) FROM \"$this->checkTable\"")->value();
43
44
        if ($count > 0) {
45
            return [EnvironmentCheck::OK, ''];
46
        }
47
48
        return [EnvironmentCheck::WARNING, "$this->checkTable queried ok but has no records"];
49
    }
50
}
51