Passed
Branch monitor-3.0.x (ef3617)
by Tim
04:19
created

Controller   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 71
dl 0
loc 156
rs 10
c 1
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A processXml() 0 5 1
A processText() 0 12 3
B main() 0 37 6
A __construct() 0 17 1
A processJson() 0 6 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
     * @param \SimpleSAML\Monitor                    $monitor The monitor object to use by the controllers.
0 ignored issues
show
Bug introduced by
The type SimpleSAML\Monitor was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
68
     *
69
     * @throws \Exception
70
     */
71
    public function __construct(Configuration $config)
72
    {
73
        $this->config = $config;
74
        $this->moduleConfig = Configuration::getOptionalConfig('module_monitor.php');
75
        $this->authsourceConfig = Configuration::getOptionalConfig('authsources.php');
76
77
        $this->serverVars = new DependencyInjection($_SERVER);
78
        $this->requestVars = new DependencyInjection($_REQUEST);
79
80
        $this->testConfiguration = new TestConfiguration(
81
            $this->serverVars,
82
            $this->requestVars,
83
            $this->config,
84
            $this->authsourceConfig,
85
            $this->moduleConfig
86
        );
87
        $this->monitor = new Monitor($this->testConfiguration);
88
    }
89
90
91
    /**
92
     * Display the main monitoring page.
93
     *
94
     * @param string $format  Default is XHTML output
95
     * @return \Symfony\Component\HttpFoundation\Response
96
     */
97
    public function main(string $format): Response
98
    {
99
        $this->monitor->invokeTestSuites();
100
101
        $this->state = $this->monitor->getState();
102
        if ($this->state === State::OK) {
103
            $this->responseCode = 200;
104
        } elseif ($this->state === State::WARNING) {
105
            $this->responseCode = 417;
106
        } else {
107
            $this->responseCode = 500;
108
        }
109
110
        $results = $this->monitor->getResults();
111
112
        switch ($format) {
113
            case 'xml':
114
                $t = $this->processXml();
115
                break;
116
            case 'json':
117
                return $this->processJson($results);
118
            case 'text':
119
                $t = $this->processText();
120
                break;
121
            default:
122
                $t = new Template($this->config, 'monitor:monitor.twig');
123
                break;
124
        }
125
126
        $t->data['header'] = 'Monitor';
127
        $t->data = array_merge($t->data, $results);
128
129
        $t->data['overall'] = $this->state;
130
        $t->data['healthInfo'] = $this->healthInfo;
131
        $t->data['responseCode'] = $this->responseCode;
132
133
        return $t;
134
    }
135
136
137
    /**
138
     * @return \SimpleSAML\XHTML\Template
139
     */
140
    private function processXml(): Template
141
    {
142
        $t = new Template($this->config, 'monitor:monitor.xml.twig');
143
        $t->headers->set('Content-Type', 'text/xml');
144
        return $t;
145
    }
146
147
148
    /**
149
     * @param array $results
150
     * @return \Symfony\Component\HttpFoundation\JsonResponse
151
     */
152
    private function processJson(array $results): JsonResponse
153
    {
154
        return JsonResponse::create(
155
            ['overall' => $this->healthInfo[$this->state][0],
156
            'results' => $results],
157
            $this->responseCode
158
        );
159
    }
160
161
162
    /**
163
     * @return \SimpleSAML\XHTML\Template
164
     */
165
    private function processText(): Template
166
    {
167
        $t = new Template($this->config, 'monitor:monitor.text.twig');
168
169
        if ($this->state === State::OK) {
170
            $t->data['status'] = 'OK';
171
        } elseif ($this->state === State::WARNING) {
172
            $t->data['status'] = 'WARN';
173
        } else {
174
            $t->data['status'] = 'FAIL';
175
        }
176
        return $t;
177
    }
178
}
179