ARP   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 49
dl 0
loc 143
rs 10
c 0
b 0
f 0
wmc 15

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getEntryXMLcontent() 0 12 3
A __construct() 0 8 2
A surround() 0 11 3
A getXML() 0 18 2
A getEntryXML() 0 6 1
A getAttributeID() 0 9 3
A loadAttributeMap() 0 10 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Module\metarefresh;
6
7
/*
8
 * @package SimpleSAMLphp
9
 */
10
class ARP
11
{
12
    /** @var array */
13
    private array $metadata;
14
15
    /** @var array */
16
    private array $attributes = [];
17
18
    /** @var string */
19
    private string $prefix;
20
21
    /** @var string */
22
    private string $suffix;
23
24
25
    /**
26
     * Constructor
27
     *
28
     * @param array $metadata
29
     * @param string $attributemap_filename
30
     * @param string $prefix
31
     * @param string $suffix
32
     */
33
    public function __construct(array $metadata, string $attributemap_filename, string $prefix, string $suffix)
34
    {
35
        $this->metadata = $metadata;
36
        $this->prefix = $prefix;
37
        $this->suffix = $suffix;
38
39
        if (isset($attributemap_filename)) {
40
            $this->loadAttributeMap($attributemap_filename);
41
        }
42
    }
43
44
45
    /**
46
     * @param string $attributemap_filename
47
     *
48
     */
49
    private function loadAttributeMap(string $attributemap_filename): void
50
    {
51
        $config = \SimpleSAML\Configuration::getInstance();
52
53
        /** @psalm-suppress PossiblyNullOperand */
54
        include($config->getPathValue('attributemap', 'attributemap/') . $attributemap_filename . '.php');
55
56
        // Note that $attributemap is defined in the included attributemap-file!
57
        /** @psalm-var array $attributemap */
58
        $this->attributes = $attributemap;
59
    }
60
61
62
    /**
63
     * @param string $name
64
     *
65
     * @return string
66
     */
67
    private function surround(string $name): string
68
    {
69
        $ret = '';
70
        if (!empty($this->prefix)) {
71
            $ret .= $this->prefix;
72
        }
73
        $ret .= $name;
74
        if (!empty($this->suffix)) {
75
            $ret .= $this->suffix;
76
        }
77
        return $ret;
78
    }
79
80
81
    /**
82
     * @param string $name
83
     *
84
     * @return string
85
     */
86
    private function getAttributeID(string $name): string
87
    {
88
        if (empty($this->attributes)) {
89
            return $this->surround($name);
90
        }
91
        if (array_key_exists($name, $this->attributes)) {
92
            return $this->surround($this->attributes[$name]);
93
        }
94
        return $this->surround($name);
95
    }
96
97
98
    /**
99
     * @return string
100
     */
101
    public function getXML(): string
102
    {
103
        $xml = <<<MSG
104
        <?xml version="1.0" encoding="UTF-8"?>
105
        <AttributeFilterPolicyGroup id="urn:mace:funet.fi:haka:kalmar" xmlns="urn:mace:shibboleth:2.0:afp"
106
    xmlns:basic="urn:mace:shibboleth:2.0:afp:mf:basic" xmlns:saml="urn:mace:shibboleth:2.0:afp:mf:saml"
107
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
108
    xsi:schemaLocation="urn:mace:shibboleth:2.0:afp classpath:/schema/shibboleth-2.0-afp.xsd
109
                        urn:mace:shibboleth:2.0:afp:mf:basic classpath:/schema/shibboleth-2.0-afp-mf-basic.xsd
110
                        urn:mace:shibboleth:2.0:afp:mf:saml classpath:/schema/shibboleth-2.0-afp-mf-saml.xsd">
111
MSG;
112
113
        foreach ($this->metadata as $metadata) {
114
            $xml .= $this->getEntryXML($metadata['metadata']);
115
        }
116
117
        $xml .= '</AttributeFilterPolicyGroup>';
118
        return $xml;
119
    }
120
121
122
    /**
123
     * @param array $entry
124
     *
125
     * @return string
126
     */
127
    private function getEntryXML(array $entry): string
128
    {
129
        $entityid = $entry['entityid'];
130
        return '    <AttributeFilterPolicy id="' . $entityid .
131
            '"><PolicyRequirementRule xsi:type="basic:AttributeRequesterString" value="' . $entityid .
132
            '" />' . $this->getEntryXMLcontent($entry) . '</AttributeFilterPolicy>';
133
    }
134
135
136
    /**
137
     * @param array $entry
138
     *
139
     * @return string
140
     */
141
    private function getEntryXMLcontent(array $entry): string
142
    {
143
        if (!array_key_exists('attributes', $entry)) {
144
            return '';
145
        }
146
147
        $ret = '';
148
        foreach ($entry['attributes'] as $a) {
149
            $ret .= '            <AttributeRule attributeID="' . $this->getAttributeID($a) .
150
                '"><PermitValueRule xsi:type="basic:ANY" /></AttributeRule>';
151
        }
152
        return $ret;
153
    }
154
}
155