Completed
Push — feature/fine-grained-authoriza... ( 9d4b80...f17ed2 )
by Michiel
12s
created

InstitutionAuthorizationContextFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 6
dl 0
loc 46
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A buildFrom() 0 33 5
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\Authorization\Service;
20
21
use Surfnet\Stepup\Identity\Value\Institution;
22
use Surfnet\StepupMiddleware\ApiBundle\Authorization\Value\InstitutionAuthorizationContext;
23
use Surfnet\StepupMiddleware\ApiBundle\Authorization\Value\InstitutionRoleSetInterface;
24
use Surfnet\StepupMiddleware\ApiBundle\Exception\InvalidArgumentException;
25
use Surfnet\StepupMiddleware\ApiBundle\Identity\Service\IdentityService;
26
use Symfony\Component\HttpFoundation\Request;
27
28
/**
29
 * InstitutionAuthorizationContextFactory
30
 *
31
 * Use this factory to build InstitutionAuthorizationContext instances
32
 * from a GET request with authorization context.
33
 */
34
class InstitutionAuthorizationContextFactory implements InstitutionAuthorizationContextFactoryInterface
35
{
36
    /**
37
     * @var IdentityService
38
     */
39
    private $identityService;
40
41
    public function __construct(IdentityService $identityService)
42
    {
43
        $this->identityService = $identityService;
44
    }
45
46
    public function buildFrom(Request $request, InstitutionRoleSetInterface $roleRequirements)
47
    {
48
        if ($request->get('actorId') && $request->get('actorInstitution')) {
49
50
            $actorId = $request->get('actorId');
51
            // Retrieve the identity from the service
52
            $actorIdentity = $this->identityService->find($actorId);
53
54
            // The identity of the actor must exist
55
            if (!$actorIdentity) {
56
                throw new InvalidArgumentException('Identity with id "%s" could not be found.');
57
            }
58
59
            $institution = $request->get('actorInstitution');
60
            $actorInstitution = new Institution($institution);
61
62
            // The identity that was returned from the service should match the institutio that was provided in the
63
            // actorInstitution request parameter.
64
            if (!$actorInstitution->equals($actorIdentity->institution)) {
65
                throw new InvalidArgumentException(
66
                    'The institution of the actor did not match that of the institution found in the actor identity.'
67
                );
68
            }
69
70
            return new InstitutionAuthorizationContext(
71
                $actorIdentity,
0 ignored issues
show
Documentation introduced by
$actorIdentity is of type object<Surfnet\StepupMid...entity\Entity\Identity>, but the function expects a string.

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...
72
                $actorInstitution,
73
                $roleRequirements
74
            );
75
        }
76
77
        return null;
78
    }
79
}
80