SSConfiguration   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 45
c 1
b 0
f 0
dl 0
loc 101
rs 10
wmc 13

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getReport() 0 4 1
A getWarnings() 0 9 2
A generateReportsHTML() 0 16 2
A checkDefaultAdmin() 0 10 4
A checkEnvironment() 0 13 4
1
<?php
2
3
namespace BiffBangPow\SSMonitor\Server\Client;
4
5
use BiffBangPow\SSMonitor\Server\Helper\ReportingHelper;
6
use SilverStripe\Core\Config\Configurable;
7
use SilverStripe\Core\Extensible;
8
use SilverStripe\Core\Injector\Injector;
9
use Psr\Log\LoggerInterface;
10
use SilverStripe\ORM\ArrayList;
11
use SilverStripe\View\ArrayData;
12
use SilverStripe\View\SSViewer;
13
14
class SSConfiguration implements MonitoringClientInterface
15
{
16
17
    use ClientCommon;
18
    use Configurable;
19
    use Extensible;
20
21
    /**
22
     * @var string
23
     */
24
    private string $clientName = 'silverstripeconfig';
0 ignored issues
show
introduced by
The private property $clientName is not used, and could be removed.
Loading history...
25
26
    /**
27
     * @config
28
     * @var string
29
     */
30
    private static $client_title = 'Silverstripe Configuration';
0 ignored issues
show
introduced by
The private property $client_title is not used, and could be removed.
Loading history...
31
32
    /**
33
     * @config
34
     * @var array
35
     */
36
    private static $data_config = [];
0 ignored issues
show
introduced by
The private property $data_config is not used, and could be removed.
Loading history...
37
38
    /**
39
     * @var array
40
     */
41
    private $warnings = [];
42
43
    /**
44
     * @var array
45
     */
46
    private $clientData = [];
47
48
    public function getWarnings($data)
49
    {
50
        //Injector::inst()->get(LoggerInterface::class)->info(print_r($data, true));
51
        $this->clientData = $data;
52
        $this->checkEnvironment();
53
        $this->checkDefaultAdmin();
54
        $allWarnings = $this->warnings;
55
        $this->extend('updateWarnings', $allWarnings, $data);
56
        return (count($allWarnings) > 0) ? $allWarnings : false;
57
    }
58
59
    public function getReport($data)
60
    {
61
        $this->clientData = $data;
62
        return $this->generateReportsHTML();
63
    }
64
65
    private function checkDefaultAdmin()
66
    {
67
        $config = $this->config()->get('data_config');
68
        if ((isset($config['warnings']['required_default_admin'])) && (isset($this->clientData['defaultadmin']))) {
69
            $adminSet = $this->clientData['defaultadmin']['value'];
70
            $requiredAdmin = $config['warnings']['required_default_admin'];
71
            if ($adminSet !== $requiredAdmin) {
72
                $this->warnings[] = _t(
73
                    __CLASS__ . '.envwarning',
74
                    'Default admin credentials are present in the environment'
75
                );
76
            }
77
        }
78
    }
79
80
    private function checkEnvironment()
81
    {
82
        $config = $this->config()->get('data_config');
83
        if ((isset($config['warnings']['required_environment'])) && (isset($this->clientData['envtype']))) {
84
            $envType = $this->clientData['envtype']['value'];
85
            $requiredEnv = $config['warnings']['required_environment'];
86
            if ($envType !== $requiredEnv) {
87
                $this->warnings[] = _t(
88
                    __CLASS__ . '.envwarning',
89
                    'Environment type is not correct. Required: {required}.  Actual: {actual}',
90
                    [
91
                        'required' => $requiredEnv,
92
                        'actual' => $envType
93
                    ]
94
                );
95
            }
96
        }
97
    }
98
99
    private function generateReportsHTML()
100
    {
101
        $data = $this->clientData;
102
        $variables = ArrayList::create();
103
104
        foreach ($data as $id => $values) {
105
            $variables->push(ArrayData::create([
106
                'Variable' => $values['label'],
107
                'Value' => $values['value']
108
            ]));
109
        }
110
111
        $viewer = new SSViewer('BiffBangPow/SSMonitor/Server/Module/SSConfiguration');
112
        return $viewer->process(ArrayData::create([
113
            'Title' => $this->getClientTitle(),
114
            'Variables' => $variables
115
        ]));
116
    }
117
118
119
}
120