Completed
Pull Request — develop (#72)
by A.
04:39
created

applyAttributeReleasePolicies()   C

Complexity

Conditions 9
Paths 10

Size

Total Lines 60
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 60
rs 6.8358
cc 9
eloc 35
nc 10
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Copyright 2016 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\EngineBlockApiClientBundle\Service;
20
21
use OpenConext\EngineBlockApiClientBundle\Exception\InvalidResponseException;
22
use OpenConext\EngineBlockApiClientBundle\Http\JsonApiClient;
23
use OpenConext\Profile\Value\Consent;
24
use OpenConext\Profile\Value\ConsentList;
25
use OpenConext\Profile\Value\SpecifiedConsent;
26
use OpenConext\Profile\Value\SpecifiedConsentList;
27
use OpenConext\ProfileBundle\Attribute\AttributeSetWithFallbacks;
28
use stdClass;
29
use Surfnet\SamlBundle\Exception\UnknownUrnException;
30
use Surfnet\SamlBundle\SAML2\Attribute\Attribute;
31
use Surfnet\SamlBundle\SAML2\Attribute\AttributeDefinition;
32
use Surfnet\SamlBundle\SAML2\Attribute\AttributeDictionary;
33
use Surfnet\SamlBundle\SAML2\Attribute\AttributeSetInterface;
34
35
/**
36
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects) Build and mapping logic causes complexity
37
 */
38
final class AttributeReleasePolicyService
39
{
40
    /**
41
     * @var JsonApiClient
42
     */
43
    private $jsonApiClient;
44
45
    /**
46
     * @var AttributeDictionary
47
     */
48
    private $attributeDictionary;
49
50
    public function __construct(JsonApiClient $jsonApiClient, AttributeDictionary $attributeDictionary)
51
    {
52
        $this->jsonApiClient = $jsonApiClient;
53
        $this->attributeDictionary = $attributeDictionary;
54
    }
55
56
    /**
57
     * @param ConsentList $consentList
58
     * @param AttributeSetInterface $attributeSet
59
     * @return SpecifiedConsentList
60
     * @SuppressWarnings(PHPMD.NPathComplexity) Build and mapping logic causes complexity
61
     */
62
    public function applyAttributeReleasePolicies(ConsentList $consentList, AttributeSetInterface $attributeSet)
63
    {
64
        $entityIds = $consentList->map(function (Consent $consent) {
65
            return $consent->getServiceProvider()->getEntity()->getEntityId()->getEntityId();
66
        });
67
68
        $mappedAttributes = [];
69
        foreach ($attributeSet as $attribute) {
70
            $mace = $attribute->getAttributeDefinition()->getUrnMace();
71
            $oid  = $attribute->getAttributeDefinition()->getUrnOid();
72
73
            if ($mace !== null) {
74
                $mappedAttributes[$mace] = $attribute->getValue();
75
            }
76
77
            if ($oid !== null) {
78
                $mappedAttributes[$oid] = $attribute->getValue();
79
            }
80
        }
81
82
        $data = [
83
            'entityIds'  => $entityIds,
84
            'attributes' => !empty($mappedAttributes) ? $mappedAttributes : new stdClass()
85
        ];
86
        $response = $this->jsonApiClient->post($data, '/arp');
87
88
        $specifiedConsents = $consentList->map(
89
            function (Consent $consent) use ($response) {
90
                $entityId = $consent->getServiceProvider()->getEntity()->getEntityId()->getEntityId();
91
92
                if (!isset($response[$entityId])) {
93
                    throw new InvalidResponseException(
94
                        sprintf(
95
                            'EntityID "%s" was not found in the ARP response (entityIDs: %s)',
96
                            $entityId,
97
                            join(', ', array_keys($response))
98
                        )
99
                    );
100
                }
101
102
                $attributes = [];
103
                foreach ($response[$entityId] as $attributeName => $attributeValue) {
104
                    try {
105
                        $attributeDefinition = $this->attributeDictionary->getAttributeDefinitionByUrn($attributeName);
106
                    } catch (UnknownUrnException $exception) {
107
                        $attributeDefinition = new AttributeDefinition($attributeName, $attributeName, $attributeName);
108
                    }
109
110
                    $attribute = new Attribute($attributeDefinition, $attributeValue);
111
                    if (!in_array($attribute, $attributes)) {
112
                        $attributes[] = $attribute;
113
                    }
114
                }
115
116
                return SpecifiedConsent::specifies($consent, AttributeSetWithFallbacks::create($attributes));
117
            }
118
        );
119
120
        return SpecifiedConsentList::createWith($specifiedConsents);
121
    }
122
}
123