Completed
Pull Request — develop (#225)
by
unknown
09:12 queued 07:20
created

AttributeMapper::attributeListToMap()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
/**
3
 * Copyright 2010 SURFnet B.V.
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
18
namespace Surfnet\StepupSelfService\SelfServiceBundle\Service\RemoteVetting;
19
20
use Surfnet\StepupSelfService\SelfServiceBundle\Exception\InvalidRemoteVettingMappingException;
21
use Surfnet\StepupSelfService\SelfServiceBundle\Service\RemoteVetting\Configuration\RemoteVettingConfiguration;
22
use Surfnet\StepupSelfService\SelfServiceBundle\Service\RemoteVetting\Dto\AttributeListDto;
23
use Surfnet\StepupSelfService\SelfServiceBundle\Service\RemoteVetting\Value\AttributeMatch;
24
use Surfnet\StepupSelfService\SelfServiceBundle\Service\RemoteVetting\Value\AttributeMatchCollection;
25
26
class AttributeMapper
27
{
28
    /**
29
     * @var RemoteVettingConfiguration
30
     */
31
    private $configuration;
32
33
    public function __construct(RemoteVettingConfiguration $configuration)
34
    {
35
        $this->configuration = $configuration;
36
    }
37
38
    /**
39
     * @param string $identityProviderName
40
     * @param AttributeListDto $localAttributes
41
     * @param AttributeListDto $remoteAttributes
42
     * @return AttributeMatchCollection
43
     * @throws InvalidRemoteVettingMappingException
44
     */
45
    public function map($identityProviderName, AttributeListDto $localAttributes, AttributeListDto $remoteAttributes)
46
    {
47
        $attributeMapping = $this->configuration->getAttributeMapping($identityProviderName);
48
49
        $localMap = $this->attributeListToMap($localAttributes);
50
        $remoteMap = $this->attributeListToMap($remoteAttributes);
51
52
        $matchCollection = new AttributeMatchCollection();
53
        foreach ($attributeMapping as $localName => $remoteName) {
54
            if (!array_key_exists($localName, $localMap)) {
55
                throw new InvalidRemoteVettingMappingException(sprintf(
56
                    'Invalid remote vetting attribute mapping, local attribute with name "%s" not found',
57
                    $localName
58
                ));
59
            }
60
61
            if (!array_key_exists($remoteName, $remoteMap)) {
62
                throw new InvalidRemoteVettingMappingException(sprintf(
63
                    'Invalid remote vetting attribute mapping, remote attribute with name "%s" not found',
64
                    $remoteName
65
                ));
66
            }
67
68
            $attributeMatch = new AttributeMatch($localMap[$localName], $remoteMap[$remoteName], false, '');
69
            $matchCollection->add($localName, $attributeMatch);
70
        };
71
72
        return $matchCollection;
73
    }
74
75
    /**
76
     * @param AttributeListDto $attributeList
77
     * @return array
78
     */
79
    private function attributeListToMap(AttributeListDto $attributeList)
80
    {
81
        $result = [];
82
        foreach ($attributeList->getAttributeCollection() as $attribute) {
83
            $result[$attribute->getName()] = $attribute;
84
        }
85
        return $result;
86
    }
87
}
88