DomainHint   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 43
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validateContent() 0 7 2
A sanitizeContent() 0 4 1
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\mdui;
6
7
use SimpleSAML\Assert\Assert;
8
use SimpleSAML\SAML2\Exception\InvalidArgumentException;
9
use SimpleSAML\SAML2\Exception\ProtocolViolationException;
10
use SimpleSAML\SAML2\XML\StringElementTrait;
11
use SimpleSAML\XML\SchemaValidatableElementInterface;
12
use SimpleSAML\XML\SchemaValidatableElementTrait;
13
14
use function filter_var;
15
use function preg_replace;
16
use function rtrim;
17
use function sprintf;
18
19
/**
20
 * Class implementing DomainHint.
21
 *
22
 * @package simplesamlphp/saml2
23
 */
24
final class DomainHint extends AbstractMduiElement implements SchemaValidatableElementInterface
25
{
26
    use SchemaValidatableElementTrait;
27
    use StringElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\SAML2\XML\StringElementTrait requires some properties which are not provided by SimpleSAML\SAML2\XML\mdui\DomainHint: $localName, $namespaceURI
Loading history...
28
29
30
    /**
31
     * @param string $content
32
     */
33
    public function __construct(string $content)
34
    {
35
        $this->setContent($content);
36
    }
37
38
39
    /**
40
     * Sanitize the content of the element.
41
     *
42
     * @param string $content  The unsanitized textContent
43
     * @throws \Exception on failure
44
     * @return string
45
     */
46
    protected function sanitizeContent(string $content): string
47
    {
48
        // Remove prefixed schema and/or trailing whitespace + forward slashes
49
        return rtrim(preg_replace('#^http[s]?://#i', '', $content), " \n\r\t\v\x00/");
50
    }
51
52
53
    /**
54
     * Validate the content of the element.
55
     *
56
     * @param string $content  The value to go in the XML textContent
57
     * @throws \Exception on failure
58
     * @return void
59
     */
60
    protected function validateContent(string $content): void
61
    {
62
        $sanitizedContent = $this->sanitizeContent($content);
63
        Assert::notWhitespaceOnly($sanitizedContent, ProtocolViolationException::class);
64
65
        if (!filter_var($sanitizedContent, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)) {
66
            throw new InvalidArgumentException(sprintf('DomainHint is not a valid hostname;  %s', $sanitizedContent));
67
        }
68
    }
69
}
70