Completed
Pull Request — feature/fine-grained-authoriza... (#246)
by
unknown
56:03 queued 45:22
created

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\AggregateNotFoundException;
23
use Broadway\Repository\RepositoryInterface;
24
use Surfnet\Stepup\Configuration\EventSourcing\InstitutionConfigurationRepository;
25
use Surfnet\Stepup\Configuration\InstitutionConfiguration;
26
use Surfnet\Stepup\Configuration\Value\InstitutionConfigurationId;
27
use Surfnet\Stepup\Configuration\Value\Institution as ConfigurationInstitution;
28
use Surfnet\Stepup\Identity\Value\ContactInformation;
29
use Surfnet\Stepup\Identity\Value\IdentityId;
30
use Surfnet\Stepup\Identity\Value\Institution;
31
use Surfnet\Stepup\Identity\Value\Location;
32
use Surfnet\Stepup\Identity\Value\RegistrationAuthorityRole;
33
use Surfnet\StepupMiddleware\CommandHandlingBundle\Exception\RuntimeException;
34
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\AccreditIdentityCommand;
35
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\AmendRegistrationAuthorityInformationCommand;
36
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\AppointRoleCommand;
37
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\RetractRegistrationAuthorityCommand;
38
39
/**
40
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
41
 */
42
class RegistrationAuthorityCommandHandler extends CommandHandler
43
{
44
    /**
45
     * @var \Surfnet\Stepup\Identity\EventSourcing\IdentityRepository
46
     */
47
    private $repository;
48
    /**
49
     * @var InstitutionConfigurationRepository
50
     */
51
    private $institutionConfigurationRepository;
52
53
    /**
54
     * @param RepositoryInterface $repository
55
     * @param InstitutionConfigurationRepository $institutionConfigurationRepository
56
     */
57
    public function __construct(
58
        RepositoryInterface $repository,
59
        InstitutionConfigurationRepository $institutionConfigurationRepository
60
    ) {
61
        $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...
62
        $this->institutionConfigurationRepository = $institutionConfigurationRepository;
63
    }
64
65
    public function handleAccreditIdentityCommand(AccreditIdentityCommand $command)
66
    {
67
        /** @var \Surfnet\Stepup\Identity\Api\Identity $identity */
68
        $identity = $this->repository->load(new IdentityId($command->identityId));
69
70
        $institutionConfiguration = $this->loadInstitutionConfigurationFor(new Institution($command->institution));
0 ignored issues
show
Deprecated Code introduced by
The method Surfnet\StepupMiddleware...utionConfigurationFor() has been deprecated with message: Should be used until existing institution configurations have been migrated to using normalized ids

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
71
72
        $role = $this->assertValidRoleAndConvertIfValid($command->role, $command->UUID);
73
74
        $identity->accreditWith(
75
            $role,
76
            new Institution($command->raInstitution),
77
            new Location($command->location),
78
            new ContactInformation($command->contactInformation),
79
            $institutionConfiguration
80
        );
81
82
        $this->repository->save($identity);
83
    }
84
85
    public function handleAmendRegistrationAuthorityInformationCommand(AmendRegistrationAuthorityInformationCommand $command)
86
    {
87
        /** @var \Surfnet\Stepup\Identity\Api\Identity $identity */
88
        $identity = $this->repository->load(new IdentityId($command->identityId));
89
90
        $identity->amendRegistrationAuthorityInformation(
91
            new Institution($command->raInstitution),
92
            new Location($command->location),
93
            new ContactInformation($command->contactInformation)
94
        );
95
96
        $this->repository->save($identity);
97
    }
98
99
    public function handleAppointRoleCommand(AppointRoleCommand $command)
100
    {
101
        /** @var \Surfnet\Stepup\Identity\Api\Identity $identity */
102
        $identity = $this->repository->load(new IdentityId($command->identityId));
103
104
        $institutionConfiguration = $this->loadInstitutionConfigurationFor(new Institution($identity->getInstitution()->getInstitution()));
0 ignored issues
show
Deprecated Code introduced by
The method Surfnet\StepupMiddleware...utionConfigurationFor() has been deprecated with message: Should be used until existing institution configurations have been migrated to using normalized ids

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
105
106
        $newRole = $this->assertValidRoleAndConvertIfValid($command->role, $command->UUID);
107
108
        $identity->appointAs(new Institution($command->raInstitution), $newRole, $institutionConfiguration);
109
110
        $this->repository->save($identity);
111
    }
112
113
    public function handleRetractRegistrationAuthorityCommand(RetractRegistrationAuthorityCommand $command)
114
    {
115
        /** @var \Surfnet\Stepup\Identity\Api\Identity $identity */
116
        $identity = $this->repository->load(new IdentityId($command->identityId));
117
118
        $identity->retractRegistrationAuthority(new Institution($command->raInstitution));
119
120
        $this->repository->save($identity);
121
    }
122
123
    /**
124
     * @param string $role
125
     * @param string $commandId
126
     * @return RegistrationAuthorityRole
127
     */
128
    private function assertValidRoleAndConvertIfValid($role, $commandId)
129
    {
130
        if ($role === 'ra') {
131
            return new RegistrationAuthorityRole(RegistrationAuthorityRole::ROLE_RA);
132
        } elseif ($role === 'raa') {
133
            return new RegistrationAuthorityRole(RegistrationAuthorityRole::ROLE_RAA);
134
        }
135
136
        throw new RuntimeException(sprintf(
137
            'Unknown role "%s" given by AccreditIdentityCommand "%s", must be "ra" or "raa"',
138
            $role,
139
            $commandId
140
        ));
141
    }
142
143
    /**
144
     * @deprecated Should be used until existing institution configurations have been migrated to using normalized ids
145
     *
146
     * @param Institution $institution
147
     * @return InstitutionConfiguration
148
     */
149 View Code Duplication
    private function loadInstitutionConfigurationFor(Institution $institution)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
150
    {
151
        $institution = new ConfigurationInstitution($institution->getInstitution());
152
        try {
153
            $institutionConfigurationId = InstitutionConfigurationId::normalizedFrom($institution);
154
            $institutionConfiguration = $this->institutionConfigurationRepository->load(
155
                $institutionConfigurationId->getInstitutionConfigurationId()
156
            );
157
        } catch (AggregateNotFoundException $exception) {
158
            $institutionConfigurationId = InstitutionConfigurationId::from($institution);
0 ignored issues
show
Deprecated Code introduced by
The method Surfnet\Stepup\Configura...ConfigurationId::from() has been deprecated with message: To be removed in next release; use normalizedFrom method to account for case-(in)sensitivity issues

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
159
            $institutionConfiguration = $this->institutionConfigurationRepository->load(
160
                $institutionConfigurationId->getInstitutionConfigurationId()
161
            );
162
        }
163
164
        return $institutionConfiguration;
165
    }
166
}
167