Passed
Pull Request — master (#16)
by steven
04:56
created

UserEntity::getLanguage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
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
     * @param string $id
65
     * @param string $email
66
     * @param string $lastName
67
     * @param string $firstName
68
     * @param string $role
69
     * @param string $language
70
     * @param string|null $password
71
     */
72
    public function __construct(
73
        string $id,
74
        string $email,
75
        string $lastName,
76
        string $firstName,
77
        string $role,
78
        string $language,
79
        ?string $password
80
    ) {
81
        parent::__construct($id);
82
83
        $this->email = $email;
84
        $this->lastName = $lastName;
85
        $this->firstName = $firstName;
86
        $this->role = $role;
87
        $this->language = $language;
88
        $this->password = $password;
89
    }
90
91
    /**
92
     * @param User $user
93
     * @return UserEntity
94
     */
95
    public static function fromUser(User $user): UserEntity
96
    {
97
        return new UserEntity(
98
            $user->getId()->toString(),
99
            $user->getEmail()->toNative(),
100
            $user->getLastName()->toNative(),
101
            $user->getFirstName()->toNative(),
102
            $user->getRole()->toNative(),
103
            $user->getLanguage()->toNative(),
104
            $user->getPassword() ? $user->getPassword()->toNative() : null
105
        );
106
    }
107
108
    /**
109
     * @return User
110
     */
111
    public function toUser(): User
112
    {
113
        $user = new User(
114
            Uuid::fromString($this->getId()),
115
            new Email($this->getEmail()),
116
            new NotEmptyString($this->getLastName()),
117
            new NotEmptyString($this->getFirstName()),
118
            new Role($this->getRole()),
119
            new Language($this->getLanguage())
120
        );
121
122
        if ($this->getPassword()) {
123
            $user = $user->withPassword(
124
                Password::fromHash($this->getPassword())
125
            );
126
        }
127
128
        return $user;
129
    }
130
131
    /**
132
     * @return string
133
     */
134
    public function getEmail(): string
135
    {
136
        return $this->email;
137
    }
138
139
    /**
140
     * @return string
141
     */
142
    public function getLastName(): string
143
    {
144
        return $this->lastName;
145
    }
146
147
    /**
148
     * @return string
149
     */
150
    public function getFirstName(): string
151
    {
152
        return $this->firstName;
153
    }
154
155
    /**
156
     * @return string
157
     */
158
    public function getRole(): string
159
    {
160
        return $this->role;
161
    }
162
163
    /**
164
     * @return string
165
     */
166
    public function getLanguage(): string
167
    {
168
        return $this->language;
169
    }
170
171
    /**
172
     * @return string|null
173
     */
174
    public function getPassword(): ?string
175
    {
176
        return $this->password;
177
    }
178
}
179