1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace SimpleSAML\Module\monitor\TestCase\Network; |
4
|
|
|
|
5
|
|
|
use \SimpleSAML\Module\monitor\State as State; |
6
|
|
|
use \SimpleSAML\Module\monitor\TestData as TestData; |
7
|
|
|
use \SimpleSAML\Module\monitor\TestResult as TestResult; |
8
|
|
|
|
9
|
|
|
final class ConnectUri extends \SimpleSAML\Module\monitor\TestCaseFactory |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @param integer|null |
13
|
|
|
*/ |
14
|
|
|
private $timeout = null; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param resource|null |
18
|
|
|
*/ |
19
|
|
|
private $context = null; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param string|null |
23
|
|
|
*/ |
24
|
|
|
private $uri = null; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var TestData $testData |
28
|
|
|
* |
29
|
|
|
* @return void |
30
|
|
|
*/ |
31
|
|
|
protected function initialize($testData) |
32
|
|
|
{ |
33
|
|
|
$uri = $testData->getInput('uri'); |
34
|
|
|
$context = $testData->getInput('context'); |
35
|
|
|
|
36
|
|
|
if (is_null($context)) { |
37
|
|
|
$context = stream_context_create(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$timeout = $testData->getInput('timeout'); |
41
|
|
|
$timeout = is_null($timeout) ? (int)ini_get("default_socket_timeout") : $timeout; |
42
|
|
|
|
43
|
|
|
$this->setUri($uri); |
44
|
|
|
$this->setContext($context); |
45
|
|
|
$this->setTimeout($timeout); |
46
|
|
|
|
47
|
|
|
parent::initialize($testData); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/* |
51
|
|
|
* @param string $uri |
52
|
|
|
* |
53
|
|
|
* @return void |
54
|
|
|
*/ |
55
|
|
|
private function setUri($uri) |
56
|
|
|
{ |
57
|
|
|
assert(is_string($uri)); |
58
|
|
|
$this->uri = $uri; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/* |
62
|
|
|
* @param resource $context |
63
|
|
|
* |
64
|
|
|
* @return void |
65
|
|
|
*/ |
66
|
|
|
private function setContext($context) |
67
|
|
|
{ |
68
|
|
|
assert(is_resource($context)); |
69
|
|
|
$this->context = $context; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/* |
73
|
|
|
* @param integer $timeout |
74
|
|
|
* |
75
|
|
|
* @return void |
76
|
|
|
*/ |
77
|
|
|
private function setTimeout($timeout) |
78
|
|
|
{ |
79
|
|
|
assert(is_int($timeout)); |
80
|
|
|
$this->timeout = $timeout; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/* |
84
|
|
|
* @return void |
85
|
|
|
*/ |
86
|
|
|
public function invokeTest() |
87
|
|
|
{ |
88
|
|
|
$connection = @stream_socket_client($this->uri, $errno, $errstr, $this->timeout, STREAM_CLIENT_CONNECT, $this->context); |
89
|
|
|
|
90
|
|
|
$testResult = new TestResult('Network connection', $this->uri); |
91
|
|
|
|
92
|
|
|
if (is_resource($connection)) { |
93
|
|
|
$params = stream_context_get_params($connection); |
94
|
|
|
|
95
|
|
|
$testResult->addOutput($connection, 'connection'); |
96
|
|
|
if (isSet($params['options']['ssl']['peer_certificate'])) { |
97
|
|
|
$certData = openssl_x509_parse($params['options']['ssl']['peer_certificate']); |
98
|
|
|
$testResult->addOutput($certData, 'certData'); |
99
|
|
|
} |
100
|
|
|
$testResult->setState(State::OK); |
101
|
|
|
} else { |
102
|
|
|
$testResult->setState(State::ERROR); |
103
|
|
|
$testResult->setMessage($errstr.' ('.$errno.')'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
<<<<<<< HEAD |
|
|
|
|
107
|
|
|
public function __destruct() |
108
|
|
|
{ |
109
|
|
|
if ($this->connection) { |
110
|
|
|
fclose($this->connection); |
111
|
|
|
} |
112
|
|
|
======= |
113
|
|
|
$this->setTestResult($testResult); |
114
|
|
|
>>>>>>> 3af6d28... First attempt on implementing TestResult |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.