Completed
Pull Request — develop (#304)
by Michiel
04:12 queued 01:57
created

BootstrapIdentityCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 Rhumsaa\Uuid\Uuid;
23
use Surfnet\Stepup\Identity\Value\Institution;
24
use Surfnet\Stepup\Identity\Value\NameId;
25
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\CreateIdentityCommand;
26
use Symfony\Component\Console\Input\InputArgument;
27
use Symfony\Component\Console\Input\InputInterface;
28
use Symfony\Component\Console\Output\OutputInterface;
29
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
30
31
final class BootstrapIdentityCommand extends AbstractBootstrapCommand
32
{
33
    protected function configure()
34
    {
35
        $this
36
            ->setDescription('Creates an identity')
37
            ->addArgument('name-id', InputArgument::REQUIRED, 'The NameID of the identity to create')
38
            ->addArgument('institution', InputArgument::REQUIRED, 'The institution of the identity to create')
39
            ->addArgument('common-name', InputArgument::REQUIRED, 'The Common Name of the identity to create')
40
            ->addArgument('email', InputArgument::REQUIRED, 'The e-mail address of the identity to create')
41
            ->addArgument('preferred-locale', InputArgument::REQUIRED, 'The preferred locale of the identity to create')
42
            ->addArgument('actor-id', InputArgument::REQUIRED, 'The id of the vetting actor');
43
    }
44
45
    protected function execute(InputInterface $input, OutputInterface $output)
46
    {
47
        $this->tokenStorage->setToken(
48
            new AnonymousToken('cli.bootstrap-identity-with-sms-token', 'cli', ['ROLE_SS'])
0 ignored issues
show
Documentation introduced by
array('ROLE_SS') is of type array<integer,string,{"0":"string"}>, but the function expects a array<integer,object<Sym...curity\Core\Role\Role>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
49
        );
50
51
        $nameId = new NameId($input->getArgument('name-id'));
52
        $institutionText = $input->getArgument('institution');
53
        $institution = new Institution($institutionText);
0 ignored issues
show
Bug introduced by
It seems like $institutionText defined by $input->getArgument('institution') on line 52 can also be of type array<integer,string> or null; however, Surfnet\Stepup\Identity\...titution::__construct() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
54
        $commonName = $input->getArgument('common-name');
55
        $email = $input->getArgument('email');
56
        $preferredLocale = $input->getArgument('preferred-locale');
57
        $actorId = $input->getArgument('actor-id');
58
59
        $this->enrichEventMetadata($actorId);
60
61
        $output->writeln(sprintf('<comment>Adding an identity named: %s</comment>', $commonName));
62
        if ($this->tokenBootstrapService->hasIdentityWithNameIdAndInstitution($nameId, $institution)) {
63
            $output->writeln(
64
                sprintf(
65
                    '<error>An identity with name ID "%s" from institution "%s" already exists</error>',
66
                    $nameId->getNameId(),
67
                    $institution->getInstitution()
68
                )
69
            );
70
            return;
71
        }
72
        try {
73
            $this->beginTransaction();
74
            $output->writeln('<info>Creating a new identity</info>');
75
            $identity = $this->createIdentity($institution, $nameId, $commonName, $email, $preferredLocale);
76
            $this->finishTransaction();
77
        } catch (Exception $e) {
78
            $output->writeln(
79
                sprintf(
80
                    '<error>An Error occurred when trying to bootstrap the identity: "%s"</error>',
81
                    $e->getMessage()
82
                )
83
            );
84
            $this->rollback();
85
            throw $e;
86
        }
87
        $output->writeln(
88
            sprintf('<info>Successfully created identity with UUID %s</info>', $identity->id)
89
        );
90
    }
91
92
    protected function createIdentity(
93
        Institution $institution,
94
        NameId $nameId,
95
        $commonName,
96
        $email,
97
        $preferredLocale
98
    ) {
99
        $identity = new CreateIdentityCommand();
100
        $identity->UUID = (string)Uuid::uuid4();
101
        $identity->id = (string)Uuid::uuid4();
102
        $identity->institution = $institution->getInstitution();
103
        $identity->nameId = $nameId->getNameId();
104
        $identity->commonName = $commonName;
105
        $identity->email = $email;
106
        $identity->preferredLocale = $preferredLocale;
107
        $this->process($identity);
108
109
        return $identity;
110
    }
111
}
112