Completed
Pull Request — develop (#70)
by A.
03:34
created

applyAttributeReleasePolicies()   B

Complexity

Conditions 8
Paths 10

Size

Total Lines 56
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 56
rs 7.3333
cc 8
eloc 32
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 Assert\Assertion;
22
use OpenConext\EngineBlockApiClientBundle\Exception\InvalidResponseException;
23
use OpenConext\EngineBlockApiClientBundle\Http\JsonApiClient;
24
use OpenConext\Profile\Value\Consent;
25
use OpenConext\Profile\Value\ConsentList;
26
use OpenConext\Profile\Value\SpecifiedConsent;
27
use OpenConext\Profile\Value\SpecifiedConsentList;
28
use OpenConext\ProfileBundle\Attribute\AttributeSetWithFallbacks;
29
use stdClass;
30
use Surfnet\SamlBundle\SAML2\Attribute\Attribute;
31
use Surfnet\SamlBundle\SAML2\Attribute\AttributeDictionary;
32
use Surfnet\SamlBundle\SAML2\Attribute\AttributeSetInterface;
33
34
final class AttributeReleasePolicyService
35
{
36
    /**
37
     * @var JsonApiClient
38
     */
39
    private $jsonApiClient;
40
41
    /**
42
     * @var AttributeDictionary
43
     */
44
    private $attributeDictionary;
45
46
    public function __construct(JsonApiClient $jsonApiClient, AttributeDictionary $attributeDictionary)
47
    {
48
        $this->jsonApiClient = $jsonApiClient;
49
        $this->attributeDictionary = $attributeDictionary;
50
    }
51
52
    /**
53
     * @param ConsentList $consentList
54
     * @param AttributeSetInterface $attributeSet
55
     * @return SpecifiedConsentList
56
     */
57
    public function applyAttributeReleasePolicies(ConsentList $consentList, AttributeSetInterface $attributeSet)
58
    {
59
        $entityIds = $consentList->map(function (Consent $consent) {
60
            return $consent->getServiceProvider()->getEntity()->getEntityId()->getEntityId();
61
        });
62
63
        $mappedAttributes = [];
64
        foreach ($attributeSet as $attribute) {
65
            $mace = $attribute->getAttributeDefinition()->getUrnMace();
66
            $oid  = $attribute->getAttributeDefinition()->getUrnOid();
67
68
            if ($mace !== null) {
69
                $mappedAttributes[$mace] = $attribute->getValue();
70
            }
71
72
            if ($oid !== null) {
73
                $mappedAttributes[$oid] = $attribute->getValue();
74
            }
75
        }
76
77
        $data = [
78
            'entityIds'  => $entityIds,
79
            'attributes' => !empty($mappedAttributes) ? $mappedAttributes : new stdClass()
80
        ];
81
        $response = $this->jsonApiClient->post($data, '/arp');
82
83
        $specifiedConsents = $consentList->map(
84
            function (Consent $consent) use ($response) {
85
                $entityId = $consent->getServiceProvider()->getEntity()->getEntityId()->getEntityId();
86
87
                if (!isset($response[$entityId])) {
88
                    throw new InvalidResponseException(
89
                        sprintf(
90
                            'EntityID "%s" was not found in the ARP response (entityIDs: %s)',
91
                            $entityId,
92
                            join(', ', array_keys($response))
93
                        )
94
                    );
95
                }
96
97
                $attributes = [];
98
                foreach ($response[$entityId] as $attributeName => $attributeValue) {
99
                    $attributeDefinition = $this->attributeDictionary->findAttributeDefinitionByUrn($attributeName);
100
101
                    $attribute = new Attribute($attributeDefinition, $attributeValue);
0 ignored issues
show
Bug introduced by
It seems like $attributeDefinition defined by $this->attributeDictiona...onByUrn($attributeName) on line 99 can be null; however, Surfnet\SamlBundle\SAML2...ttribute::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
102
                    if (!in_array($attribute, $attributes)) {
103
                        $attributes[] = $attribute;
104
                    }
105
                }
106
107
                return SpecifiedConsent::specifies($consent, AttributeSetWithFallbacks::create($attributes));
108
            }
109
        );
110
111
        return SpecifiedConsentList::createWith($specifiedConsents);
112
    }
113
}
114