Test Failed
Push — master ( 8af420...1fd71c )
by Nicolas
03:42
created

User::setImage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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