Passed
Push — master ( 9792fc...04e5e0 )
by Tim
06:59
created

MonitorController::main()   B

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
namespace SimpleSAML\Module\monitor\Controller;
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 MonitorController
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 = $this->moduleConfig->getInteger('warningStatusCode', 202);
104
        } else {
105
            $this->responseCode = $this->moduleConfig->getInteger('errorStatusCode', 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(
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

152
        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...
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