Completed
Push — master ( 728b57...1a78ff )
by Beñat
03:57
created

ByInvitationSignUpUserHandler   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 98
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 11

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 0
cbo 11
dl 98
loc 98
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 13 13 1
B __invoke() 28 28 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Service\SignUp;
14
15
use BenGorUser\User\Application\DataTransformer\UserDataTransformer;
16
use BenGorUser\User\Domain\Model\Exception\UserGuestDoesNotExistException;
17
use BenGorUser\User\Domain\Model\UserFactory;
18
use BenGorUser\User\Domain\Model\UserGuestRepository;
19
use BenGorUser\User\Domain\Model\UserPassword;
20
use BenGorUser\User\Domain\Model\UserPasswordEncoder;
21
use BenGorUser\User\Domain\Model\UserRepository;
22
use BenGorUser\User\Domain\Model\UserRole;
23
use BenGorUser\User\Domain\Model\UserToken;
24
25
/**
26
 * By invitation sign up user user command handler class.
27
 *
28
 * @author Beñat Espiña <[email protected]>
29
 * @author Gorka Laucirica <[email protected]>
30
 */
31 View Code Duplication
class ByInvitationSignUpUserHandler
0 ignored issues
show
Duplication introduced by
This class 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...
32
{
33
    /**
34
     * The user data transformer.
35
     *
36
     * @var UserDataTransformer
37
     */
38
    private $dataTransformer;
39
40
    /**
41
     * The user password encoder.
42
     *
43
     * @var UserPasswordEncoder
44
     */
45
    private $encoder;
46
47
    /**
48
     * The user factory.
49
     *
50
     * @var UserFactory
51
     */
52
    private $factory;
53
54
    /**
55
     * The user repository.
56
     *
57
     * @var UserRepository
58
     */
59
    private $userRepository;
60
61
    /**
62
     * The user guest repository.
63
     *
64
     * @var UserGuestRepository
65
     */
66
    private $userGuestRepository;
67
68
    /**
69
     * Constructor.
70
     *
71
     * @param UserRepository      $aUserRepository      The user repository
72
     * @param UserPasswordEncoder $anEncoder            The password encoder
73
     * @param UserFactory         $aFactory             The user factory
74
     * @param UserDataTransformer $aDataTransformer     The user data transformer
75
     * @param UserGuestRepository $aUserGuestRepository The user guest repository
76
     */
77
    public function __construct(
78
        UserRepository $aUserRepository,
79
        UserPasswordEncoder $anEncoder,
80
        UserFactory $aFactory,
81
        UserDataTransformer $aDataTransformer,
82
        UserGuestRepository $aUserGuestRepository
83
    ) {
84
        $this->userRepository = $aUserRepository;
85
        $this->encoder = $anEncoder;
86
        $this->factory = $aFactory;
87
        $this->dataTransformer = $aDataTransformer;
88
        $this->userGuestRepository = $aUserGuestRepository;
89
    }
90
91
    /**
92
     * Handles the given command.
93
     *
94
     * @param ByInvitationSignUpUserCommand $aCommand The command
95
     *
96
     * @throws UserGuestDoesNotExistException when the user guest does not exist
97
     *
98
     * @return mixed
99
     */
100
    public function __invoke(ByInvitationSignUpUserCommand $aCommand)
101
    {
102
        $userGuest = $this->userGuestRepository->userGuestOfInvitationToken(
103
            new UserToken($aCommand->invitationToken())
104
        );
105
        if (null === $userGuest) {
106
            throw new UserGuestDoesNotExistException();
107
        }
108
        $email = $userGuest->email();
109
        $this->userGuestRepository->remove($userGuest);
110
111
        $userRoles = array_map(function ($role) {
112
            return new UserRole($role);
113
        }, $aCommand->roles());
114
115
        $user = $this->factory->register(
116
            $this->userRepository->nextIdentity(),
117
            $email,
118
            UserPassword::fromPlain($aCommand->password(), $this->encoder),
119
            $userRoles
120
        );
121
        $user->enableAccount();
122
123
        $this->userRepository->persist($user);
124
        $this->dataTransformer->write($user);
125
126
        return $this->dataTransformer->read();
127
    }
128
}
129