| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the BenGorUser package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) Beñat Espiña <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * (c) Gorka Laucirica <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | namespace BenGorUser\User\Application\Command\SignUp; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use BenGorUser\User\Domain\Model\Exception\UserDoesNotExistException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use BenGorUser\User\Domain\Model\UserFactorySignUp; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use BenGorUser\User\Domain\Model\UserPassword; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use BenGorUser\User\Domain\Model\UserPasswordEncoder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use BenGorUser\User\Domain\Model\UserRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use BenGorUser\User\Domain\Model\UserRole; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use BenGorUser\User\Domain\Model\UserToken; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * By invitation sign up user user command handler class. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * @author Beñat Espiña <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * @author Gorka Laucirica <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 29 |  | View Code Duplication | class ByInvitationSignUpUserHandler | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * The user password encoder. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @var UserPasswordEncoder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     private $encoder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * The user factory. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @var UserFactorySignUp | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     private $factory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * The user repository. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @var UserRepository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     private $userRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * Constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @param UserRepository      $aUserRepository The user repository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @param UserPasswordEncoder $anEncoder       The password encoder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @param UserFactorySignUp   $aFactory        The user sign up factory | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |     public function __construct( | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         UserRepository $aUserRepository, | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         UserPasswordEncoder $anEncoder, | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |         UserFactorySignUp $aFactory | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |     ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |         $this->userRepository = $aUserRepository; | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |         $this->encoder = $anEncoder; | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |         $this->factory = $aFactory; | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * Handles the given command. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @param ByInvitationSignUpUserCommand $aCommand The command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * @throws UserDoesNotExistException when the user does not exist | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     public function __invoke(ByInvitationSignUpUserCommand $aCommand) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $user = $this->userRepository->userOfInvitationToken( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             new UserToken($aCommand->invitationToken()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         if (null === $user) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             throw new UserDoesNotExistException(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         foreach ($aCommand->roles() as $role) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             $user->grant(new UserRole($role)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         $user->changePassword(UserPassword::fromPlain($aCommand->password(), $this->encoder)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $user->enableAccount(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         $this->userRepository->persist($user); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 93 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |  | 
            
                        
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.