Completed
Pull Request — develop (#236)
by
unknown
05:26 queued 02:26
created

InstitutionAuthorizationService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * Copyright 2018 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\Configuration\Service;
20
21
use Surfnet\Stepup\Configuration\Value\Institution;
22
use Surfnet\Stepup\Configuration\Value\InstitutionAuthorizationOption;
23
use Surfnet\Stepup\Configuration\Value\InstitutionAuthorizationOptionMap;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Surfnet\StepupMiddleware...nAuthorizationOptionMap.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
24
use Surfnet\Stepup\Configuration\Value\InstitutionRole;
25
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Repository\InstitutionAuthorizationRepository;
26
27
class InstitutionAuthorizationService
28
{
29
    /**
30
     * @var InstitutionAuthorizationRepository
31
     */
32
    private $repository;
33
34
    /**
35
     * @param InstitutionAuthorizationRepository $repository
36
     */
37
    public function __construct(
38
        InstitutionAuthorizationRepository $repository
39
    ) {
40
        $this->repository = $repository;
41
    }
42
43
    /**
44
     * @param Institution $institution
45
     * @param InstitutionRole $role
46
     * @return InstitutionAuthorizationOption
47
     */
48
    public function findAuthorizationsByRoleFor(Institution $institution, InstitutionRole $role)
49
    {
50
        $authorizations = $this->repository->findAuthorizationOptionsForInstitutionByRole($institution, $role);
51
52
        $institutions = [];
53
        foreach ($authorizations as $authorization) {
54
            $institutions[] = $authorization->institutionRelation;
55
        }
56
57
        return InstitutionAuthorizationOption::fromInstitutions($role, $institution, $institutions);
58
    }
59
60
    /**
61
     * @param Institution $institution
62
     * @return InstitutionAuthorizationOptionMap
63
     */
64
    public function findAuthorizationsFor(Institution $institution)
65
    {
66
        $authorizations = $this->repository->findAuthorizationOptionsForInstitution($institution);
67
68
        return InstitutionAuthorizationOptionMap::fromInstitutionAuthorizations($institution, $authorizations);
69
    }
70
}
71