Completed
Pull Request — develop (#82)
by
unknown
01:47
created

AttributeAggregationService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 4
1
<?php
2
3
/**
4
 * Copyright 2017 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\ProfileBundle\Service;
20
21
use Exception;
22
use OpenConext\Profile\Entity\AuthenticatedUser;
23
use OpenConext\Profile\Repository\AttributeAggregationRepository;
24
use OpenConext\Profile\Value\AttributeAggregation\AttributeAggregationAttribute;
25
use OpenConext\Profile\Value\AttributeAggregation\AttributeAggregationAttributesList;
26
use OpenConext\Profile\Value\AttributeAggregation\AttributeAggregationEnabledAttributes;
27
use OpenConext\Profile\Value\SurfConextId;
28
use Psr\Log\LoggerInterface;
29
use Surfnet\SamlBundle\SAML2\Attribute\AttributeDefinition;
30
31
final class AttributeAggregationService
32
{
33
    /**
34
     * @var AttributeDefinition
35
     */
36
    private $surfConextUserIdAttributeDefinition;
37
38
    /**
39
     * @var AttributeAggregationRepository
40
     */
41
    private $repository;
42
43
    /**
44
     * @var AttributeAggregationEnabledAttributes
45
     */
46
    private $attributeAggregationEnabledAttributes;
47
48
    /**
49
     * @var LoggerInterface
50
     */
51
    private $logger;
52
53
    public function __construct(
54
        AttributeAggregationRepository $repository,
55
        AttributeDefinition $surfConextUserIdAttributeDefinition,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $surfConextUserIdAttributeDefinition exceeds the maximum configured length of 30.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
56
        AttributeAggregationEnabledAttributes $attributeAggregationEnabledAttributes,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $attributeAggregationEnabledAttributes exceeds the maximum configured length of 30.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
57
        LoggerInterface $logger
58
    ) {
59
        $this->repository = $repository;
60
        $this->surfConextUserIdAttributeDefinition = $surfConextUserIdAttributeDefinition;
61
        $this->attributeAggregationEnabledAttributes = $attributeAggregationEnabledAttributes;
62
        $this->logger = $logger;
63
    }
64
65
    /**
66
     * @param AuthenticatedUser $user
67
     * @return null|AttributeAggregationAttributesList
68
     */
69
    public function findByUser(AuthenticatedUser $user)
70
    {
71
        $enabledAttributes = $this->attributeAggregationEnabledAttributes;
72
73
        try {
74
            $definition = $this->surfConextUserIdAttributeDefinition;
75
            $userAttributes = $user->getAttributes();
76
            // Does the logged in user have the SurfConextUserId attribute?
77
            if ($userAttributes->containsAttributeDefinedBy($definition)) {
78
                $collection = [];
79
80
                $samlAttribute = $userAttributes->getAttributeByDefinition($definition);
81
                $surfConextIdValue = $samlAttribute->getValue()[0];
82
                $surfConextId = new SurfConextId($surfConextIdValue);
83
                $attributeAggregationAttributes = $this->repository->findAllFor($surfConextId);
84
85
                foreach ($enabledAttributes->getAttributes() as $enabledAttribute) {
86
                    $identifier = $enabledAttribute->getIdentifier();
87
                    if ($attributeAggregationAttributes->hasAttribute($identifier)) {
88
                        $collection[] = AttributeAggregationAttribute::fromConfig($enabledAttribute, true);
89
                    } else {
90
                        $collection[] = AttributeAggregationAttribute::fromConfig($enabledAttribute, false);
91
                    }
92
                }
93
94
                return new AttributeAggregationAttributesList($collection);
95
            }
96
        } catch (Exception $e) {
97
            $this->logger->error(
98
                sprintf(
99
                    'Error while finding AA attributes. Original error message: "%s"',
100
                    $e->getMessage()
101
                )
102
            );
103
            return null;
104
        }
105
106
        return null;
107
    }
108
}
109