AttributeAggregationService   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A findByUser() 0 39 4
A disconnectAttributeFor() 0 11 3
A isValidRequest() 0 14 2
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\AttributeAggregationRepositoryInterface;
24
use OpenConext\Profile\Value\AttributeAggregation\AttributeAggregationAttribute;
25
use OpenConext\Profile\Value\AttributeAggregation\AttributeAggregationAttributesList;
26
use OpenConext\Profile\Value\AttributeAggregation\AttributeAggregationEnabledAttributes;
27
use Psr\Log\LoggerInterface;
28
29
final class AttributeAggregationService
30
{
31
    /**
32
     * @var AttributeAggregationRepositoryInterface
33
     */
34
    private $repository;
35
36
    /**
37
     * @var AttributeAggregationEnabledAttributes
38
     */
39
    private $attributeAggregationEnabledAttributes;
40
41
    /**
42
     * @var LoggerInterface
43
     */
44
    private $logger;
45
46
    public function __construct(
47
        AttributeAggregationRepositoryInterface $repository,
48
        AttributeAggregationEnabledAttributes $attributeAggregationEnabledAttributes,
49
        LoggerInterface $logger
50
    ) {
51
        $this->repository = $repository;
52
        $this->attributeAggregationEnabledAttributes = $attributeAggregationEnabledAttributes;
53
        $this->logger = $logger;
54
    }
55
56
    /**
57
     * @param AuthenticatedUser $user
58
     * @return null|AttributeAggregationAttributesList
59
     */
60
    public function findByUser(AuthenticatedUser $user)
61
    {
62
        $enabledAttributes = $this->attributeAggregationEnabledAttributes;
63
64
        try {
65
            $collection = [];
66
67
            $attributeAggregationAttributes = $this->repository->findAllFor($user->getNameId());
68
69
            foreach ($enabledAttributes->getAttributes() as $enabledAttribute) {
70
                $accountType = $enabledAttribute->getAccountType();
71
                if ($attributeAggregationAttributes->hasAttribute($accountType)) {
72
                    $aaAttribute = $attributeAggregationAttributes->getAttribute($accountType);
73
                    $collection[] = AttributeAggregationAttribute::fromConfig(
74
                        $enabledAttribute,
75
                        true,
76
                        $aaAttribute->getId(),
77
                        $aaAttribute->getUserNameId(),
78
                        $aaAttribute->getLinkedId()
79
                    );
80
                } else {
81
                    $collection[] = AttributeAggregationAttribute::fromConfig($enabledAttribute, false, -1, '');
82
                }
83
            }
84
85
            return new AttributeAggregationAttributesList($collection);
86
        } catch (Exception $e) {
87
            $this->logger->error(
88
                sprintf(
89
                    'Error while finding AA attributes. Original error message: "%s"',
90
                    $e->getMessage()
91
                )
92
            );
93
            return null;
94
        }
95
96
        $this->logger->notice('No enabled attribute aggregation attributes found.');
0 ignored issues
show
Unused Code introduced by
$this->logger->notice('N...on attributes found.'); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
97
        return null;
98
    }
99
100
    /**
101
     * @param AuthenticatedUser $user
102
     * @param AttributeAggregationAttribute $orcidAttribute
103
     *
104
     * @return bool returns false when deletion failed
105
     */
106
    public function disconnectAttributeFor(AuthenticatedUser $user, AttributeAggregationAttribute $orcidAttribute)
107
    {
108
        if ($this->isValidRequest($user, $orcidAttribute)) {
109
            $result = $this->repository->unsubscribeAccount($orcidAttribute->getId());
110
            if (!$result) {
111
                $this->logger->error('Error while unsubscribing the AA attribute for the authenticating user.');
112
            }
113
            return $result;
114
        }
115
        return false;
116
    }
117
118
    /**
119
     * Validate the users identity matches that of the identity set on the ORCID attribute retrieved from AA.
120
     *
121
     * @param AttributeAggregationAttribute $orcidAttribute
122
     *
123
     * @return bool
124
     */
125
    private function isValidRequest(AuthenticatedUser $user, AttributeAggregationAttribute $orcidAttribute)
126
    {
127
        $nameId = $user->getNameId();
128
129
        if ($nameId !== $orcidAttribute->getUserNameId()) {
130
            $this->logger->error(
131
                'The users NameId associated with ORCID iD account does not match the NameId of the 
132
                authenticated user.'
133
            );
134
            return false;
135
        }
136
137
        return true;
138
    }
139
}
140