Completed
Branch feature-unit-tests (9d0273)
by Tim
01:44
created

Configuration::invokeTest()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 52
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 30
nc 6
nop 0
dl 0
loc 52
rs 8.9408
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace SimpleSAML\Module\monitor\TestSuite;
4
5
use \SimpleSAML\Module\monitor\TestConfiguration as TestConfiguration;
6
use \SimpleSAML\Module\monitor\TestCase as TestCase;
7
use \SimpleSAML\Module\monitor\TestData as TestData;
8
use \SimpleSAML\Module\monitor\TestResult as TestResult;
9
use \SimpleSAML\Module\monitor\State as State;
10
use \SimpleSAML\Utils as Utils;
11
12
final class Configuration extends \SimpleSAML\Module\monitor\TestSuiteFactory
13
{
14
    /**
15
     * @param string|null
16
     */
17
    private $metadataCert = null;
18
19
    /**
20
     * @param string|null;
21
     */
22
    private $serverName = null;
23
24
    /**
25
     * @param integer|null;
26
     */
27
    private $serverPort = null;
28
29
    /**
30
     * @param integer|null;
31
     */
32
    private $certExpirationWarning = null;
33
34
    /**
35
     * @param TestConfiguration $configuration
36
     */
37
    public function __construct($configuration)
38
    {
39
        $globalConfig = $configuration->getGlobalConfig();
40
        $moduleConfig = $configuration->getModuleConfig();
41
        $serverVars = $configuration->getServerVars();
42
43
        $this->metadataCert = $globalConfig->getString('metadata.sign.certificate', null);
44
        $this->certExpirationWarning = $moduleConfig->getValue('certExpirationWarning', 28);
45
        $this->serverName = $serverVars->get('SERVER_NAME');
46
        $this->serverPort = $serverVars->get('SERVER_PORT');
47
        $this->setCategory('Configuration');
48
49
        parent::__construct($configuration);
50
    }
51
52
    /**
53
     * @return void
54
     */
55
    public function invokeTest()
56
    {
57
        // Check network connection to full public URL
58
        $input = [
59
            'uri' => 'ssl://'.$this->serverName.':'.$this->serverPort,
60
            'context' => stream_context_create([
61
                "ssl" => [
62
                    "capture_peer_cert" => true,
63
                    "verify_peer" => false,
64
                    "verify_peer_name" => false
65
                ]
66
            ]),
67
        ];
68
69
        $connTest = new TestCase\Network\ConnectUri(new TestData($input));
70
        $connTestResult = $connTest->getTestResult();
71
72
        $this->addTestResult($connTest->getTestResult());
73
74
        if ($connTestResult->getState() === State::OK) {
75
            // We were able to connect
76
            if (Utils\HTTP::isHTTPS()) {
77
                // Check Service Communications Certificate
78
                $certData = $connTestResult->getOutput('certData');
79
80
                $input = [
81
                    'category' => 'Service Communications Certificate',
82
                    'certData' => $certData,
83
                    'certExpirationWarning' => $this->certExpirationWarning,
84
                ];
85
86
                $certTest = new TestCase\Cert(new TestData($input));
87
                $this->addTestResult($certTest->getTestResult());
88
            }
89
        }
90
91
        // Check metadata signing certificate when available
92
        if (is_string($this->metadataCert)) {
93
            $input = array(
94
                'certFile' => Utils\Config::getCertPath($this->metadataCert),
95
                'category' => 'Metadata Signing Certificate',
96
                'certExpirationWarning' => $this->certExpirationWarning,
97
            );
98
            $testData = new TestData($input);
99
100
            $test = new TestCase\Cert\File($testData);
101
            $this->addTestResult($test->getTestResult());
102
        }
103
104
        $testResult = new TestResult('Configuration', '');
105
        $testResult->setState($this->calculateState());
106
        $this->setTestResult($testResult);
107
    }
108
}
109