Passed
Push — master ( d346aa...fd2cec )
by Nicolas
04:54
created

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