Completed
Push — develop ( 0782fe...96e20c )
by
unknown
16:08 queued 13:01
created

Arp   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 4
dl 0
loc 116
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A createWith() 0 14 4
B __construct() 0 28 5
B isValidAttribute() 0 16 5
A getNonIdpAttributes() 0 6 1
A getAttributesGroupedBySource() 0 4 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;
20
21
use OpenConext\Profile\Exception\InvalidArpDataException;
22
use Surfnet\SamlBundle\Exception\UnknownUrnException;
23
use Surfnet\SamlBundle\SAML2\Attribute\Attribute;
24
use Surfnet\SamlBundle\SAML2\Attribute\AttributeDefinition;
25
use Surfnet\SamlBundle\SAML2\Attribute\AttributeDictionary;
26
27
/**
28
 * The Arp value object represents the Arp configuration for a given entity.
29
 */
30
final class Arp
31
{
32
    /**
33
     * @var array The arp configuration is grouped on source. The source values are a collection of Attribute
34
     */
35
    private $arp;
36
37
    public static function createWith(array $arp, AttributeDictionary $dictionary = null)
38
    {
39
        // Input validation
40
        foreach ($arp as $attributeInformation) {
41
            if (!is_array($attributeInformation)) {
42
                throw new InvalidArpDataException('The attribute information in the arp should be an array.');
43
            }
44
            if (!self::isValidAttribute($attributeInformation)) {
45
                throw new InvalidArpDataException('The attribute information is formatted invalidly.');
46
            }
47
        }
48
49
        return new self($arp, $dictionary);
50
    }
51
52
    private function __construct(array $arp, AttributeDictionary $dictionary = null)
53
    {
54
        $arpCollection = [];
55
56
        // Create Attribute instances for all attributes
57
        foreach ($arp as $attributeName => $attributeDefinitionInformation) {
58
            $attributeSource = 'idp';
59
            $attributeDefinition = new AttributeDefinition($attributeName, $attributeName, $attributeName);
60
61
            // When the dictionary is available. Lookup the attribute in dictionary to load friendly attribute names
62
            if (!is_null($dictionary)) {
63
                try {
64
                    $attributeDefinition = $dictionary->getAttributeDefinitionByUrn($attributeName);
65
                } catch (UnknownUrnException $exception) {
66
                    // Use the previously created attributeDefinition.
67
                }
68
            }
69
70
            if (isset($attributeDefinitionInformation[0]['source'])) {
71
                $attributeSource = $attributeDefinitionInformation[0]['source'];
72
            }
73
74
            // The arp is grouped on attribute source
75
            $arpCollection[$attributeSource][] = new Attribute($attributeDefinition, $attributeDefinitionInformation);
76
        }
77
78
        $this->arp = $arpCollection;
79
    }
80
81
    /**
82
     * Tests the structure of the Arp attribute information.
83
     *
84
     * This information should be an array, should have the attribute names as its keys and have array values.
85
     * These array values can have two keys (value & source) the values aof these entries should be string.
86
     *
87
     * Example of a valid attribute information array:
88
     *
89
     * [
90
     *   'urn.mace.email' => [
91
     *     ['value' => '*'],
92
     *   ],
93
     *   'urn.mace.eduPersonTargetedId' => [
94
     *     [
95
     *       'value' => '*',
96
     *       'source' => 'sab',
97
     *     ]
98
     *   ],
99
     *   'urn.mace.orcid' => [
100
     *     [
101
     *       'value' => 'orcid.org/\d{4}-\d{4}',
102
     *       'source' => 'orcid',
103
     *     ],
104
     *     [
105
     *       'value' => 'sandbox.orcid.org/\d{4}-\d{4}',
106
     *       'source' => 'orcid',
107
     *     ],
108
     *   ],
109
     * ]
110
     *
111
     * @param array $attributeInformation
112
     * @return bool
113
     */
114
    private static function isValidAttribute(array $attributeInformation)
115
    {
116
        $validKeys = ['value', 'source'];
117
118
        foreach ($attributeInformation as $attributeInformationEntry) {
119
            foreach ($attributeInformationEntry as $key => $attributeConfigValue) {
120
                if (!in_array($key, $validKeys)) {
121
                    return false;
122
                }
123
                if (!is_string($attributeConfigValue)) {
124
                    return false;
125
                }
126
            }
127
        }
128
        return true;
129
    }
130
131
    /**
132
     * @return array
133
     */
134
    public function getNonIdpAttributes()
135
    {
136
        $attributes = $this->getAttributesGroupedBySource();
137
        unset($attributes['idp']);
138
        return $attributes;
139
    }
140
141
    public function getAttributesGroupedBySource()
142
    {
143
        return $this->arp;
144
    }
145
}
146