| 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() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 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( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         $command->institution     = $input->getArgument('institution'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         $command->commonName      = $input->getArgument('common-name'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         $command->email           = $input->getArgument('email'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         $command->preferredLocale = $input->getArgument('preferred-locale'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         $command->secondFactorId  = (string) Uuid::uuid4(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         $command->yubikeyPublicId = $input->getArgument('yubikey'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 142 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 143 |  |  |  | 
            
                        
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.