Completed
Push — develop ( b46512...5ad542 )
by A.
04:22
created

AuthenticatedUser::createFrom()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 0
loc 8
rs 9.4286
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
3
/**
4
 * Copyright 2015 SURFnet B.V.
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace OpenConext\Profile\Entity;
20
21
use OpenConext\Profile\Assert;
22
use OpenConext\Profile\Value\EntityId;
23
use Surfnet\SamlBundle\SAML2\Attribute\AttributeSet;
24
use Surfnet\SamlBundle\SAML2\Response\AssertionAdapter;
25
26
final class AuthenticatedUser
27
{
28
    /**
29
     * @var string
30
     */
31
    private $nameId;
32
33
    /**
34
     * @var AttributeSet
35
     */
36
    private $attributes;
37
38
    /**
39
     * @var EntityId[]
40
     */
41
    private $authenticatingAuthorities;
42
43
    /**
44
     * @param AssertionAdapter $assertionAdapter
45
     * @param EntityId[] $authenticatingAuthorities
46
     * @return AuthenticatedUser
47
     */
48
    public static function createFrom(AssertionAdapter $assertionAdapter, array $authenticatingAuthorities)
49
    {
50
        return new self(
51
            $assertionAdapter->getNameId(),
52
            $assertionAdapter->getAttributeSet(),
53
            $authenticatingAuthorities
54
        );
55
    }
56
57
    /**
58
     * @param string $nameId
59
     * @param AttributeSet $attributes
60
     * @param EntityId[] $authenticatingAuthorities
61
     */
62
    private function __construct($nameId, AttributeSet $attributes, array $authenticatingAuthorities)
63
    {
64
        Assert::string($nameId);
65
        Assert::allIsInstanceOf($authenticatingAuthorities, '\OpenConext\Profile\Value\EntityId');
66
67
        $this->nameId                    = $nameId;
68
        $this->attributes                = $attributes;
69
        $this->authenticatingAuthorities = $authenticatingAuthorities;
70
    }
71
72
    /**
73
     * @return string
74
     */
75
    public function getNameId()
76
    {
77
        return $this->nameId;
78
    }
79
80
    /**
81
     * @return AttributeSet
82
     */
83
    public function getAttributes()
84
    {
85
        return $this->attributes;
86
    }
87
88
    /**
89
     * @return EntityId[]
90
     */
91
    public function getAuthenticatingAuthorities()
92
    {
93
        return $this->authenticatingAuthorities;
94
    }
95
96
    /**
97
     * Using toString in order to comply with AbstractToken's setUser method,
98
     * which uses the string representation to detect changes in the user object.
99
     * Not implementing a UserInterface, because methods defined there will not be used.
100
     *
101
     * @return string
102
     */
103
    public function __toString()
104
    {
105
        return $this->nameId;
106
    }
107
}
108