Passed
Branch master (4b23d6)
by Tim
04:40
created

Controller::main()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 37
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 37
rs 8.8657
c 0
b 0
f 0
cc 6
nc 12
nop 1
1
<?php
2
3
namespace SimpleSAML\Module\Monitor;
4
5
use SimpleSAML\Configuration;
6
use SimpleSAML\Module\Monitor\DependencyInjection;
7
use SimpleSAML\Module\Monitor\State;
8
use SimpleSAML\Module\Monitor\TestConfiguration;
9
use SimpleSAML\Module\Monitor\Monitor;
10
use SimpleSAML\XHTML\Template;
11
use Symfony\Component\HttpFoundation\JsonResponse;
12
use Symfony\Component\HttpFoundation\Response;
13
14
/**
15
 * Controller class for the monitor module.
16
 *
17
 * This class serves the different views available in the module.
18
 *
19
 * @package SimpleSAML\Module\monitor
20
 */
21
class Controller
22
{
23
    /** @var \SimpleSAML\Configuration */
24
    protected $config;
25
26
    /** @var \SimpleSAML\Configuration */
27
    protected $moduleConfig;
28
29
    /** @var \SimpleSAML\Configuration */
30
    protected $authsourceConfig;
31
32
    /** @var \SimpleSAML\Module\Monitor\DependencyInjection */
33
    protected $serverVars;
34
35
    /** @var \SimpleSAML\Module\Monitor\DependencyInjection */
36
    protected $requestVars;
37
38
    /** @var array */
39
    private $healthInfo = [
40
        State::SKIPPED => ['SKIPPED', 'yellow'],
41
        State::FATAL   => ['FATAL',   'purple'],
42
        State::ERROR   => ['NOK',     'red'   ],
43
        State::NOSTATE => ['NOSTATE',   'cyan'],
44
        State::WARNING => ['WARNING', 'orange'],
45
        State::OK      => ['OK',      'green' ]
46
    ];
47
48
    /** @var \SimpleSAML\Module\Monitor\TestConfiguration */
49
    protected $testConfiguration;
50
51
    /** @var int */
52
    protected $state;
53
54
    /** @var int */
55
    protected $responseCode = 200;
56
57
    /** @var \SimpleSAML\Module\Monitor\Monitor */
58
    protected $monitor;
59
60
61
    /**
62
     * Controller constructor.
63
     *
64
     * It initializes the global configuration and auth source configuration for the controllers implemented here.
65
     *
66
     * @param \SimpleSAML\Configuration              $config The configuration to use by the controllers.
67
     * @throws \Exception
68
     */
69
    public function __construct(Configuration $config)
70
    {
71
        $this->config = $config;
72
        $this->moduleConfig = Configuration::getOptionalConfig('module_monitor.php');
73
        $this->authsourceConfig = Configuration::getOptionalConfig('authsources.php');
74
75
        $this->serverVars = new DependencyInjection($_SERVER);
76
        $this->requestVars = new DependencyInjection($_REQUEST);
77
78
        $this->testConfiguration = new TestConfiguration(
79
            $this->serverVars,
80
            $this->requestVars,
81
            $this->config,
82
            $this->authsourceConfig,
83
            $this->moduleConfig
84
        );
85
        $this->monitor = new Monitor($this->testConfiguration);
86
    }
87
88
89
    /**
90
     * Display the main monitoring page.
91
     *
92
     * @param string $format  Default is XHTML output
93
     * @return \Symfony\Component\HttpFoundation\Response
94
     */
95
    public function main(string $format): Response
96
    {
97
        $this->monitor->invokeTestSuites();
98
99
        $this->state = $this->monitor->getState();
100
        if ($this->state === State::OK) {
101
            $this->responseCode = 200;
102
        } elseif ($this->state === State::WARNING) {
103
            $this->responseCode = 417;
104
        } else {
105
            $this->responseCode = 500;
106
        }
107
108
        $results = $this->monitor->getResults();
109
110
        switch ($format) {
111
            case 'xml':
112
                $t = $this->processXml();
113
                break;
114
            case 'json':
115
                return $this->processJson($results);
116
            case 'text':
117
                $t = $this->processText();
118
                break;
119
            default:
120
                $t = new Template($this->config, 'monitor:monitor.twig');
121
                break;
122
        }
123
124
        $t->data['header'] = 'Monitor';
125
        $t->data = array_merge($t->data, $results);
126
127
        $t->data['overall'] = $this->state;
128
        $t->data['healthInfo'] = $this->healthInfo;
129
        $t->data['responseCode'] = $this->responseCode;
130
131
        return $t;
132
    }
133
134
135
    /**
136
     * @return \SimpleSAML\XHTML\Template
137
     */
138
    private function processXml(): Template
139
    {
140
        $t = new Template($this->config, 'monitor:monitor.xml.twig');
141
        $t->headers->set('Content-Type', 'text/xml');
142
        return $t;
143
    }
144
145
146
    /**
147
     * @param array $results
148
     * @return \Symfony\Component\HttpFoundation\JsonResponse
149
     */
150
    private function processJson(array $results): JsonResponse
151
    {
152
        return JsonResponse::create(
153
            ['overall' => $this->healthInfo[$this->state][0],
154
            'results' => $results],
155
            $this->responseCode
156
        );
157
    }
158
159
160
    /**
161
     * @return \SimpleSAML\XHTML\Template
162
     */
163
    private function processText(): Template
164
    {
165
        $t = new Template($this->config, 'monitor:monitor.text.twig');
166
167
        if ($this->state === State::OK) {
168
            $t->data['status'] = 'OK';
169
        } elseif ($this->state === State::WARNING) {
170
            $t->data['status'] = 'WARN';
171
        } else {
172
            $t->data['status'] = 'FAIL';
173
        }
174
        return $t;
175
    }
176
}
177