Passed
Pull Request — master (#374)
by Tim
02:35
created

DomainTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
dl 0
loc 23
rs 10
c 1
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A validDomain() 0 10 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\Assert;
6
7
use SimpleSAML\Assert\AssertionFailedException;
8
use SimpleSAML\SAML2\Exception\ProtocolViolationException;
9
10
/**
11
 * @package simplesamlphp/saml2
12
 */
13
trait DomainTrait
14
{
15
    private static string $domain_regex = '/^
16
      (?!\-)
17
      (?:(?:[a-zA-Z\d][a-zA-Z\d\-]{0,61})?[a-zA-Z\d]\.){0,126}
18
      (?!\d+)[a-zA-Z\d]{1,63}
19
      $/Dxi';
20
21
22
    /**
23
     * @param string $value
24
     * @param string $message
25
     */
26
    protected static function validDomain(string $value, string $message = ''): void
27
    {
28
        try {
29
            parent::regex(
30
                $value,
31
                self::$domain_regex,
32
                $message ?: '%s is not a valid domain name',
33
            );
34
        } catch (AssertionFailedException $e) {
35
            throw new ProtocolViolationException($e->getMessage());
36
        }
37
    }
38
}
39