Certificates::invokeTest()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 56
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 32
c 0
b 0
f 0
nc 6
nop 0
dl 0
loc 56
rs 9.408

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