Completed
Pull Request — develop (#225)
by
unknown
05:15 queued 02:48
created

AttributeMapper   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 0
loc 69
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A map() 0 31 4
A attributeListToMap() 0 8 2
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\Exception\InvalidRemoteVettingMappingException;
22
use Surfnet\StepupSelfService\SelfServiceBundle\Service\RemoteVetting\Configuration\RemoteVettingConfiguration;
23
use Surfnet\StepupSelfService\SelfServiceBundle\Service\RemoteVetting\Dto\AttributeListDto;
24
use Surfnet\StepupSelfService\SelfServiceBundle\Service\RemoteVetting\Value\AttributeMatch;
25
use Surfnet\StepupSelfService\SelfServiceBundle\Service\RemoteVetting\Value\AttributeMatchCollection;
26
27
class AttributeMapper
28
{
29
    /**
30
     * @var RemoteVettingConfiguration
31
     */
32
    private $configuration;
33
    /**
34
     * @var LoggerInterface
35
     */
36
    private $logger;
37
38
    public function __construct(RemoteVettingConfiguration $configuration, LoggerInterface  $logger)
39
    {
40
        $this->configuration = $configuration;
41
        $this->logger = $logger;
42
    }
43
44
    /**
45
     * @param string $identityProviderName
46
     * @param AttributeListDto $localAttributes
47
     * @param AttributeListDto $remoteAttributes
48
     * @return AttributeMatchCollection
49
     * @throws InvalidRemoteVettingMappingException
50
     */
51
    public function map($identityProviderName, AttributeListDto $localAttributes, AttributeListDto $remoteAttributes)
52
    {
53
        $attributeMapping = $this->configuration->getAttributeMapping($identityProviderName);
54
55
        $localMap = $this->attributeListToMap($localAttributes);
56
        $remoteMap = $this->attributeListToMap($remoteAttributes);
57
58
        $matchCollection = new AttributeMatchCollection();
59
        foreach ($attributeMapping as $localName => $remoteName) {
60
            if (!array_key_exists($localName, $localMap)) {
61
                $this->logger->warning(sprintf(
62
                    'Invalid remote vetting attribute mapping, local attribute with name "%s" not found, skipping',
63
                    $localName
64
                ));
65
                continue;
66
            }
67
68
            if (!array_key_exists($remoteName, $remoteMap)) {
69
                $this->logger->warning(sprintf(
70
                    'Invalid remote vetting attribute mapping, remote attribute with name "%s" not found, skipping',
71
                    $remoteName
72
                ));
73
                continue;
74
            }
75
76
            $attributeMatch = new AttributeMatch($localMap[$localName], $remoteMap[$remoteName], false, '');
77
            $matchCollection->add($localName, $attributeMatch);
78
        };
79
80
        return $matchCollection;
81
    }
82
83
    /**
84
     * @param AttributeListDto $attributeList
85
     * @return array
86
     */
87
    private function attributeListToMap(AttributeListDto $attributeList)
88
    {
89
        $result = [];
90
        foreach ($attributeList->getAttributeCollection() as $attribute) {
91
            $result[$attribute->getName()] = $attribute;
92
        }
93
        return $result;
94
    }
95
}
96