Completed
Push — master ( dc5b69...0eb8a8 )
by Michiel
05:16 queued 02:56
created

__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
dl 12
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 4
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 Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
22
use Exception;
23
use Rhumsaa\Uuid\Uuid;
24
use Surfnet\Stepup\Identity\Value\Institution;
25
use Surfnet\Stepup\Identity\Value\NameId;
26
use Surfnet\StepupMiddleware\CommandHandlingBundle\EventHandling\BufferedEventBus;
27
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\BootstrapIdentityWithYubikeySecondFactorCommand
28
    as BootstrapIdentityWithYubikeySecondFactorIdentityCommand;
29
use Surfnet\StepupMiddleware\CommandHandlingBundle\Pipeline\TransactionAwarePipeline;
30
use Surfnet\StepupMiddleware\ManagementBundle\Service\DBALConnectionHelper;
31
use Symfony\Component\Console\Command\Command;
32
use Symfony\Component\Console\Input\InputArgument;
33
use Symfony\Component\Console\Input\InputInterface;
34
use Symfony\Component\Console\Output\OutputInterface;
35
use Symfony\Component\DependencyInjection\Container;
36
37
final class BootstrapIdentityWithYubikeySecondFactorCommand extends Command
38
{
39
    /**
40
     * @var DBALConnectionHelper
41
     */
42
    private $connection;
43
44
    /**
45
     * @var BufferedEventBus
46
     */
47
    private $eventBus;
48
49
    /**
50
     * @var TransactionAwarePipeline
51
     */
52
    private $pipeline;
53
54
    /**
55
     * @var ServiceEntityRepository
56
     */
57
    private $projectionRepository;
58
59 View Code Duplication
    protected function configure()
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...
60
    {
61
        $this
62
            ->setName('middleware:bootstrap:identity-with-yubikey')
63
            ->setDescription('Creates an identity with a vetted Yubikey second factor')
64
            ->addArgument('name-id', InputArgument::REQUIRED, 'The NameID of the identity to create')
65
            ->addArgument('institution', InputArgument::REQUIRED, 'The institution of the identity to create')
66
            ->addArgument('common-name', InputArgument::REQUIRED, 'The Common Name of the identity to create')
67
            ->addArgument('email', InputArgument::REQUIRED, 'The e-mail address of the identity to create')
68
            ->addArgument('preferred-locale', InputArgument::REQUIRED, 'The preferred locale of the identity to create')
69
            ->addArgument(
70
                'yubikey',
71
                InputArgument::REQUIRED,
72
                'The public ID of the Yubikey. Remove the last 32 characters of a Yubikey OTP to acquire this.'
73
            );
74
    }
75
76 View Code Duplication
    public function __construct(
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...
77
        ServiceEntityRepository $projectionRepository,
78
        TransactionAwarePipeline $pipeline,
79
        BufferedEventBus $eventBus,
80
        DBALConnectionHelper $connection
81
    ) {
82
        parent::__construct();
83
        $this->projectionRepository = $projectionRepository;
84
        $this->pipeline = $pipeline;
85
        $this->eventBus = $eventBus;
86
        $this->connection = $connection;
87
    }
88
89
    protected function execute(InputInterface $input, OutputInterface $output)
90
    {
91
        /** @var Container $container */
92
93
        $nameId      = new NameId($input->getArgument('name-id'));
94
        $institution = new Institution($input->getArgument('institution'));
95
96
        if ($this->projectionRepository->hasIdentityWithNameIdAndInstitution($nameId, $institution)) {
97
            $output->writeln(
98
                sprintf(
99
                    '<error>An identity with name ID "%s" from institution "%s" already exists</error>',
100
                    $nameId->getNameId(),
101
                    $institution->getInstitution()
102
                )
103
            );
104
105
            return 1;
106
        }
107
108
        $command                  = new BootstrapIdentityWithYubikeySecondFactorIdentityCommand();
109
        $command->UUID            = (string) Uuid::uuid4();
110
        $command->identityId      = (string) Uuid::uuid4();
111
        $command->nameId          = $input->getArgument('name-id');
0 ignored issues
show
Documentation Bug introduced by
It seems like $input->getArgument('name-id') can also be of type array<integer,string>. However, the property $nameId is declared as type string. Maybe add an additional type 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 mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
112
        $command->institution     = $input->getArgument('institution');
0 ignored issues
show
Documentation Bug introduced by
It seems like $input->getArgument('institution') can also be of type array<integer,string>. However, the property $institution is declared as type string. Maybe add an additional type 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 mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
113
        $command->commonName      = $input->getArgument('common-name');
0 ignored issues
show
Documentation Bug introduced by
It seems like $input->getArgument('common-name') can also be of type array<integer,string>. However, the property $commonName is declared as type string. Maybe add an additional type 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 mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
114
        $command->email           = $input->getArgument('email');
0 ignored issues
show
Documentation Bug introduced by
It seems like $input->getArgument('email') can also be of type array<integer,string>. However, the property $email is declared as type string. Maybe add an additional type 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 mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
115
        $command->preferredLocale = $input->getArgument('preferred-locale');
0 ignored issues
show
Documentation Bug introduced by
It seems like $input->getArgument('preferred-locale') can also be of type array<integer,string>. However, the property $preferredLocale is declared as type string. Maybe add an additional type 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 mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
116
        $command->secondFactorId  = (string) Uuid::uuid4();
117
        $command->yubikeyPublicId = $input->getArgument('yubikey');
0 ignored issues
show
Documentation Bug introduced by
It seems like $input->getArgument('yubikey') can also be of type array<integer,string>. However, the property $yubikeyPublicId is declared as type string. Maybe add an additional type 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 mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
118
119
        $this->connection->beginTransaction();
120
121
        try {
122
            $command = $this->pipeline->process($command);
123
            $this->eventBus->flush();
124
125
            $this->connection->commit();
126
        } catch (Exception $e) {
127
            $output->writeln(sprintf(
128
                '<error>An Error occurred when trying to bootstrap the token for identity: "%s"</error>',
129
                $e->getMessage()
130
            ));
131
132
            $this->connection->rollBack();
133
134
            throw $e;
135
        }
136
137
        $output->writeln(sprintf(
138
            '<info>Successfully registered a Yubikey token with UUID %s</info>',
139
            $command->secondFactorId
0 ignored issues
show
Bug introduced by
Accessing secondFactorId on the interface Surfnet\StepupMiddleware...gBundle\Command\Command suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
140
        ));
141
    }
142
}
143