User::getPassword()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Donut Social Network - Yet another experimental social network.
5
 * Copyright (C) 2016-2017, Dejan Angelov <[email protected]>
6
 *
7
 * This file is part of Donut Social Network.
8
 *
9
 * Donut Social Network is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation, either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * Donut Social Network is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with Donut Social Network.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 * @package Donut Social Network
23
 * @copyright Copyright (C) 2016-2017, Dejan Angelov <[email protected]>
24
 * @license https://github.com/angelov/donut/blob/master/LICENSE
25
 * @author Dejan Angelov <[email protected]>
26
 */
27
28
namespace Angelov\Donut\Users;
29
30
use JMS\Serializer\Annotation as Serializer;
31
use Angelov\Donut\Friendships\Friendship;
32
use Angelov\Donut\Friendships\FriendshipRequests\FriendshipRequest;
33
use Angelov\Donut\Places\City;
34
use Angelov\Donut\Thoughts\Thought;
35
use Doctrine\Common\Collections\ArrayCollection;
36
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
37
use Symfony\Component\Security\Core\User\UserInterface;
38
use Doctrine\ORM\Mapping as ORM;
39
40
/**
41
 * @ORM\Entity()
42
 * @ORM\Table(name="user")
43
 * @UniqueEntity(fields={"email"}, message="The email is already in use.")
44
 * @Serializer\ExclusionPolicy("all")
45
 */
46
class User implements UserInterface
47
{
48
    /**
49
     * @ORM\Id
50
     * @ORM\GeneratedValue(strategy="NONE")
51
     * @ORM\Column(type="guid")
52
     * @Serializer\Type(name="string")
53
     */
54
    private $id;
55
56
    /**
57
     * @ORM\Column(type="string", unique=true)
58
     * @Serializer\Expose()
59
     */
60
    private $email;
61
62
    /**
63
     * @ORM\Column(type="string")
64
     */
65
    private $password;
66
67
    /**
68
     * @ORM\Column(type="string")
69
     * @Serializer\Expose()
70
     */
71
    private $name;
72
73
    /**
74
     * @ORM\OneToMany(targetEntity="Angelov\Donut\Thoughts\Thought", mappedBy="author")
75
     */
76
    private $thoughts;
77
78
    /**
79
     * @ORM\Column(type="boolean", options={"default": false})
80
     * @Serializer\Expose()
81
     */
82
    private $isAdmin = false;
83
84
    /**
85
     * @ORM\OneToMany(targetEntity="Angelov\Donut\Friendships\FriendshipRequests\FriendshipRequest", mappedBy="fromUser")
86
     */
87
    private $sentFriendshipRequests;
88
89
    /**
90
     * @ORM\OneToMany(targetEntity="Angelov\Donut\Friendships\FriendshipRequests\FriendshipRequest", mappedBy="toUser")
91
     */
92
    private $receivedFriendshipRequests;
93
94
    /**
95
     * @ORM\OneToMany(targetEntity="Angelov\Donut\Friendships\Friendship", mappedBy="user", cascade={"remove"})
96
     */
97
    private $friendships;
98
99
    /**
100
     * @ORM\ManyToOne(targetEntity="Angelov\Donut\Places\City", inversedBy="residents", cascade={"persist"})
101
     */
102
    private $city;
103
104
    public function __construct(string $id, string $name, string $email, string $password, City $city)
105
    {
106
        $this->id = $id;
107
        $this->name = $name;
108
        $this->email = $email;
109
        $this->password = $password;
110
        $this->thoughts = new ArrayCollection();
111
        $this->sentFriendshipRequests = new ArrayCollection();
112
        $this->receivedFriendshipRequests = new ArrayCollection();
113
        $this->friendships = new ArrayCollection();
114
        $this->city = $city;
115
    }
116
117
    public function getId() : string
118
    {
119
        return $this->id;
120
    }
121
122
    public function setId(string $id) : void
123
    {
124
        $this->id = $id;
125
    }
126
127
    public function getUsername() : string
128
    {
129
        return $this->email;
130
    }
131
132
    public function setEmail(string $email = '') : void
133
    {
134
        $this->email = $email;
135
    }
136
137
    public function getEmail() : string
138
    {
139
        return $this->email;
140
    }
141
142
    public function getRoles() : array
143
    {
144
        return ['ROLE_USER'];
145
    }
146
147
    public function getPassword() : string
148
    {
149
        return $this->password;
150
    }
151
152
    public function setPassword(string $password = '') : void
153
    {
154
        $this->password = $password;
155
    }
156
157
    public function getSalt() : string
158
    {
159
        return '';
160
    }
161
162
    /**
163
     * @return Thought[]
164
     */
165
    public function getThoughts() : array
166
    {
167
        return $this->thoughts->getValues();
168
    }
169
170
    public function addThought(Thought $thought) : void
171
    {
172
        $this->thoughts->add($thought);
173
    }
174
175
    public function eraseCredentials() : void
176
    {
177
    }
178
179
    public function getName() : string
180
    {
181
        return $this->name;
182
    }
183
184
    public function setName(string $name = '') : void
185
    {
186
        $this->name = $name;
187
    }
188
189
    public function isIsAdmin(): bool
190
    {
191
        return $this->isAdmin;
192
    }
193
194
    // @todo remove admin stuff
195
    public function setIsAdmin(bool $isAdmin) : void
196
    {
197
        $this->isAdmin = $isAdmin;
198
    }
199
200
    public function equals(User $user) : bool
201
    {
202
        return $this->getId() === $user->getId();
203
    }
204
205
    /**
206
     * @return FriendshipRequest[]
207
     */
208
    public function getSentFriendshipRequests() : array
209
    {
210
        return $this->sentFriendshipRequests->getValues();
211
    }
212
213
    public function addSentFriendshipRequest(FriendshipRequest $friendshipRequest) : void
214
    {
215
        if (!$this->sentFriendshipRequests->contains($friendshipRequest)) {
216
            $this->sentFriendshipRequests->add($friendshipRequest);
217
        }
218
    }
219
220
    public function hasSentFriendshipRequestTo(User $user) : bool
221
    {
222
        /** @var FriendshipRequest $friendshipRequest */
223
        foreach ($this->sentFriendshipRequests as $friendshipRequest) {
224
            if ($friendshipRequest->getToUser()->equals($user)) {
225
                return true;
226
            }
227
        }
228
229
        return false;
230
    }
231
232
    /**
233
     * @return FriendshipRequest[]
234
     */
235
    public function getReceivedFriendshipRequests() : array
236
    {
237
        return $this->receivedFriendshipRequests->getValues();
238
    }
239
240
    public function addReceivedFriendshipRequest(FriendshipRequest $friendshipRequest) : void
241
    {
242
        if (!$this->receivedFriendshipRequests->contains($friendshipRequest)) {
243
            $this->receivedFriendshipRequests->add($friendshipRequest);
244
        }
245
    }
246
247
    public function hasReceivedFriendshipRequestFrom(User $user) : bool
248
    {
249
        foreach ($this->getReceivedFriendshipRequests() as $friendshipRequest) {
250
            if ($friendshipRequest->getFromUser()->equals($user)) {
251
                return true;
252
            }
253
        }
254
255
        return false;
256
    }
257
258
    /**
259
     * @return User[]
260
     */
261
    public function getFriends() : array
262
    {
263
        $friends = [];
264
265
        /** @var Friendship $friendship */
266
        foreach ($this->friendships as $friendship) {
267
            $friends[] = $friendship->getFriend();
268
        }
269
270
        return $friends;
271
    }
272
273
    public function addFriendship(Friendship $friendship) : void
274
    {
275
        $this->friendships->add($friendship);
276
    }
277
278
    // @todo this will have a big effect on performance, use redis or something
279
    public function isFriendWith(User $user) : bool
280
    {
281
        /** @var Friendship $friendship */
282
        foreach ($this->friendships as $friendship) {
283
            $friend = $friendship->getFriend();
284
285
            if ($friend->equals($user)) {
286
                return true;
287
            }
288
        }
289
290
        return false;
291
    }
292
293
    public function getCity() : City
294
    {
295
        return $this->city;
296
    }
297
298
    public function setCity(City $city) : void
299
    {
300
        $this->city = $city;
301
    }
302
}
303