RegistrationAuthorityCommandHandler   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 51
dl 0
loc 122
rs 10
c 0
b 0
f 0
wmc 11

8 Methods

Rating   Name   Duplication   Size   Complexity  
A handleSaveVettingTypeHintCommand() 0 10 1
A loadInstitutionConfigurationFor() 0 16 2
A assertValidRoleAndConvertIfValid() 0 13 3
A handleAppointRoleCommand() 0 12 1
A handleRetractRegistrationAuthorityCommand() 0 8 1
A handleAmendRegistrationAuthorityInformationCommand() 0 13 1
A __construct() 0 8 1
A handleAccreditIdentityCommand() 0 18 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\SimpleCommandHandler;
22
use Broadway\Repository\AggregateNotFoundException;
23
use Broadway\Repository\Repository as RepositoryInterface;
24
use Surfnet\Stepup\Configuration\EventSourcing\InstitutionConfigurationRepository;
25
use Surfnet\Stepup\Configuration\InstitutionConfiguration;
26
use Surfnet\Stepup\Configuration\Value\Institution as ConfigurationInstitution;
27
use Surfnet\Stepup\Configuration\Value\InstitutionConfigurationId;
28
use Surfnet\Stepup\Identity\Api\Identity;
29
use Surfnet\Stepup\Identity\Value\ContactInformation;
30
use Surfnet\Stepup\Identity\Value\IdentityId;
31
use Surfnet\Stepup\Identity\Value\Institution;
32
use Surfnet\Stepup\Identity\Value\Location;
33
use Surfnet\Stepup\Identity\Value\RegistrationAuthorityRole;
0 ignored issues
show
Bug introduced by
The type Surfnet\Stepup\Identity\...gistrationAuthorityRole was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
34
use Surfnet\StepupMiddleware\CommandHandlingBundle\Exception\RuntimeException;
35
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\AccreditIdentityCommand;
36
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\AmendRegistrationAuthorityInformationCommand;
37
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\AppointRoleCommand;
38
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\RetractRegistrationAuthorityCommand;
39
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\SaveVettingTypeHintCommand;
40
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Service\VettingTypeHintService;
41
42
/**
43
 * @SuppressWarnings("PHPMD.CouplingBetweenObjects")
44
 */
45
class RegistrationAuthorityCommandHandler extends SimpleCommandHandler
46
{
47
    public function __construct(
48
        private readonly RepositoryInterface $repository,
49
        private readonly InstitutionConfigurationRepository $institutionConfigurationRepository,
50
        /**
51
         * @var VettingTypeHintService;
52
         */
53
        private readonly VettingTypeHintService $vettingTypeHintService,
54
    ) {
55
    }
56
57
    public function handleAccreditIdentityCommand(AccreditIdentityCommand $command): void
58
    {
59
        /** @var Identity $identity */
60
        $identity = $this->repository->load(new IdentityId($command->identityId));
61
62
        $institutionConfiguration = $this->loadInstitutionConfigurationFor(new Institution($command->raInstitution));
0 ignored issues
show
Deprecated Code introduced by
The function Surfnet\StepupMiddleware...utionConfigurationFor() has been deprecated: Should be used until existing institution configurations have been migrated to using normalized ids ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

62
        $institutionConfiguration = /** @scrutinizer ignore-deprecated */ $this->loadInstitutionConfigurationFor(new Institution($command->raInstitution));

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

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

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

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

97
        $institutionConfiguration = /** @scrutinizer ignore-deprecated */ $this->loadInstitutionConfigurationFor(new Institution($command->raInstitution));

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

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

Loading history...
98
99
        $newRole = $this->assertValidRoleAndConvertIfValid($command->role, $command->UUID);
100
101
        $identity->appointAs(new Institution($command->raInstitution), $newRole, $institutionConfiguration);
102
103
        $this->repository->save($identity);
104
    }
105
106
    public function handleRetractRegistrationAuthorityCommand(RetractRegistrationAuthorityCommand $command): void
107
    {
108
        /** @var Identity $identity */
109
        $identity = $this->repository->load(new IdentityId($command->identityId));
110
111
        $identity->retractRegistrationAuthority(new Institution($command->raInstitution));
112
113
        $this->repository->save($identity);
114
    }
115
116
    public function handleSaveVettingTypeHintCommand(SaveVettingTypeHintCommand $command): void
117
    {
118
        /** @var Identity $identity */
119
        $identity = $this->repository->load(new IdentityId($command->identityId));
120
        $collection = $this->vettingTypeHintService->collectionFrom($command->hints);
121
        $identity->saveVettingTypeHints(
122
            new Institution($command->institution),
123
            $collection,
124
        );
125
        $this->repository->save($identity);
126
    }
127
128
    /**
129
     * @return RegistrationAuthorityRole
130
     */
131
    private function assertValidRoleAndConvertIfValid(string $role, string $commandId): RegistrationAuthorityRole
132
    {
133
        if ($role === 'ra') {
134
            return new RegistrationAuthorityRole(RegistrationAuthorityRole::ROLE_RA);
135
        } elseif ($role === 'raa') {
136
            return new RegistrationAuthorityRole(RegistrationAuthorityRole::ROLE_RAA);
137
        }
138
139
        throw new RuntimeException(
140
            sprintf(
141
                'Unknown role "%s" given by AccreditIdentityCommand "%s", must be "ra" or "raa"',
142
                $role,
143
                $commandId,
144
            ),
145
        );
146
    }
147
148
    /**
149
     * @deprecated Should be used until existing institution configurations have been migrated to using normalized ids
150
     */
151
    private function loadInstitutionConfigurationFor(Institution $institution): InstitutionConfiguration
152
    {
153
        $institution = new ConfigurationInstitution($institution->getInstitution());
154
        try {
155
            $institutionConfigurationId = InstitutionConfigurationId::normalizedFrom($institution);
156
            $institutionConfiguration = $this->institutionConfigurationRepository->load(
157
                $institutionConfigurationId->getInstitutionConfigurationId(),
158
            );
159
        } catch (AggregateNotFoundException) {
160
            $institutionConfigurationId = InstitutionConfigurationId::from($institution);
0 ignored issues
show
Deprecated Code introduced by
The function Surfnet\Stepup\Configura...ConfigurationId::from() has been deprecated: To be removed in next release; use normalizedFrom method to account for case-(in)sensitivity issues ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

160
            $institutionConfigurationId = /** @scrutinizer ignore-deprecated */ InstitutionConfigurationId::from($institution);

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

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

Loading history...
161
            $institutionConfiguration = $this->institutionConfigurationRepository->load(
162
                $institutionConfigurationId->getInstitutionConfigurationId(),
163
            );
164
        }
165
        assert($institutionConfiguration instanceof InstitutionConfiguration);
166
        return $institutionConfiguration;
167
    }
168
}
169