Completed
Pull Request — develop (#80)
by
unknown
03:24 queued 01:23
created

AttributeAggregationAttribute::fromApiResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 16
nc 1
nop 1
1
<?php
2
3
/**
4
 * Copyright 2017 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\Value\AttributeAggregation;
20
21
use Assert\Assertion;
22
23
final class AttributeAggregationAttribute
24
{
25
    /**
26
     * @var string
27
     */
28
    private $identifier;
29
30
    /**
31
     * @var string
32
     */
33
    private $values;
34
35
    /**
36
     * @var string
37
     */
38
    private $source;
39
40
    /**
41
     * @var string
42
     */
43
    private $logoPath;
44
45
    /**
46
     * @var string
47
     */
48
    private $connectUrl;
49
50
    /**
51
     * @var string
52
     */
53
    private $disconnectUrl;
54
55
    /**
56
     * @var bool
57
     */
58
    private $isConnected = false;
59
60
    /**
61
     * @param string $identifier
62
     * @param string $logoPath
63
     * @param string $connectUrl
64
     * @param string $disconnectUrl
65
     * @param bool $isConnected
66
     * @param array $values
0 ignored issues
show
Documentation introduced by
Should the type for parameter $values not be null|array?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
67
     * @param $source
68
     */
69
    public function __construct(
70
        $identifier,
71
        $logoPath,
72
        $connectUrl,
73
        $disconnectUrl,
74
        $isConnected,
75
        array $values = null,
76
        $source = null
77
    ) {
78
        $this->identifier = $identifier;
79
        $this->logoPath = $logoPath;
80
        $this->connectUrl = $connectUrl;
81
        $this->disconnectUrl = $disconnectUrl;
82
        $this->isConnected = $isConnected;
83
        $this->values = $values;
0 ignored issues
show
Documentation Bug introduced by
It seems like $values can also be of type array. However, the property $values is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
84
        $this->source = $source;
85
    }
86
87
    public static function fromConfig(
88
        AttributeAggregationAttributeConfiguration $enabledAttribute,
89
        $isConnected,
90
        array $values = null,
91
        $source = null
92
    ) {
93
        return new self(
94
            $enabledAttribute->getIdentifier(),
95
            $enabledAttribute->getLogoPath(),
96
            $enabledAttribute->getConnectUrl(),
97
            $enabledAttribute->getDisconnectUrl(),
98
            $isConnected,
99
            $values,
100
            $source
101
        );
102
    }
103
104
    public static function fromApiResponse(array $attributeData)
105
    {
106
        Assertion::keyExists($attributeData, 'name', 'The name should be set on the attribute');
107
        Assertion::string($attributeData['name'], 'The name should be a string');
108
        Assertion::keyExists($attributeData, 'values', 'The values should be set on the attribute');
109
        Assertion::isArray($attributeData['values'], 'The values should be an array');
110
        Assertion::keyExists($attributeData, 'source', 'The source should be set on the attribute');
111
        Assertion::string($attributeData['source'], 'The source should be a string');
112
113
        $attribute = new self(
114
            $attributeData['name'],
115
            '',
116
            '',
117
            '',
118
            true,
119
            $attributeData['values'],
120
            $attributeData['source']
121
        );
122
        return $attribute;
123
    }
124
125
    /**
126
     * @return string
127
     */
128
    public function getIdentifier()
129
    {
130
        return $this->identifier;
131
    }
132
133
    /**
134
     * @return string
135
     */
136
    public function getLogoPath()
137
    {
138
        return $this->logoPath;
139
    }
140
141
    /**
142
     * @return string
143
     */
144
    public function getConnectUrl()
145
    {
146
        return $this->connectUrl;
147
    }
148
149
    /**
150
     * @return string
151
     */
152
    public function getDisconnectUrl()
153
    {
154
        return $this->disconnectUrl;
155
    }
156
157
    /**
158
     * @return bool
159
     */
160
    public function isConnected()
161
    {
162
        return $this->isConnected;
163
    }
164
165
    /**
166
     * @return string
167
     */
168
    public function getValues()
169
    {
170
        return $this->values;
171
    }
172
173
    /**
174
     * @return string
175
     */
176
    public function getSource()
177
    {
178
        return $this->source;
179
    }
180
}
181