Completed
Pull Request — develop (#225)
by
unknown
04:30 queued 02:13
created

AttributeListDto::fromAttributeSet()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
cc 4
nc 4
nop 1
1
<?php
2
3
/**
4
 * Copyright 2020 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 Surfnet\StepupSelfService\SelfServiceBundle\Service\RemoteVetting\Dto;
20
21
use SAML2\XML\saml\NameID;
22
use Serializable;
23
use Surfnet\SamlBundle\SAML2\Attribute\Attribute as SAMLAttribute;
24
use Surfnet\SamlBundle\SAML2\Attribute\AttributeSet;
25
use Surfnet\StepupSelfService\SelfServiceBundle\Assert;
26
use Surfnet\StepupSelfService\SelfServiceBundle\Service\RemoteVetting\Value\Attribute;
27
use Surfnet\StepupSelfService\SelfServiceBundle\Service\RemoteVetting\Value\AttributeCollection;
28
use Surfnet\StepupSelfService\SelfServiceBundle\Service\RemoteVetting\Value\AttributeCollectionInterface;
29
30
/**
31
 * The identity is a set of SAML Response assertion attributes
32
 *
33
 * Which of the attributes are considered identity data is to be decided by the
34
 * user of this DTO.
35
 */
36
class AttributeListDto implements Serializable, AttributeCollectionInterface
37
{
38
    /**
39
     * @var AttributeCollection
40
     */
41
    private $attributes;
42
43
    /**
44
     * @var string
45
     */
46
    private $nameId;
47
48
    public function __construct(array $attributes, $nameId)
49
    {
50
        Assert::string($nameId, 'The $nameId in an AttributeListDto must be a string value');
51
52
        $this->attributes = new AttributeCollection($attributes);
53
        $this->nameId = $nameId;
54
    }
55
56
    /**
57
     * @param AttributeSet $attributeSet
58
     * @return AttributeListDto
59
     */
60
    public static function fromAttributeSet(AttributeSet $attributeSet)
61
    {
62
        $attributes = [];
63
        $nameID = '';
64
        /** @var SAMLAttribute $attribute */
65
        foreach ($attributeSet as $attribute) {
66
            $name = $attribute->getAttributeDefinition()->getName();
67
            $values = $attribute->getValue();
68
            foreach ($values as $value) {
69
                if ($value instanceof NameID) {
70
                    $nameID = (string)$value->value;
71
                    continue;
72
                }
73
74
                $attributes[$name] = $values;
75
            }
76
        }
77
78
        return new self($attributes, $nameID);
79
    }
80
81
    /**
82
     * @param string $serialized
83
     * @return AttributeListDto
84
     */
85
    public static function deserialize($serialized)
86
    {
87
        $instance = new self([], '');
88
        $instance->unserialize($serialized);
89
        return $instance;
90
    }
91
92
    /**
93
     * @return AttributeListDto
94
     */
95
    public static function notSet()
96
    {
97
        return new self([], '');
98
    }
99
100
    /**
101
     * @return AttributeCollection|Attribute[]
102
     */
103
    public function getAttributeCollection()
104
    {
105
        return $this->attributes;
106
    }
107
108
    /**
109
     * @inheritDoc
110
     */
111
    public function serialize()
112
    {
113
        return json_encode($this->getAttributes());
114
    }
115
116
    /**
117
     * @inheritDoc
118
     */
119
    public function unserialize($serialized)
120
    {
121
        $data = json_decode($serialized, true);
122
123
        $this->nameId = $data['nameId'];
124
        $this->attributes = new AttributeCollection($data['attributes']);
125
    }
126
127
    public function getAttributes()
128
    {
129
        $attributes = [];
130
        foreach ($this->attributes as $item) {
131
            $attributes[$item->getName()] = $item->getValue();
132
        }
133
134
        return [
135
            'nameId' => $this->nameId,
136
            'attributes' => $attributes,
137
        ];
138
    }
139
140
    /**
141
     * @return string
142
     */
143
    public function getNameId()
144
    {
145
        return $this->nameId;
146
    }
147
}
148