Completed
Push — feature/VSVGVQ-24 ( 890c4d...a3069c )
by Luc
05:52 queued 02:46
created

UserEntity   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
dl 0
loc 138
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
A getRole() 0 3 1
A getEmail() 0 3 1
A getPassword() 0 3 1
A fromUser() 0 9 2
A getFirstName() 0 3 1
A toUser() 0 17 2
A getLastName() 0 3 1
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\NotEmptyString;
9
use VSV\GVQ_API\User\Models\User;
10
use VSV\GVQ_API\User\ValueObjects\Email;
11
use VSV\GVQ_API\User\ValueObjects\Password;
12
use VSV\GVQ_API\User\ValueObjects\Role;
13
14
/**
15
 * @ORM\Entity()
16
 * @ORM\Table(name="user")
17
 */
18
class UserEntity extends Entity
19
{
20
    /**
21
     * @var string
22
     *
23
     * @ORM\Column(type="string", length=255, unique=true, nullable=false)
24
     */
25
    private $email;
26
27
    /**
28
     * @var string
29
     *
30
     * @ORM\Column(type="string", length=255, nullable=false)
31
     */
32
    private $lastName;
33
34
    /**
35
     * @var string
36
     *
37
     * @ORM\Column(type="string", length=255, nullable=false)
38
     */
39
    private $firstName;
40
41
    /**
42
     * @var string
43
     *
44
     * @ORM\Column(type="string", length=20, nullable=false)
45
     */
46
    private $role;
47
48
    /**
49
     * @var string|null
50
     *
51
     * @ORM\Column(type="string", length=255, nullable=true)
52
     */
53
    private $password;
54
55
    /**
56
     * @param string $id
57
     * @param string $email
58
     * @param string $lastName
59
     * @param string $firstName
60
     * @param string $role
61
     * @param string|null $password
62
     */
63
    public function __construct(
64
        string $id,
65
        string $email,
66
        string $lastName,
67
        string $firstName,
68
        string $role,
69
        ?string $password
70
    ) {
71
        parent::__construct($id);
72
73
        $this->email = $email;
74
        $this->lastName = $lastName;
75
        $this->firstName = $firstName;
76
        $this->role = $role;
77
        $this->password = $password;
78
    }
79
80
    /**
81
     * @param User $user
82
     * @return UserEntity
83
     */
84
    public static function fromUser(User $user): UserEntity
85
    {
86
        return new UserEntity(
87
            $user->getId()->toString(),
88
            $user->getEmail()->toNative(),
89
            $user->getLastName()->toNative(),
90
            $user->getFirstName()->toNative(),
91
            $user->getRole()->toNative(),
92
            $user->getPassword() ? $user->getPassword()->toNative() : null
93
        );
94
    }
95
96
    /**
97
     * @return User
98
     */
99
    public function toUser(): User
100
    {
101
        $user = new User(
102
            Uuid::fromString($this->getId()),
103
            new Email($this->getEmail()),
104
            new NotEmptyString($this->getLastName()),
105
            new NotEmptyString($this->getFirstName()),
106
            new Role($this->getRole())
107
        );
108
109
        if ($this->getPassword()) {
110
            $user = $user->withPassword(
111
                Password::fromHash($this->getPassword())
112
            );
113
        }
114
115
        return $user;
116
    }
117
118
    /**
119
     * @return string
120
     */
121
    public function getEmail(): string
122
    {
123
        return $this->email;
124
    }
125
126
    /**
127
     * @return string
128
     */
129
    public function getLastName(): string
130
    {
131
        return $this->lastName;
132
    }
133
134
    /**
135
     * @return string
136
     */
137
    public function getFirstName(): string
138
    {
139
        return $this->firstName;
140
    }
141
142
    /**
143
     * @return string
144
     */
145
    public function getRole(): string
146
    {
147
        return $this->role;
148
    }
149
150
    /**
151
     * @return string|null
152
     */
153
    public function getPassword(): ?string
154
    {
155
        return $this->password;
156
    }
157
}
158