Completed
Pull Request — develop (#225)
by
unknown
04:30 queued 02:13
created

AttributeMapper::map()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 9.424
c 0
b 0
f 0
cc 4
nc 4
nop 3
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 Psr\Log\LoggerInterface;
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
     * @var LoggerInterface
34
     */
35
    private $logger;
36
37
    public function __construct(RemoteVettingConfiguration $configuration, LoggerInterface  $logger)
38
    {
39
        $this->configuration = $configuration;
40
        $this->logger = $logger;
41
    }
42
43
    /**
44
     * @param string $identityProviderName
45
     * @param AttributeListDto $localAttributes
46
     * @param AttributeListDto $remoteAttributes
47
     * @return AttributeMatchCollection
48
     */
49
    public function map($identityProviderName, AttributeListDto $localAttributes, AttributeListDto $remoteAttributes)
50
    {
51
        $attributeMapping = $this->configuration->getAttributeMapping($identityProviderName);
52
53
        $localMap = $this->attributeListToMap($localAttributes);
54
        $remoteMap = $this->attributeListToMap($remoteAttributes);
55
56
        $matchCollection = new AttributeMatchCollection();
57
        foreach ($attributeMapping as $localName => $remoteName) {
58
            if (!array_key_exists($localName, $localMap)) {
59
                $this->logger->warning(sprintf(
60
                    'Invalid remote vetting attribute mapping, local attribute with name "%s" not found, skipping',
61
                    $localName
62
                ));
63
                continue;
64
            }
65
66
            if (!array_key_exists($remoteName, $remoteMap)) {
67
                $this->logger->warning(sprintf(
68
                    'Invalid remote vetting attribute mapping, remote attribute with name "%s" not found, skipping',
69
                    $remoteName
70
                ));
71
                continue;
72
            }
73
74
            $attributeMatch = new AttributeMatch($localMap[$localName], $remoteMap[$remoteName], false, '');
75
            $matchCollection->add($localName, $attributeMatch);
76
        };
77
78
        return $matchCollection;
79
    }
80
81
    /**
82
     * @param AttributeListDto $attributeList
83
     * @return array
84
     */
85
    private function attributeListToMap(AttributeListDto $attributeList)
86
    {
87
        $result = [];
88
        foreach ($attributeList->getAttributeCollection() as $attribute) {
89
            $result[$attribute->getName()] = $attribute;
90
        }
91
        return $result;
92
    }
93
}
94