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

WithConfirmationSignUpUserHandler   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 84
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 8

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 11 11 1
B __invoke() 24 24 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\UserAlreadyExistException;
17
use BenGorUser\User\Domain\Model\UserEmail;
18
use BenGorUser\User\Domain\Model\UserFactory;
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
24
/**
25
 * With confirmation sign up user user command handler class.
26
 *
27
 * @author Beñat Espiña <[email protected]>
28
 * @author Gorka Laucirica <[email protected]>
29
 */
30 View Code Duplication
class WithConfirmationSignUpUserHandler
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...
31
{
32
    /**
33
     * The user data transformer.
34
     *
35
     * @var UserDataTransformer
36
     */
37
    private $dataTransformer;
38
39
    /**
40
     * The user password encoder.
41
     *
42
     * @var UserPasswordEncoder
43
     */
44
    private $encoder;
45
46
    /**
47
     * The user factory.
48
     *
49
     * @var UserFactory
50
     */
51
    private $factory;
52
53
    /**
54
     * The user repository.
55
     *
56
     * @var UserRepository
57
     */
58
    private $repository;
59
60
    /**
61
     * Constructor.
62
     *
63
     * @param UserRepository      $aRepository      The user repository
64
     * @param UserPasswordEncoder $anEncoder        The password encoder
65
     * @param UserFactory         $aFactory         The user factory
66
     * @param UserDataTransformer $aDataTransformer The user data transformer
67
     */
68
    public function __construct(
69
        UserRepository $aRepository,
70
        UserPasswordEncoder $anEncoder,
71
        UserFactory $aFactory,
72
        UserDataTransformer $aDataTransformer
73
    ) {
74
        $this->repository = $aRepository;
75
        $this->encoder = $anEncoder;
76
        $this->factory = $aFactory;
77
        $this->dataTransformer = $aDataTransformer;
78
    }
79
80
    /**
81
     * Handles the given command.
82
     *
83
     * @param WithConfirmationSignUpUserCommand $aCommand The command
84
     *
85
     * @throws UserAlreadyExistException when the user alreay exists
86
     *
87
     * @return mixed
88
     */
89
    public function __invoke(WithConfirmationSignUpUserCommand $aCommand)
90
    {
91
        $email = new UserEmail($aCommand->email());
92
93
        if (null !== $this->repository->userOfEmail($email)) {
94
            throw new UserAlreadyExistException();
95
        }
96
97
        $userRoles = array_map(function ($role) {
98
            return new UserRole($role);
99
        }, $aCommand->roles());
100
101
        $user = $this->factory->register(
102
            $this->repository->nextIdentity(),
103
            $email,
104
            UserPassword::fromPlain($aCommand->password(), $this->encoder),
105
            $userRoles
106
        );
107
108
        $this->repository->persist($user);
109
        $this->dataTransformer->write($user);
110
111
        return $this->dataTransformer->read();
112
    }
113
}
114