Completed
Pull Request — master (#29)
by Valentyn
07:01
created

User::changeEmail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
ccs 2
cts 2
cp 1
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace App\Users\Entity;
5
6
use App\Users\Entity\UserProfile;
7
use Doctrine\Common\Collections\ArrayCollection;
8
use Doctrine\ORM\Mapping as ORM;
9
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
10
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
11
use Symfony\Component\Security\Core\User\UserInterface;
12
use JMS\Serializer\Annotation\ExclusionPolicy;
13
use JMS\Serializer\Annotation\Expose;
14
15
/**
16
 * @ORM\Entity(repositoryClass="App\Users\Repository\UserRepository")
17
 * @ORM\Table(name="users")
18
 * @UniqueEntity(fields="email", message="Email already taken")
19
 * @UniqueEntity(fields="username", message="Username already taken")
20
 * @ExclusionPolicy("all")
21
 */
22
class User implements UserInterface
23
{
24
    /**
25
     * @ORM\Id()
26
     * @ORM\GeneratedValue()
27
     * @ORM\Column(type="integer")
28
     * @Expose
29
     */
30
    private $id;
31
32
    /**
33
     * @var $profile UserProfile
34
     * @ORM\OneToOne(targetEntity="App\Users\Entity\UserProfile", cascade={"persist", "remove"})
35
     * @Expose
36
     */
37
    private $profile;
38
39
    /**
40
     * @ORM\Column(type="string", length=255, unique=true)
41
     */
42
    private $email;
43
44
    /**
45
     * @ORM\Column(type="integer", length=1, options={"default": 0})
46
     */
47
    private $isEmailConfirmed = 0;
48
49
    /**
50
     * @ORM\Column(type="string", length=255, unique=true)
51
     * @Expose
52
     */
53
    private $username;
54
55
    /**
56
     * @ORM\Embedded(class="App\Users\Entity\UserRoles", columnPrefix=false)
57
     */
58
    private $roles;
59
60
    /**
61
     * @var string
62
     */
63
    private $plainPassword;
64
65
    /**
66
     * @ORM\Column(type="string", length=64)
67
     */
68
    private $password;
69
70
    public function __construct(string $email, string $username, string $password)
71
    {
72 31
        $this->roles = new UserRoles();
73
        $this->profile = new UserProfile($this);
74 31
75 31
        $this->email = $email;
76 31
        $this->username = $username;
77
        $this->setPlainPassword($password);
78 8
    }
79
80 8
    public function getProfile(): UserProfile
81
    {
82
        return $this->profile;
83 4
    }
84
85 4
    public function getId(): ?int
86
    {
87
        return $this->id;
88 31
    }
89
90 31
    public function getEmail(): string
91 3
    {
92 3
        return $this->email;
93
    }
94
95
    public function setPlainPassword(string $plainPassword): self
96 31
    {
97
        if (!empty($plainPassword)) {
98
            $this->plainPassword = $plainPassword;
99 1
            // Change some mapped values so preUpdate will get called.
100
            $this->password = ''; // just blank it out
101 1
        }
102 1
103
        return $this;
104 1
    }
105 1
106 1
    public function getPlainPassword(): string
107
    {
108
        return (string)$this->plainPassword;
109
    }
110
111
    public function setPassword($password, UserPasswordEncoderInterface $passwordEncoder): self
112 3
    {
113
        $this->plainPassword = $password;
114 3
        $this->password = $passwordEncoder->encodePassword($this, $password);
115 1
116 1
        return $this;
117
    }
118
119 3
    public function isPasswordValid($password, UserPasswordEncoderInterface $passwordEncoder): bool
120
    {
121 3
        return $passwordEncoder->isPasswordValid($this, $password);
122
    }
123
124 35
    public function getPassword(): ?string
125
    {
126 35
        return $this->password;
127 33
    }
128
129
    public function getSalt(): ?string
130 5
    {
131
        return null;
132 5
    }
133
134
    public function getUsername(): ?string
135
    {
136 5
        return $this->username;
137
    }
138
139 33
    public function eraseCredentials(): self
140
    {
141 33
        $this->plainPassword = null;
142
143
        return $this;
144 5
    }
145
146 5
    public function confirmEmail(): self
147 5
    {
148
        $this->isEmailConfirmed = 1;
149 5
150
        return $this;
151
    }
152 5
153
    public function changeEmail($email): self
154
    {
155 7
        $this->email = $email;
156
        $this->isEmailConfirmed = 0;
157 7
158
        return $this;
159
    }
160 9
161
    /**
162 9
     * @return array
163
     */
164 9
    public function getRoles()
165
    {
166
        return $this->getRolesObject()->getRoles();
167 6
    }
168
169 6
    public function getRolesObject(): UserRoles
170
    {
171
        return $this->roles;
172
    }
173
}