Passed
Push — master ( c83d6c...5bd3d9 )
by Nicolas
05:49
created

User::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Entity;
6
7
use Doctrine\Common\Collections\ArrayCollection;
8
use Doctrine\Common\Collections\Collection;
9
use Doctrine\ORM\Mapping as ORM;
10
use Gedmo\Timestampable\Traits\TimestampableEntity;
11
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
12
use Symfony\Component\Security\Core\User\UserInterface;
13
use Symfony\Component\Validator\Constraints as Assert;
14
15
/**
16
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
17
 * @ORM\Table(name="rw_user")
18
 *
19
 * @UniqueEntity("email", message="user.email.unique")
20
 * @UniqueEntity("username", message="user.username.unique")
21
 */
22
class User implements UserInterface
23
{
24
    use TimestampableEntity;
25
26
    /**
27
     * @ORM\Id()
28
     * @ORM\GeneratedValue()
29
     * @ORM\Column(type="integer")
30
     */
31
    private ?int $id = null;
32
33
    /**
34
     * @ORM\Column(type="string", length=255, unique=true)
35
     *
36
     * @Assert\NotBlank(message="user.email.not_blank")
37
     * @Assert\Email(message="user.email.email")
38
     */
39
    private ?string $email = null;
40
41
    /**
42
     * @ORM\Column(type="string", length=255)
43
     *
44
     * @Assert\NotBlank(message="user.password.not_blank")
45
     * @Assert\Length(min="8", minMessage="user.password.length.min")
46
     */
47
    private ?string $password = null;
48
49
    /**
50
     * @ORM\Column(type="string", length=255, unique=true)
51
     *
52
     * @Assert\NotBlank(message="user.username.not_blank")
53
     * @Assert\Length(
54
     *     min="1",
55
     *     max="20",
56
     *     minMessage="user.username.length.min",
57
     *     maxMessage="user.username.length.max"
58
     * )
59
     */
60
    private ?string $username = null;
61
62
    /**
63
     * @ORM\Column(type="text", nullable=true)
64
     */
65
    private ?string $bio = null;
66
67
    /**
68
     * @ORM\Column(type="text", nullable=true)
69
     *
70
     * @Assert\Url(message="user.image.url")
71
     */
72
    private ?string $image = null;
73
74
    /**
75
     * @var Collection|User[]
76
     *
77
     * @ORM\ManyToMany(targetEntity="App\Entity\User", mappedBy="followers")
78
     */
79
    private Collection $followed;
80
81
    /**
82
     * @var Collection|User[]
83
     *
84
     * @ORM\ManyToMany(targetEntity="App\Entity\User", inversedBy="followed")
85
     * @ORM\JoinTable(
86
     *   name="rw_user_follower",
87
     *   joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
88
     *   inverseJoinColumns={@ORM\JoinColumn(name="follower_id", referencedColumnName="id")}
89
     * )
90
     */
91
    private Collection $followers;
92
93
    /**
94
     * @var Collection|Article[]
95
     *
96
     * @ORM\ManyToMany(targetEntity="App\Entity\Article", inversedBy="favoritedBy")
97
     * @ORM\JoinTable(name="rw_user_favorite")
98
     */
99
    private Collection $favorites;
100
101 4
    public function __construct()
102
    {
103 4
        $this->followed = new ArrayCollection();
104 4
        $this->followers = new ArrayCollection();
105 4
        $this->favorites = new ArrayCollection();
106 4
    }
107
108
    public function __toString(): string
109
    {
110
        return \sprintf('%s', $this->email);
111
    }
112
113 7
    public function getId(): ?int
114
    {
115 7
        return $this->id;
116
    }
117
118 7
    public function getEmail(): ?string
119
    {
120 7
        return $this->email;
121
    }
122
123 4
    public function setEmail(?string $email): void
124
    {
125 4
        $this->email = $email;
126 4
    }
127
128 28
    public function getPassword(): ?string
129
    {
130 28
        return $this->password;
131
    }
132
133 3
    public function setPassword(?string $password): void
134
    {
135 3
        $this->password = $password;
136 3
    }
137
138 24
    public function getUsername(): string
139
    {
140 24
        return $this->username ?? '';
141
    }
142
143 3
    public function setUsername(?string $username): void
144
    {
145 3
        $this->username = $username;
146 3
    }
147
148 24
    public function getBio(): ?string
149
    {
150 24
        return $this->bio;
151
    }
152
153 1
    public function setBio(?string $bio): void
154
    {
155 1
        $this->bio = $bio;
156 1
    }
157
158 24
    public function getImage(): ?string
159
    {
160 24
        return $this->image;
161
    }
162
163 1
    public function setImage(?string $image): void
164
    {
165 1
        $this->image = $image;
166 1
    }
167
168
    /**
169
     * @return string[]
170
     */
171 26
    public function getRoles(): array
172
    {
173 26
        return ['ROLE_USER'];
174
    }
175
176
    /**
177
     * {@inheritdoc}
178
     */
179 26
    public function getSalt(): ?string
180
    {
181 26
        return null;
182
    }
183
184
    /**
185
     * {@inheritdoc}
186
     */
187 25
    public function eraseCredentials(): void
188
    {
189 25
    }
190
191 10
    public function follows(User $user): bool
192
    {
193 10
        return $this->followed->contains($user);
194
    }
195
196 1
    public function follow(User $user): void
197
    {
198 1
        if ($user->getFollowers()->contains($this)) {
199
            return;
200
        }
201
202 1
        $user->getFollowers()->add($this);
203 1
    }
204
205 1
    public function unfollow(User $user): void
206
    {
207 1
        if (!$user->getFollowers()->contains($this)) {
208 1
            return;
209
        }
210
211
        $user->getFollowers()->removeElement($this);
212
    }
213
214
    /**
215
     * @return Collection|User[]
216
     */
217 2
    public function getFollowers(): Collection
218
    {
219 2
        return $this->followers;
220
    }
221
222
    /**
223
     * @param Collection|User[] $followers
224
     */
225
    public function setFollowers(Collection $followers): void
226
    {
227
        $this->followers = $followers;
228
    }
229
230
    /**
231
     * @return Collection|User[]
232
     */
233 3
    public function getFolloweds(): Collection
234
    {
235 3
        return $this->followed;
236
    }
237
238
    /**
239
     * @return Collection|Article[]
240
     */
241
    public function getFavorites(): Collection
242
    {
243
        return $this->favorites;
244
    }
245
246
    /**
247
     * @param Collection|Article[] $favorites
248
     */
249
    public function setFavorites(Collection $favorites): void
250
    {
251
        $this->favorites = $favorites;
252
    }
253
254 6
    public function hasFavorite(Article $article): bool
255
    {
256 6
        return $this->favorites->contains($article);
257
    }
258
259 1
    public function addToFavorites(Article $article): void
260
    {
261 1
        if ($this->favorites->contains($article)) {
262 1
            return;
263
        }
264
265
        $this->favorites->add($article);
266
    }
267
268 1
    public function removeFromFavorites(Article $article): void
269
    {
270 1
        if (!$this->favorites->contains($article)) {
271
            return;
272
        }
273
274 1
        $this->favorites->removeElement($article);
275 1
    }
276
}
277