Completed
Push — master ( 39c9e0...aa0be4 )
by Tim
02:09
created

Remote::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 16
rs 9.4285
1
<?php
2
3
namespace SimpleSAML\Module\monitor\TestCase\Cert;
4
5
use \SimpleSAML\Module\monitor\State as State;
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
10
// We're cheating here, because this TestCase doesn't decent from Cert, like Data and File do.
11
final class Remote extends \SimpleSAML\Module\monitor\TestCaseFactory
12
{
13
    /*
14
     * @var string|null
15
     */
16
    private $connectString = null;
17
18
    /*
19
     * @var resource|null
20
     */
21
    private $context = null;
22
23
    /*
24
     * @param TestData $testData
25
     */
26
    protected function initialize($testData)
27
    {
28
        $hostname = $testData->getInput('hostname');
29
        $port = $testData->getInput('port');
30
        $this->setCategory($testData->getInput('category'));
31
        $this->connectString = 'ssl://' . $hostname . ':' . $port;
32
        $this->context = stream_context_create([
33
            "ssl" => array(
34
                "capture_peer_cert" => true,
35
                "verify_peer" => false,
36
                "verify_peer_name" => false
37
            )
38
        ]);
39
40
        parent::initialize($testData);
41
    }
42
43
    protected function invokeTest()
44
    {
45
        $testsuite = $this->getTestSuite();
46
        $testResult = new TestResult();
47
48
        $testData = new TestData([
49
            'uri' => $this->connectString,
50
            'context' => $this->context
51
        ]);
52
        $connTest = new TestCase\Network\ConnectUri(
53
            $testsuite,
54
            $testData
55
        );
56
        $connTestResult = $connTest->getTestResult();
57
58
        $connState = $testResult->getState();
59
60
        if ($connState === State::OK) { // Connection OK
61
            $connection = $connTestResult->getOutput('connection');
62
            $certData = $connTestResult->getOutput('certData');
63
64
            if ($certData !== null) {
65
                $testData = new TestData([
66
                    'certData' => $certData,
67
                    'category' => $this->getCategory()
68
                ]);
69
                
70
                $certTest = new TestCase\Cert\Data(
71
                    $testsuite,
72
                    $testData
73
                );
74
                $certTestResult = $certTest->getTestResult();
75
                $testResult->setState($certTestResult->getState());
76
                $testResult->setMessage($certTestResult->getMessage());
77
            } else {
78
                $testResult->setState(State::SKIPPED);
79
                $testResult->setMessage(State::SKIPPED, $this->getCategory(), $this->getSubject(), 'Unable to capture peer certificate');
80
            }
81
        } else {
82
            $testResult->setState(State::FATAL);
83
            $testResult->setMessage($connTestResult->getMessage());
84
        }
85
        $this->setTestResult($testResult);
86
    }
87
88
    unset($connTestResult, $certTestResult);
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_UNSET, expecting T_FUNCTION or T_CONST
Loading history...
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
Coding Style introduced by
The visibility should be declared for property $connTestResult.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
89
}
90