Completed
Push — feature/more-metadata-values ( 04e0cc...df50c8 )
by Daan van
05:57 queued 03:45
created

NameIdFormat   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 2
cbo 1
dl 0
loc 144
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A unspecified() 0 4 1
A emailAddress() 0 4 1
A x509SubjectName() 0 4 1
A windowsDomainQualifiedName() 0 4 1
A kerberosPrincipalName() 0 4 1
A entity() 0 4 1
A persistent() 0 4 1
A transient() 0 4 1
A equals() 0 4 1
A getFormat() 0 4 1
A deserialize() 0 6 1
A serialize() 0 4 1
A __toString() 0 4 1
1
<?php
2
3
namespace OpenConext\Value\Saml;
4
5
use OpenConext\Value\Assert\Assertion;
6
use OpenConext\Value\Serializable;
7
8
/**
9
 * @SuppressWarnings(PHPMD.TooManyPublicMethods) this is due to the various named constructors
10
 */
11
final class NameIdFormat implements Serializable
12
{
13
    /**
14
     * The various types of Name Identifier Format Identifiers as defined in section 8.3 of
15
     * Assertions and Protocols for the OASIS Security Assertion Markup Language (SAML) V2.0
16
     *
17
     * @see https://docs.oasis-open.org/security/saml/v2.0/saml-core-2.0-os.pdf
18
     */
19
    const UNSPECIFIED                   = 'urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified';
20
    const EMAIL_ADDRESS                 = 'urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress';
21
    const X509_SUBJECT_NAME             = 'urn:oasis:names:tc:SAML:1.1:nameid-format:X509SubjectName';
22
    const WINDOWS_DOMAIN_QUALIFIED_NAME = 'urn:oasis:names:tc:SAML:1.1:nameid-format:WindowsDomainQualifiedName';
23
    const KERBEROS_PRINCIPLE_NAME       = 'urn:oasis:names:tc:SAML:2.0:nameid-format:kerberos';
24
    const ENTITY_IDENTIFIER             = 'urn:oasis:names:tc:SAML:2.0:nameid-format:entity';
25
    const PERSISTENT_IDENTIFIER         = 'urn:oasis:names:tc:SAML:2.0:nameid-format:persistent';
26
    const TRANSIENT_IDENTIFIER          = 'urn:oasis:names:tc:SAML:2.0:nameid-format:transient';
27
28
    /**
29
     * @var array
30
     */
31
    private static $validFormats = array(
32
        self::UNSPECIFIED,
33
        self::EMAIL_ADDRESS,
34
        self::X509_SUBJECT_NAME,
35
        self::WINDOWS_DOMAIN_QUALIFIED_NAME,
36
        self::KERBEROS_PRINCIPLE_NAME,
37
        self::ENTITY_IDENTIFIER,
38
        self::PERSISTENT_IDENTIFIER,
39
        self::TRANSIENT_IDENTIFIER
40
    );
41
42
    /**
43
     * @var string
44
     */
45
    private $format;
46
47
    /**
48
     ** @param string $format one of the valid NameID formats
49
     */
50
    public function __construct($format)
51
    {
52
        Assertion::inArray($format, self::$validFormats);
53
54
        $this->format = $format;
55
    }
56
57
    /**
58
     * @return NameIdFormat
59
     */
60
    public static function unspecified()
61
    {
62
        return new self(self::UNSPECIFIED);
63
    }
64
65
    /**
66
     * @return NameIdFormat
67
     */
68
    public static function emailAddress()
69
    {
70
        return new self(self::EMAIL_ADDRESS);
71
    }
72
73
    /**
74
     * @return NameIdFormat
75
     */
76
    public static function x509SubjectName()
77
    {
78
        return new self(self::X509_SUBJECT_NAME);
79
    }
80
81
    /**
82
     * @return NameIdFormat
83
     */
84
    public static function windowsDomainQualifiedName()
85
    {
86
        return new self(self::WINDOWS_DOMAIN_QUALIFIED_NAME);
87
    }
88
89
    /**
90
     * @return NameIdFormat
91
     */
92
    public static function kerberosPrincipalName()
93
    {
94
        return new self(self::KERBEROS_PRINCIPLE_NAME);
95
    }
96
97
    /**
98
     * @return NameIdFormat
99
     */
100
    public static function entity()
101
    {
102
        return new self(self::PERSISTENT_IDENTIFIER);
103
    }
104
105
    /**
106
     * @return NameIdFormat
107
     */
108
    public static function persistent()
109
    {
110
        return new self(self::PERSISTENT_IDENTIFIER);
111
    }
112
113
    /**
114
     * @return NameIdFormat
115
     */
116
    public static function transient()
117
    {
118
        return new self(self::TRANSIENT_IDENTIFIER);
119
    }
120
121
    /**
122
     * @param NameIdFormat $other
123
     * @return bool
124
     */
125
    public function equals(NameIdFormat $other)
126
    {
127
        return $this->format === $other->format;
128
    }
129
130
    /**
131
     * @return string
132
     */
133
    public function getFormat()
134
    {
135
        return $this->format;
136
    }
137
138
    public static function deserialize($data)
139
    {
140
        Assertion::nonEmptyString($data, 'data');
141
142
        return new self($data);
143
    }
144
145
    public function serialize()
146
    {
147
        return $this->format;
148
    }
149
150
    public function __toString()
151
    {
152
        return $this->format;
153
    }
154
}
155