MonitorController::main()   B
last analyzed

Complexity

Conditions 6
Paths 12

Size

Total Lines 37
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

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

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

156
        return /** @scrutinizer ignore-deprecated */ JsonResponse::create(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
157
            ['overall' => $this->healthInfo[$this->state][0],
158
            'results' => $results],
159
            $this->responseCode
160
        );
161
    }
162
163
164
    /**
165
     * @return \SimpleSAML\XHTML\Template
166
     */
167
    private function processText(): Template
168
    {
169
        $t = new Template($this->config, 'monitor:monitor.text.twig');
170
171
        if ($this->state === State::OK) {
172
            $t->data['status'] = 'OK';
173
        } elseif ($this->state === State::WARNING) {
174
            $t->data['status'] = 'WARN';
175
        } else {
176
            $t->data['status'] = 'FAIL';
177
        }
178
        return $t;
179
    }
180
}
181