Passed
Pull Request — master (#31)
by Tim
01:53
created

URITrait::getUri()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Assert;
6
7
use GuzzleHttp\Psr7\Exception\MalformedUriException;
8
use GuzzleHttp\Psr7\Uri;
9
use InvalidArgumentException;
10
11
use function sprintf;
12
use function strlen;
13
use function substr;
14
15
/**
16
 * @package simplesamlphp/assert
17
 */
18
trait URITrait
19
{
20
    private static Uri $uri;
21
22
    /***********************************************************************************
23
     *  NOTE:  Custom assertions may be added below this line.                         *
24
     *         They SHOULD be marked as `protected` to ensure the call is forced       *
25
     *          through __callStatic().                                                *
26
     *         Assertions marked `public` are called directly and will                 *
27
     *          not handle any custom exception passed to it.                          *
28
     ***********************************************************************************/
29
30
31
    /**
32
     */
33
    protected static function validURN(string $value, string $message = ''): string
0 ignored issues
show
introduced by
Expected 2 blank lines between class members, found 1.
Loading history...
34
    {
35
        try {
36
            self::$uri = new Uri($value);
37
        } catch (MalformedUriException $e) {
38
            throw new InvalidArgumentException(sprintf(
39
                $message ?: '\'%s\' is not a valid RFC3986 compliant URI',
40
                $value,
41
            ));
42
        }
43
44
        if (
45
            self::$uri->getScheme() !== 'urn'
46
            || self::$uri->getPath() !== substr($value, strlen(self::$uri->getScheme()) + 1)
47
        ) {
48
            throw new InvalidArgumentException(sprintf(
49
                $message ?: '\'%s\' is not a valid RFC8141 compliant URN',
50
                $value,
51
            ));
52
        }
53
54
        return $value;
55
    }
56
57
58
    /**
59
     */
60
    protected static function validURL(string $value, string $message = ''): string
61
    {
62
        try {
63
            self::$uri = new Uri($value);
64
        } catch (MalformedUriException $e) {
65
            throw new InvalidArgumentException(sprintf(
66
                $message ?: '\'%s\' is not a valid RFC3986 compliant URI',
67
                $value,
68
            ));
69
        }
70
71
        if (self::$uri->getScheme() !== 'http' && self::$uri->getScheme() !== 'https') {
72
            throw new InvalidArgumentException(sprintf(
73
                $message ?: '\'%s\' is not a valid RFC2396 compliant URL',
74
                $value,
75
            ));
76
        }
77
78
        return $value;
79
    }
80
81
82
    /**
83
     */
84
    protected static function validURI(string $value, string $message = ''): string
85
    {
86
        try {
87
            $self::$uri = new Uri($value);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $self seems to be never defined.
Loading history...
88
        } catch (MalformedUriException $e) {
89
            throw new InvalidArgumentException(sprintf(
90
                $message ?: '\'%s\' is not a valid RFC3986 compliant URI',
91
                $value,
92
            ));
93
        }
94
95
        return $value;
96
    }
97
98
99
    /**
100
     * For convenience and efficiency, to get the Uri-object from the last assertion.
101
     */
102
    public static function getUri(): Uri
103
    {
104
        return self::$uri;
105
    }
106
}
107