Passed
Push — feature/VSVGVQ-50 ( daa6dd )
by steven
07:47 queued 04:45
created

UserEntity::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php declare(strict_types=1);
2
3
namespace VSV\GVQ_API\User\Repositories\Entities;
4
5
use Ramsey\Uuid\Uuid;
6
use VSV\GVQ_API\Common\Repositories\Entities\Entity;
7
use Doctrine\ORM\Mapping as ORM;
8
use VSV\GVQ_API\Common\ValueObjects\Language;
9
use VSV\GVQ_API\Common\ValueObjects\NotEmptyString;
10
use VSV\GVQ_API\User\Models\User;
11
use VSV\GVQ_API\User\ValueObjects\Email;
12
use VSV\GVQ_API\User\ValueObjects\Password;
13
use VSV\GVQ_API\User\ValueObjects\Role;
14
15
/**
16
 * @ORM\Entity()
17
 * @ORM\Table(name="user")
18
 */
19
class UserEntity extends Entity
20
{
21
    /**
22
     * @var string
23
     *
24
     * @ORM\Column(type="string", length=255, unique=true, nullable=false)
25
     */
26
    private $email;
27
28
    /**
29
     * @var string
30
     *
31
     * @ORM\Column(type="string", length=255, nullable=false)
32
     */
33
    private $lastName;
34
35
    /**
36
     * @var string
37
     *
38
     * @ORM\Column(type="string", length=255, nullable=false)
39
     */
40
    private $firstName;
41
42
    /**
43
     * @var string
44
     *
45
     * @ORM\Column(type="string", length=20, nullable=false)
46
     */
47
    private $role;
48
49
    /**
50
     * @var string
51
     *
52
     * @ORM\Column(type="string", length=2, nullable=false)
53
     */
54
    private $language;
55
56
    /**
57
     * @var string|null
58
     *
59
     * @ORM\Column(type="string", length=255, nullable=true)
60
     */
61
    private $password;
62
63
    /**
64
     * @var bool
65
     *
66
     * @ORM\Column(type="boolean", nullable=false)
67
     */
68
    private $active;
69
70
    /**
71
     * @param string $id
72
     * @param string $email
73
     * @param string $lastName
74
     * @param string $firstName
75
     * @param string $role
76
     * @param string $language
77
     * @param string|null $password
78
     * @param bool $active
79
     */
80
    public function __construct(
81
        string $id,
82
        string $email,
83
        string $lastName,
84
        string $firstName,
85
        string $role,
86
        string $language,
87
        ?string $password,
88
        bool $active
89
    ) {
90
        parent::__construct($id);
91
92
        $this->email = $email;
93
        $this->lastName = $lastName;
94
        $this->firstName = $firstName;
95
        $this->role = $role;
96
        $this->language = $language;
97
        $this->password = $password;
98
        $this->active = $active;
99
    }
100
101
    /**
102
     * @param User $user
103
     * @return UserEntity
104
     */
105
    public static function fromUser(User $user): UserEntity
106
    {
107
        return new UserEntity(
108
            $user->getId()->toString(),
109
            $user->getEmail()->toNative(),
110
            $user->getLastName()->toNative(),
111
            $user->getFirstName()->toNative(),
112
            $user->getRole()->toNative(),
113
            $user->getLanguage()->toNative(),
114
            $user->getPassword() ? $user->getPassword()->toNative() : null,
115
            $user->isActive()
116
        );
117
    }
118
119
    /**
120
     * @return User
121
     */
122
    public function toUser(): User
123
    {
124
        $user = new User(
125
            Uuid::fromString($this->getId()),
126
            new Email($this->getEmail()),
127
            new NotEmptyString($this->getLastName()),
128
            new NotEmptyString($this->getFirstName()),
129
            new Role($this->getRole()),
130
            new Language($this->getLanguage()),
131
            $this->isActive()
132
        );
133
134
        if ($this->getPassword()) {
135
            $user = $user->withPassword(
136
                Password::fromHash($this->getPassword())
137
            );
138
        }
139
140
        return $user;
141
    }
142
143
    /**
144
     * @return string
145
     */
146
    public function getEmail(): string
147
    {
148
        return $this->email;
149
    }
150
151
    /**
152
     * @return string
153
     */
154
    public function getLastName(): string
155
    {
156
        return $this->lastName;
157
    }
158
159
    /**
160
     * @return string
161
     */
162
    public function getFirstName(): string
163
    {
164
        return $this->firstName;
165
    }
166
167
    /**
168
     * @return string
169
     */
170
    public function getRole(): string
171
    {
172
        return $this->role;
173
    }
174
175
    /**
176
     * @return string
177
     */
178
    public function getLanguage(): string
179
    {
180
        return $this->language;
181
    }
182
183
    /**
184
     * @return string|null
185
     */
186
    public function getPassword(): ?string
187
    {
188
        return $this->password;
189
    }
190
191
    /**
192
     * @return bool
193
     */
194
    public function isActive(): bool
195
    {
196
        return $this->active;
197
    }
198
}
199