Completed
Pull Request — develop (#90)
by
unknown
02:04
created

fromApiResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
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 OpenConext\AttributeAggregationApiClientBundle\Exception\InvalidArgumentException;
22
23
/**
24
 * AttributeAggregationAttributesList as returned from the AA API
25
 */
26
final class AttributeAggregationAttributesList
27
{
28
    /**
29
     * @var AttributeAggregationAttribute[]
30
     */
31
    private $attributes;
32
33
    /**
34
     * @param AttributeAggregationAttribute[] $attributes
35
     */
36
    public function __construct(array $attributes)
37
    {
38
        $this->attributes = $attributes;
39
    }
40
41
    /**
42
     * @param array $attributes
43
     * @return AttributeAggregationAttributesList
44
     */
45
    public static function fromApiResponse(array $attributes)
46
    {
47
        $attributeCollection = [];
48
        foreach ($attributes as $attributeData) {
49
            $attributeCollection[] = AttributeAggregationAttribute::fromApiResponse($attributeData);
50
        }
51
        return new self($attributeCollection);
52
    }
53
54
    public function getActiveAttributes()
55
    {
56
        $output = [];
57
        foreach ($this->attributes as $attribute) {
58
            if ($attribute->isConnected()) {
59
                $output[] = $attribute;
60
            }
61
        }
62
        return $output;
63
    }
64
65
    public function getAvailableAttributes()
66
    {
67
        $output = [];
68
        foreach ($this->attributes as $attribute) {
69
            if (!$attribute->isConnected()) {
70
                $output[] = $attribute;
71
            }
72
        }
73
        return $output;
74
    }
75
76
    /**
77
     * @param string $accountType
78
     * @return bool
79
     */
80
    public function hasAttribute($accountType)
81
    {
82
        foreach ($this->attributes as $attribute) {
83
            if ($attribute->getAccountType() === $accountType) {
84
                return true;
85
            }
86
        }
87
        return false;
88
    }
89
90
    public function filterEnabledAttributes(AttributeAggregationEnabledAttributes $enabledAttributes)
91
    {
92
        $this->attributes = array_filter(
93
            $this->attributes,
94
            function (AttributeAggregationAttribute $attribute) use ($enabledAttributes) {
95
                return $enabledAttributes->isEnabled($attribute->getAccountType());
96
            }
97
        );
98
    }
99
100
    /**
101
     * @param string $accountType
102
     * @return AttributeAggregationAttribute
103
     * @throws InvalidArgumentException
104
     */
105
    public function getAttribute($accountType)
106
    {
107
        foreach ($this->attributes as $attribute) {
108
            if ($attribute->getAccountType() === $accountType) {
109
                return $attribute;
110
            }
111
        }
112
113
        throw new InvalidArgumentException(
114
            sprintf(
115
                'The requested attribute for account type "%s" could not be found',
116
                $accountType
117
            )
118
        );
119
    }
120
}
121