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

BootstrapIdentityWithYubikeySecondFactorCommand::execute()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 54
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 36
nc 4
nop 2
dl 0
loc 54
rs 9.344
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 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\MiddlewareBundle\Console\Command;
20
21
use Exception;
22
use Ramsey\Uuid\Uuid;
23
use Surfnet\Stepup\Identity\Value\Institution;
24
use Surfnet\Stepup\Identity\Value\NameId;
25
use Surfnet\StepupMiddleware\ApiBundle\Identity\Repository\IdentityRepository;
26
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\BootstrapIdentityWithYubikeySecondFactorCommand
27
    as BootstrapIdentityWithYubikeySecondFactorIdentityCommand;
28
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...
29
use Symfony\Component\Console\Attribute\Argument;
30
use Symfony\Component\Console\Attribute\AsCommand;
31
use Symfony\Component\Console\Output\OutputInterface;
32
33
/**
34
 * @SuppressWarnings("PHPMD.CouplingBetweenObjects")
35
 */
36
#[AsCommand(
37
    name: 'middleware:bootstrap:identity-with-yubikey',
38
    description: 'Creates an identity with a vetted Yubikey second factor',
39
)]
40
final class BootstrapIdentityWithYubikeySecondFactorCommand
41
{
42
    public function __construct(private readonly IdentityRepository $projectionRepository, private readonly TransactionHelper $transactionHelper)
43
    {
44
    }
45
46
    public function __invoke(
47
        #[Argument(description: 'The NameID of the identity to create', name: 'name-id')]
48
        string $nameId,
49
        #[Argument(description: 'The institution of the identity to create', name: 'institution')]
50
        string $institution,
51
        #[Argument(description: 'The Common Name of the identity to create', name: 'common-name')]
52
        string $commonName,
53
        #[Argument(description: 'The e-mail address of the identity to create', name: 'email')]
54
        string $email,
55
        #[Argument(description: 'The preferred locale of the identity to create', name: 'preferred-locale')]
56
        string $preferredLocale,
57
        #[Argument(description: 'The public ID of the Yubikey. Remove the last 32 characters of a Yubikey OTP to acquire this.', name: 'yubikey')]
58
        string $yubikey,
59
        OutputInterface $output
60
    ): int {
61
        $nameId = new NameId($nameId);
62
        $institution = new Institution($institution);
63
64
        if ($this->projectionRepository->hasIdentityWithNameIdAndInstitution($nameId, $institution)) {
65
            $output->writeln(
66
                sprintf(
67
                    '<error>An identity with name ID "%s" from institution "%s" already exists</error>',
68
                    $nameId->getNameId(),
69
                    $institution->getInstitution(),
70
                ),
71
            );
72
73
            return 1;
74
        }
75
76
        $command = new BootstrapIdentityWithYubikeySecondFactorIdentityCommand();
77
        $command->UUID = (string)Uuid::uuid4();
78
        $command->identityId = (string)Uuid::uuid4();
79
        $command->nameId = $nameId;
80
        $command->institution = $institution;
81
        $command->commonName = $commonName;
82
        $command->email = $email;
83
        $command->preferredLocale = $preferredLocale;
84
        $secondFactorId = (string)Uuid::uuid4();
85
        $command->secondFactorId = $secondFactorId;
86
        $command->yubikeyPublicId = $yubikey;
87
88
        $this->transactionHelper->beginTransaction();
89
90
        try {
91
            $command = $this->transactionHelper->process($command);
0 ignored issues
show
Unused Code introduced by
The assignment to $command is dead and can be removed.
Loading history...
92
            $this->transactionHelper->finishTransaction();
93
        } catch (Exception $e) {
94
            $output->writeln(
95
                sprintf(
96
                    '<error>An Error occurred when trying to bootstrap the token for identity: "%s"</error>',
97
                    $e->getMessage(),
98
                ),
99
            );
100
101
            $this->transactionHelper->rollBack();
102
103
            throw $e;
104
        }
105
106
        $output->writeln(
107
            sprintf(
108
                '<info>Successfully registered a Yubikey token with UUID %s</info>',
109
                $secondFactorId,
110
            ),
111
        );
112
        return 0;
113
    }
114
}
115