ILIAS  trunk Revision v12.0_alpha-1613-gae4c99ebb18
ConnectionTester.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
25use ilLogger;
26
27readonly class ConnectionTester
28{
29 public function __construct(
30 private \ilSetting $settings,
31 private Factory $ui_factory,
32 private ilLogger $logger,
33 ) {
34 }
35
39 public function testConnection(string $ext_uid, string $soap_pw, bool $new_user): array
40 {
41 $client = (new SoapAuthEndpoint($this->settings))->createValidationClient();
42
43 try {
44 $result = $client->validateSession($ext_uid, $soap_pw, $new_user);
45 } catch (\SoapFault $e) {
46 $this->logger->error('SOAP auth connection test failed: ' . $e->getMessage());
47 $this->logger->error($e->getTraceAsString());
48
49 return [
50 $this->ui_factory->messageBox()->failure(
51 'SOAP Authentication Call Error: ' . $e->getMessage()
52 )
53 ];
54 }
55
56 $validation = $result->validation;
57 $is_valid = (bool) ($validation['valid'] ?? false);
58 $status = $is_valid
59 ? $this->ui_factory->messageBox()->success('SOAP authentication succeeded.')
60 : $this->ui_factory->messageBox()->info('SOAP authentication returned invalid credentials.');
61
62 return [
63 $status,
64 $this->ui_factory->listing()->descriptive([
65 'Valid' => $is_valid ? 'true' : 'false',
66 'First Name' => (string) ($validation['firstname'] ?? ''),
67 'Last Name' => (string) ($validation['lastname'] ?? ''),
68 'Email' => (string) ($validation['email'] ?? ''),
69 'Request XML' => $this->renderXmlBlock($result->request),
70 'Response XML' => $this->renderXmlBlock($result->response),
71 ]),
72 ];
73 }
74
75 private function renderXmlBlock(string $xml): Component
76 {
77 return $this->ui_factory->legacy()->content(
78 $this->formatXmlForDisplay($xml)
79 );
80 }
81
82 private function formatXmlForDisplay(string $xml): string
83 {
84 if (trim($xml) === '') {
85 return '(no XML captured)';
86 }
87
88 $formatted = $this->prettyPrintXml($xml);
89 $escaped = htmlspecialchars($formatted, ENT_QUOTES);
90 $escaped = str_replace(' ', '&nbsp;', $escaped);
91
92 return nl2br($escaped, false);
93 }
94
95 private function prettyPrintXml(string $xml): string
96 {
97 if (trim($xml) === '') {
98 return '';
99 }
100
101 $dom = new \DOMDocument('1.0', 'UTF-8');
102 $dom->preserveWhiteSpace = false;
103 $dom->formatOutput = true;
104
105 if (@$dom->loadXML($xml) === false) {
106 return str_replace(['>', '" '], [">\n", "\"\n "], $xml);
107 }
108
109 return (string) $dom->saveXML();
110 }
111}
testConnection(string $ext_uid, string $soap_pw, bool $new_user)
__construct(private \ilSetting $settings, private Factory $ui_factory, private ilLogger $logger,)
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
Component logger with individual log levels by component id.
ILIAS Setting Class.
$soap_pw
$ext_uid
$new_user
$client
A component is the most general form of an entity in the UI.
Definition: Component.php:28
This is how the factory for UI elements looks.
Definition: Factory.php:38