Passed
Push — master ( 7c583e...84adb5 )
by Tim
02:21
created

DomainHint   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 6
c 2
b 0
f 1
dl 0
loc 40
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A validateContent() 0 5 2
A sanitizeContent() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\mdui;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\SAML2\Exception\InvalidArgumentException;
10
use SimpleSAML\XML\StringElementTrait;
11
12
use function filter_var;
13
14
/**
15
 * Class implementing DomainHint.
16
 *
17
 * @package simplesamlphp/saml2
18
 */
19
final class DomainHint extends AbstractMduiElement
20
{
21
    use StringElementTrait;
22
23
24
    /**
25
     * @param string $content
26
     */
27
    public function __construct(string $content)
28
    {
29
        $this->setContent($content);
30
    }
31
32
33
    /**
34
     * Sanitize the content of the element.
35
     *
36
     * @param string $content  The unsanitized textContent
37
     * @throws \Exception on failure
38
     * @return string
39
     */
40
    protected function sanitizeContent(string $content): string
41
    {
42
        // Remove prefixed schema and/or trailing whitespace + forward slashes
43
        return trimr(preg_replace('#^http[s]?://#i', '', $content), " \n\r\t\v\x00/");
0 ignored issues
show
Bug introduced by
The function trimr was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
        return /** @scrutinizer ignore-call */ trimr(preg_replace('#^http[s]?://#i', '', $content), " \n\r\t\v\x00/");
Loading history...
44
    }
45
46
47
    /**
48
     * Validate the content of the element.
49
     *
50
     * @param string $content  The value to go in the XML textContent
51
     * @throws \Exception on failure
52
     * @return void
53
     */
54
    protected function validateContent(string $content): void
55
    {
56
        Assert::notEmpty($content, 'DomainHint cannot be empty');
57
        if (!filter_var($content, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)) {
58
            throw new InvalidArgumentException(sprintf('DomainHint is not a valid hostname;  %s', $content));
59
        }
60
    }
61
}
62