Passed
Pull Request — main (#557)
by Johan
10:46 queued 05:30
created

BootstrapIdentityCommand::__invoke()   A

Complexity

Conditions 3
Paths 6

Size

Total Lines 58
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 32
nc 6
nop 7
dl 0
loc 58
rs 9.408
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Copyright 2020 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\MiddlewareBundle\Console\Command;
20
21
use Exception;
22
use Surfnet\Stepup\Identity\Value\Institution;
23
use Surfnet\Stepup\Identity\Value\NameId;
24
use Surfnet\StepupMiddleware\MiddlewareBundle\Service\BootstrapCommandService;
25
use Surfnet\StepupMiddleware\MiddlewareBundle\Service\TransactionHelper;
0 ignored issues
show
Bug introduced by
The type Surfnet\StepupMiddleware...rvice\TransactionHelper 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...
26
use Symfony\Component\Console\Attribute\Argument;
27
use Symfony\Component\Console\Attribute\AsCommand;
28
use Symfony\Component\Console\Output\OutputInterface;
29
30
#[AsCommand(name: 'middleware:bootstrap:identity', description: 'Creates an identity')]
31
final class BootstrapIdentityCommand
32
{
33
    public function __construct(private readonly BootstrapCommandService $bootstrapService, private readonly TransactionHelper $transactionHelper)
34
    {
35
    }
36
37
    public function __invoke(
38
        #[Argument(description: 'The NameID of the identity to create', name: 'name-id')]
39
        string $nameId,
40
        #[Argument(description: 'The institution of the identity to create', name: 'institution')]
41
        string $institution,
42
        #[Argument(description: 'The Common Name of the identity to create', name: 'common-name')]
43
        string $commonName,
44
        #[Argument(description: 'The e-mail address of the identity to create', name: 'email')]
45
        string $email,
46
        #[Argument(description: 'The preferred locale of the identity to create', name: 'preferred-locale')]
47
        string $preferredLocale,
48
        #[Argument(description: 'The id of the vetting actor', name: 'actor-id')]
49
        string $actorId,
50
        OutputInterface $output
51
    ): int {
52
        $nameId = new NameId($nameId);
53
        $institutionText = $institution;
54
        $institution = new Institution($institutionText);
55
56
        $this->bootstrapService->enrichEventMetadata($actorId);
57
58
        $output->writeln(sprintf('<comment>Adding an identity named: %s</comment>', $commonName));
59
        if ($this->bootstrapService->identityExists($nameId, $institution)) {
60
            $output->writeln(
61
                sprintf(
62
                    '<error>An identity with name ID "%s" from institution "%s" already exists</error>',
63
                    $nameId->getNameId(),
64
                    $institution->getInstitution(),
65
                ),
66
            );
67
            return 1;
68
        }
69
        try {
70
            $this->transactionHelper->beginTransaction();
71
            $output->writeln('<info>Creating a new identity</info>');
72
            $identity = $this->bootstrapService->createIdentity(
73
                $institution,
74
                $nameId,
75
                $commonName,
76
                $email,
77
                $preferredLocale,
78
            );
79
            $this->transactionHelper->finishTransaction();
80
        } catch (Exception $e) {
81
            $output->writeln(
82
                sprintf(
83
                    '<error>An Error occurred when trying to bootstrap the identity: "%s"</error>',
84
                    $e->getMessage(),
85
                ),
86
            );
87
            $this->transactionHelper->rollback();
88
            return 1;
89
        }
90
        $output->writeln(
91
            sprintf('<info>Successfully created identity with UUID %s</info>', $identity->id),
92
        );
93
94
        return 0;
95
    }
96
}
97