Passed
Push — master ( a4e022...b6d9f7 )
by Tim
04:12
created

Controller::processXml()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace SimpleSAML\Module\monitor;
4
5
use \SimpleSAML\Modules\Monitor\DependencyInjection as DependencyInjection;
6
use \SimpleSAML\Modules\Monitor\State as State;
7
use \SimpleSAML\Modules\Monitor\TestConfiguration as TestConfiguration;
8
use \SimpleSAML\Modules\Monitor\Monitor as Monitor;
9
use \SimpleSAML\Configuration as ApplicationConfiguration;
10
use \Symfony\Component\HttpFoundation\JsonResponse;
11
12
/**
13
 * Controller class for the monitor module.
14
 *
15
 * This class serves the different views available in the module.
16
 *
17
 * @package SimpleSAML\Module\monitor
18
 */
19
class Controller
20
{
21
    /** @var \SimpleSAML\Configuration */
22
    protected $config;
23
24
    /** @var \SimpleSAML\Configuration */
25
    protected $moduleConfig;
26
27
    /** @var \SimpleSAML\Configuration */
28
    protected $authsourceConfig;
29
30
    /** @var \SimpleSAML\Modules\Monitor\DependencyInjection */
31
    protected $serverVars;
32
33
    /** @var \SimpleSAML\Modules\Monitor\DependencyInjection */
34
    protected $requestVars;
35
36
    /** @var array */
37
    static private $healthInfo = [
38
        State::SKIPPED => ['SKIPPED', 'yellow'],
39
        State::FATAL   => ['FATAL',   'purple'],
40
        State::ERROR   => ['NOK',     'red'   ],
41
        State::NOSTATE   => ['NOSTATE',   'cyan'  ],
42
        State::WARNING => ['WARNING', 'orange'],
43
        State::OK      => ['OK',      'green' ]
44
    ];
45
46
    /** @var \SimpleSAML\Module\Monitor\TestConfiguration */
0 ignored issues
show
Bug introduced by
The type SimpleSAML\Module\Monitor\TestConfiguration 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...
47
    protected $testConfiguration;
48
49
    /** @var int */
50
    protected $state;
51
52
    /** @var int */
53
    protected $reponseCode = 200;
54
55
    /** @var \SimpleSAML\Module\Monitor\Monitor */
0 ignored issues
show
Bug introduced by
The type SimpleSAML\Module\Monitor\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...
56
    protected $monitor;
57
58
59
    /**
60
     * Controller constructor.
61
     *
62
     * It initializes the global configuration and auth source configuration for the controllers implemented here.
63
     *
64
     * @param \SimpleSAML\Configuration              $config The configuration to use by the controllers.
65
     * @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...
66
     *
67
     * @throws \Exception
68
     */
69
    public function __construct(
70
        Configuration $config
0 ignored issues
show
Bug introduced by
The type SimpleSAML\Module\monitor\Configuration 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...
71
    ) {
72
        $this->config = $config;
73
        $this->moduleConfig = ApplicationConfiguration::getOptionalConfig('module_monitor.php');
74
        $this->authsourceConfig = ApplicationConfiguration::getOptionalConfig('authsources.php');
75
76
        $this->serverVars = new DependencyInjection($_SERVER);
77
        $this->requestVars = new DependencyInjection($_REQUEST);
78
79
        $this->testConfiguration = new TestConfiguration($this->serverVars, $this->requestVars, $this->config, $this->authsourceConfig, $this->moduleConfig);
0 ignored issues
show
Documentation Bug introduced by
It seems like new SimpleSAML\Modules\M...g, $this->moduleConfig) of type SimpleSAML\Modules\Monitor\TestConfiguration is incompatible with the declared type SimpleSAML\Module\Monitor\TestConfiguration of property $testConfiguration.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
80
        $this->monitor = new Monitor($this->testConfiguration);
0 ignored issues
show
Documentation Bug introduced by
It seems like new SimpleSAML\Modules\M...his->testConfiguration) of type SimpleSAML\Modules\Monitor\Monitor is incompatible with the declared type SimpleSAML\Module\Monitor\Monitor of property $monitor.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
81
82
        $this->state = $this->monitor->getState();
83
        if ($this->state === State::OK) {
84
            $this->responseCode = 200;
0 ignored issues
show
Bug Best Practice introduced by
The property responseCode does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
85
        } else if ($this->state === State::WARNING) {
86
            $this->responseCode = 417;
87
        } else {
88
            $this->responseCode = 500;
89
        }
90
    }
91
92
93
    /**
94
     * Display the main monitoring page.
95
     *
96
     * @param string $format  Default is XHTML output
97
     * @return \SimpleSAML\XHTML\Template
98
     */
99
    public function main($format)
0 ignored issues
show
Unused Code introduced by
The parameter $format is not used and could be removed. ( Ignorable by Annotation )

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

99
    public function main(/** @scrutinizer ignore-unused */ $format)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
100
    {
101
        $this->monitor->invokeTestSuites();
102
        $results = $this->monitor->getResults();
103
104
        switch ($this->requestVars->get('output')) {
105
            case 'xml':
106
                $t = $this->processXml();
107
                break;
108
            case 'json':
109
                return $this->processJson($results);
110
            case 'text':
111
                $t = $this->processText();
112
                break;
113
            default:
114
                $t = new \SimpleSAML\XHTML\Template($globalConfig, 'monitor:monitor.php');
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $globalConfig seems to be never defined.
Loading history...
115
                break;
116
        }
117
118
        $t->data['header'] = 'Monitor';
119
        $t->data = array_merge($t->data, $results);
120
121
        $t->data['overall'] = $this->state;
122
        $t->data['healthInfo'] = $this->healthInfo;
123
        $t->data['responseCode'] = $this->responseCode;
124
125
        $this->setStatusCode($this->responseCode);
0 ignored issues
show
Bug introduced by
The method setStatusCode() does not exist on SimpleSAML\Module\monitor\Controller. ( Ignorable by Annotation )

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

125
        $this->/** @scrutinizer ignore-call */ 
126
               setStatusCode($this->responseCode);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
126
        return $t;
127
    }
128
129
130
    /**
131
     * @return \SimpleSAML\XHTML\Template
132
     */
133
    private function processXml() {
134
        $t = new \SimpleSAML\XHTML\Template($this->config, 'monitor:monitor.xml.php');
135
        $t->headers->set('Content-Type', 'text/xml');
136
        return $t;
137
    }
138
139
140
    /**
141
     * @param array $results
142
     * @return \SimpleSAML\XHTML\Template
143
     */
144
    private function processJson(array $results) {
145
        return JsonResponse::create(['overall' => $this->healthInfo[$this->state][0], 'results' => $results], $this->responseCode);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Symfony\Component...), $this->responseCode) returns the type Symfony\Component\HttpFoundation\JsonResponse which is incompatible with the documented return type SimpleSAML\XHTML\Template.
Loading history...
146
    }
147
148
149
    /**
150
     * @return \SimpleSAML\XHTML\Template
151
     */
152
    private function processText() {
153
        $t = new \SimpleSAML\XHTML\Template($this->config, 'monitor:monitor.text.php');
154
155
        if ($this->state === State::OK) {
156
            $t->data['status'] = 'OK';
157
        } else if ($this->state === State::WARNING) {
158
            $t->data['status'] = 'WARN';
159
        } else {
160
            $t->data['status'] = 'FAIL';
161
        }
162
        return $t;
163
    }
164
}
165