Completed
Push — feature/upgrade-remote-vetting ( 883904 )
by
unknown
65:35
created

AttributeListDto::notSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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) {
0 ignored issues
show
Bug introduced by
The class SAML2\XML\saml\NameID does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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