Completed
Pull Request — develop (#70)
by A.
02:39
created

applyAttributeReleasePolicies()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 41
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 22
nc 3
nop 2
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\Http\JsonApiClient;
22
use OpenConext\Profile\Value\Consent;
23
use OpenConext\Profile\Value\ConsentList;
24
use OpenConext\Profile\Value\SpecifiedConsent;
25
use OpenConext\Profile\Value\SpecifiedConsentList;
26
use OpenConext\ProfileBundle\Attribute\AttributeSetWithFallbacks;
27
use Surfnet\SamlBundle\SAML2\Attribute\Attribute;
28
use Surfnet\SamlBundle\SAML2\Attribute\AttributeSetInterface;
29
30
final class AttributeReleasePolicyService
31
{
32
    /**
33
     * @var JsonApiClient
34
     */
35
    private $jsonApiClient;
36
37
    public function __construct(JsonApiClient $jsonApiClient)
38
    {
39
        $this->jsonApiClient = $jsonApiClient;
40
    }
41
42
    /**
43
     * @param ConsentList $consentList
44
     * @param AttributeSetInterface $attributeSet
45
     * @return SpecifiedConsentList
46
     */
47
    public function applyAttributeReleasePolicies(ConsentList $consentList, AttributeSetInterface $attributeSet)
48
    {
49
        $entityIds = $consentList->map(function (Consent $consent) {
50
            return $consent->getServiceProvider()->getEntity()->getEntityId()->getEntityId();
51
        });
52
53
        $mappedAttributes = [];
54
        $definitions = [];
55
        foreach ($attributeSet as $attribute) {
56
            $name = $attribute->getAttributeDefinition()->getUrnMace();
57
58
            if ($name === null) {
59
                $name = $attribute->getAttributeDefinition()->getUrnOid();
60
            }
61
62
            $mappedAttributes[$name] = $attribute->getValue();
63
64
            // Remember the attribute definitions so we can more easily build the attributes again
65
            $definitions[$name] = $attribute->getAttributeDefinition();
66
        }
67
68
        $response = $this->jsonApiClient->post('/arp', [
69
            'entityIds'  => $entityIds,
70
            'attributes' => $mappedAttributes
71
        ]);
72
73
        return SpecifiedConsentList::createWith(
74
            $consentList->map(
75
                function (Consent $consent) use ($response, $definitions) {
76
                    $entityId = $consent->getServiceProvider()->getEntity()->getEntityId()->getEntityId();
77
78
                    $attributes = [];
79
                    foreach ($response[$entityId] as $attributeName => $attributeValue) {
80
                        $attributes[] = new Attribute($definitions[$attributeName], $attributeValue);
81
                    }
82
83
                    return SpecifiedConsent::specifies($consent, AttributeSetWithFallbacks::create($attributes));
84
                }
85
            )
86
        );
87
    }
88
}
89