Completed
Push — feature/profile-retrieve-impli... ( 3a9ede...231202 )
by
unknown
03:23
created

ProfileService::findAuthorizationsBy()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
/**
4
 * Copyright 2019 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 Surfnet\StepupMiddleware\ApiBundle\Identity\Service;
20
21
use Surfnet\Stepup\Configuration\Value\InstitutionRole;
22
use Surfnet\Stepup\Identity\Value\IdentityId;
23
use Surfnet\StepupMiddleware\ApiBundle\Authorization\Value\InstitutionAuthorizationContextInterface;
24
use Surfnet\StepupMiddleware\ApiBundle\Authorization\Service\InstitutionAuthorizationService;
25
use Surfnet\StepupMiddleware\ApiBundle\Identity\Repository\InstitutionListingRepository;
26
use Surfnet\StepupMiddleware\ApiBundle\Identity\Repository\RaListingRepository;
27
use Surfnet\StepupMiddleware\ApiBundle\Identity\Value\AuthorizedInstitutionCollection;
28
use Surfnet\StepupMiddleware\ApiBundle\Identity\Value\Profile;
29
30
class ProfileService extends AbstractSearchService
31
{
32
    /**
33
     * @var RaListingRepository
34
     */
35
    private $raListingRepository;
36
37
    /**
38
     * @var IdentityService
39
     */
40
    private $identityService;
41
42
    /**
43
     * @var InstitutionListingRepository
44
     */
45
    private $institutionListingRepository;
46
    /**
47
     * @var InstitutionAuthorizationService
48
     */
49
    private $authorizationService;
50
51
    public function __construct(
52
        RaListingRepository $raListingRepository,
53
        InstitutionListingRepository $institutionListingRepository,
54
        IdentityService $identityService,
55
        InstitutionAuthorizationService $institutionAuthorizationService
56
    ) {
57
        $this->raListingRepository = $raListingRepository;
58
        $this->institutionListingRepository = $institutionListingRepository;
59
        $this->identityService = $identityService;
60
        $this->authorizationService = $institutionAuthorizationService;
61
    }
62
63
    /**
64
     * Uses the identityId to first load the ra credentials (if present)
65
     * These credentials are then used to test what type of administrator we are dealing with ((S)RA(A)). Next the
66
     * authorizations are retrieved from the InstitutionAuthorizationRepository. Finally identity is retrieved for the
67
     * provided identityId. This data is then merged in a Profile value object.
68
     *
69
     * When the profile is incorrect, for example because no identity can be found, null is returned instead of a
70
     * Profile. Its possible to retrieve profile data for a non RA user, in that case no authorization data is set
71
     * on the profile. The same goes for the SRAA user. As that user is allowed all authorizations for all institutions.
72
     * An additional isSraa flag is set to true for these administrators.
73
     *
74
     * @param string $identityId
75
     * @return Profile|null
76
     */
77
    public function createProfile($identityId)
78
    {
79
        $identity = $this->identityService->find($identityId);
80
        if ($identity === null) {
81
            return null;
82
        }
83
84
        $authorizationContextRa = $this->authorizationService->buildInstitutionAuthorizationContext(
85
            new IdentityId($identityId),
86
            new InstitutionRole(InstitutionRole::ROLE_USE_RA)
87
        );
88
89
        $authorizationContextRaa = $this->authorizationService->buildInstitutionAuthorizationContext(
90
            new IdentityId($identityId),
91
            new InstitutionRole(InstitutionRole::ROLE_USE_RAA)
92
        );
93
94
        $authorizations = AuthorizedInstitutionCollection::from(
95
            $authorizationContextRa->getInstitutions(),
0 ignored issues
show
Bug introduced by
It seems like $authorizationContextRa->getInstitutions() can be null; however, from() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
96
            $authorizationContextRaa->getInstitutions()
97
        );
98
99
        return new Profile($identity, $authorizations, $authorizationContextRa->isActorSraa());
100
    }
101
102
}
103