Completed
Pull Request — develop (#80)
by
unknown
01:57
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
                        $aaAttribute = $attributeAggregationAttributes->getAttribute($identifier);
89
                        $collection[] = AttributeAggregationAttribute::fromConfig(
90
                            $enabledAttribute,
91
                            true,
92
                            $aaAttribute->getValues(),
0 ignored issues
show
Documentation introduced by
$aaAttribute->getValues() is of type string, but the function expects a null|array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
93
                            $aaAttribute->getSource()
94
                        );
95
                    } else {
96
                        $collection[] = AttributeAggregationAttribute::fromConfig($enabledAttribute, false);
97
                    }
98
                }
99
100
                return new AttributeAggregationAttributesList($collection);
101
            }
102
        } catch (Exception $e) {
103
            $this->logger->error(
104
                sprintf(
105
                    'Error while finding AA attributes. Original error message: "%s"',
106
                    $e->getMessage()
107
                )
108
            );
109
            return null;
110
        }
111
112
        return null;
113
    }
114
}
115