Completed
Pull Request — develop (#290)
by
unknown
02:26
created

RegistrationAuthorityCommandHandler::loadInstitutionConfigurationFor()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 17

Duplication

Lines 17
Ratio 100 %

Importance

Changes 0
Metric Value
dl 17
loc 17
rs 9.7
c 0
b 0
f 0
cc 2
nc 3
nop 1
1
<?php
2
3
/**
4
 * Copyright 2014 SURFnet bv
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\CommandHandlingBundle\Identity\CommandHandler;
20
21
use Broadway\CommandHandling\CommandHandler;
22
use Broadway\Repository\RepositoryInterface;
23
use Surfnet\Stepup\Configuration\EventSourcing\InstitutionConfigurationRepository;
24
use Surfnet\Stepup\Identity\Value\ContactInformation;
25
use Surfnet\Stepup\Identity\Value\IdentityId;
26
use Surfnet\Stepup\Identity\Value\Institution;
27
use Surfnet\Stepup\Identity\Value\Location;
28
use Surfnet\Stepup\Identity\Value\RegistrationAuthorityRole;
29
use Surfnet\StepupMiddleware\CommandHandlingBundle\Exception\RuntimeException;
30
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\AccreditIdentityCommand;
31
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\AmendRegistrationAuthorityInformationCommand;
32
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\AppointRoleCommand;
33
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\RetractRegistrationAuthorityCommand;
34
35
/**
36
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
37
 */
38
class RegistrationAuthorityCommandHandler extends CommandHandler
39
{
40
    /**
41
     * @var \Surfnet\Stepup\Identity\EventSourcing\IdentityRepository
42
     */
43
    private $repository;
44
45
    /**
46
     * @param RepositoryInterface $repository
47
     * @param InstitutionConfigurationRepository $institutionConfigurationRepository
48
     */
49
    public function __construct(
50
        RepositoryInterface $repository,
51
        InstitutionConfigurationRepository $institutionConfigurationRepository
0 ignored issues
show
Unused Code introduced by
The parameter $institutionConfigurationRepository is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
52
    ) {
53
        $this->repository = $repository;
0 ignored issues
show
Documentation Bug introduced by
$repository is of type object<Broadway\Repository\RepositoryInterface>, but the property $repository was declared to be of type object<Surfnet\Stepup\Id...ing\IdentityRepository>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
54
    }
55
56
    public function handleAccreditIdentityCommand(AccreditIdentityCommand $command)
57
    {
58
        /** @var \Surfnet\Stepup\Identity\Api\Identity $identity */
59
        $identity = $this->repository->load(new IdentityId($command->identityId));
60
61
        $role = $this->assertValidRoleAndConvertIfValid($command->role, $command->UUID);
62
63
        $identity->accreditWith(
64
            $role,
65
            new Institution($command->raInstitution),
66
            new Location($command->location),
67
            new ContactInformation($command->contactInformation)
68
        );
69
70
        $this->repository->save($identity);
71
    }
72
73
    public function handleAmendRegistrationAuthorityInformationCommand(AmendRegistrationAuthorityInformationCommand $command)
74
    {
75
        /** @var \Surfnet\Stepup\Identity\Api\Identity $identity */
76
        $identity = $this->repository->load(new IdentityId($command->identityId));
77
78
        $identity->amendRegistrationAuthorityInformation(
79
            new Institution($command->raInstitution),
80
            new Location($command->location),
81
            new ContactInformation($command->contactInformation)
82
        );
83
84
        $this->repository->save($identity);
85
    }
86
87
    public function handleAppointRoleCommand(AppointRoleCommand $command)
88
    {
89
        /** @var \Surfnet\Stepup\Identity\Api\Identity $identity */
90
        $identity = $this->repository->load(new IdentityId($command->identityId));
91
92
        $newRole = $this->assertValidRoleAndConvertIfValid($command->role, $command->UUID);
93
94
        $identity->appointAs(new Institution($command->raInstitution), $newRole);
95
96
        $this->repository->save($identity);
97
    }
98
99
    public function handleRetractRegistrationAuthorityCommand(RetractRegistrationAuthorityCommand $command)
100
    {
101
        /** @var \Surfnet\Stepup\Identity\Api\Identity $identity */
102
        $identity = $this->repository->load(new IdentityId($command->identityId));
103
104
        $identity->retractRegistrationAuthority(new Institution($command->raInstitution));
105
106
        $this->repository->save($identity);
107
    }
108
109
    /**
110
     * @param string $role
111
     * @param string $commandId
112
     * @return RegistrationAuthorityRole
113
     */
114
    private function assertValidRoleAndConvertIfValid($role, $commandId)
115
    {
116
        if ($role === 'ra') {
117
            return new RegistrationAuthorityRole(RegistrationAuthorityRole::ROLE_RA);
118
        } elseif ($role === 'raa') {
119
            return new RegistrationAuthorityRole(RegistrationAuthorityRole::ROLE_RAA);
120
        }
121
122
        throw new RuntimeException(sprintf(
123
            'Unknown role "%s" given by AccreditIdentityCommand "%s", must be "ra" or "raa"',
124
            $role,
125
            $commandId
126
        ));
127
    }
128
}
129