Completed
Pull Request — master (#9)
by Valentyn
02:26
created

User::getProfile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

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 4
rs 10
ccs 2
cts 2
cp 1
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace App\Entity;
5
6
use Doctrine\ORM\Mapping as ORM;
7
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
8
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
9
use Symfony\Component\Security\Core\User\UserInterface;
10
use JMS\Serializer\Annotation\Exclude;
11
use JMS\Serializer\Annotation\Expose;
12
13
/**
14
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
15
 * @ORM\Table(name="users")
16
 * @UniqueEntity(fields="email", message="Email already taken")
17
 * @UniqueEntity(fields="username", message="Username already taken")
18
 */
19
class User implements UserInterface, \Serializable
20
{
21
    const ROLE_USER = 'ROLE_USER';
22
23
    /**
24
     * @ORM\Id()
25
     * @ORM\GeneratedValue()
26
     * @ORM\Column(type="integer")
27
     */
28
    private $id;
29
30
    /**
31
     * @var $profile UserProfile
32
     * @ORM\OneToOne(targetEntity="App\Entity\UserProfile", mappedBy="user", cascade={"persist", "remove"})
33
     */
34
    private $profile;
35
36
    /**
37
     * @ORM\Column(type="string", length=255, unique=true)
38
     */
39
    public $email;
40
41
    /**
42
     * @ORM\Column(type="string", length=255, unique=true)
43
     */
44
    public $username;
45
46
    /**
47
     * @ORM\Column(type="string", length=255, nullable=true)
48
     */
49
    private $roles;
50
51
    /**
52
     * @Exclude
53
     */
54
    public $plainPassword;
55
56
    /**
57
     * @Exclude
58
     * @ORM\Column(type="string", length=64)
59
     */
60
    private $password;
61
62 22
    public function __construct()
63
    {
64 22
        $this->addRole(self::ROLE_USER);
65 22
        $this->profile = new UserProfile($this);
66 22
    }
67
68 8
    public function getProfile(): UserProfile
69
    {
70 8
        return $this->profile;
71
    }
72
73 2
    public function getId(): ?int
74
    {
75 2
        return $this->id;
76
    }
77
78 22
    public function addRole(string $role): self
79
    {
80 22
        if (array_search($role, $this->getRoles()) === false) {
81 3
            return $this->setRoles(
82 3
                array_merge($this->getRoles(), [$role])
83
            );
84
        }
85
86 22
        return $this;
87
    }
88
89 1
    public function removeRole(string $role): self
90
    {
91 1
        $roles = $this->getRoles();
92 1
        $foundedRoleKey = array_search($role, $roles);
93
94 1
        if ($foundedRoleKey !== false) {
95 1
            unset($roles[$foundedRoleKey]);
96 1
            return $this->setRoles($roles);
97
        }
98
99
        return $this;
100
    }
101
102 3
    private function setRoles(array $roles): self
103
    {
104 3
        if (!count($roles)) {
105 1
            $this->roles = null;
106 1
            return $this;
107
        }
108
109 3
        $this->roles = json_encode($roles);
110
111 3
        return $this;
112
    }
113
114 22
    public function getRoles(): array
115
    {
116 22
        if (!$this->roles) {
117 22
            return $this->getDefaultRoles();
118
        }
119
120 3
        $roles = (array)json_decode($this->roles);
121
122 3
        if (!count($roles)) {
123
            return $this->getDefaultRoles();
124
        }
125
126 3
        return array_values($roles);
127
    }
128
129 22
    private function getDefaultRoles()
130
    {
131 22
        return [self::ROLE_USER];
132
    }
133
134 4
    public function setPassword($password, UserPasswordEncoderInterface $passwordEncoder): self
135
    {
136 4
        $this->plainPassword = $password;
137 4
        $this->password = $passwordEncoder->encodePassword($this, $this->plainPassword);
138
139 4
        return $this;
140
    }
141
142 1
    public function isPasswordValid($password, UserPasswordEncoderInterface $passwordEncoder): bool
143
    {
144 1
        return $passwordEncoder->isPasswordValid($this, $password);
145
    }
146
147 1
    public function getPassword(): ?string
148
    {
149 1
        return $this->password;
150
    }
151
152 5
    public function getSalt(): ?string
153
    {
154 5
        return null;
155
    }
156
157 3
    public function getUsername(): ?string
158
    {
159 3
        return $this->username;
160
    }
161
162 1
    public function eraseCredentials(): self
163
    {
164 1
        $this->plainPassword = null;
165
166 1
        return $this;
167
    }
168
169 1
    public function serialize(): string
170
    {
171 1
        return serialize([
172 1
            $this->id,
173 1
            $this->email,
174 1
            $this->username,
175 1
            $this->roles,
176
        ]);
177
    }
178
179 1
    public function unserialize($serialized): self
180
    {
181
        list (
182 1
            $this->id,
183 1
            $this->email,
184 1
            $this->username,
185 1
            $this->roles,
186 1
            ) = unserialize($serialized);
187
188 1
        return $this;
189
    }
190
}