1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Merodiro\Friendships; |
4
|
|
|
|
5
|
|
|
use Event; |
6
|
|
|
|
7
|
|
|
trait Friendable |
8
|
|
|
{ |
9
|
32 |
|
public function checkFriendship($user) |
10
|
|
|
{ |
11
|
32 |
|
if ($this->id == $user->id) { |
|
|
|
|
12
|
2 |
|
return 'same_user'; |
13
|
|
|
} |
14
|
|
|
|
15
|
30 |
|
$friendship = Friendship::betweenUsers($this, $user) |
16
|
30 |
|
->first(); |
17
|
|
|
|
18
|
30 |
|
if (!$friendship) { |
19
|
30 |
|
return 'not_friends'; |
20
|
|
|
} |
21
|
|
|
|
22
|
28 |
|
if ($friendship->status == 1) { |
23
|
6 |
|
return 'friends'; |
24
|
|
|
} |
25
|
28 |
|
if ($friendship->requester == $this->id) { |
26
|
10 |
|
return 'waiting'; |
27
|
|
|
} |
28
|
26 |
|
if ($friendship->user_requested == $this->id) { |
29
|
26 |
|
return 'pending'; |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
32 |
|
public function addFriend($recipient) |
34
|
|
|
{ |
35
|
32 |
|
$friendshipStatus = $this->checkFriendship($recipient); |
36
|
|
|
|
37
|
32 |
|
if ($friendshipStatus == 'not_friends') { |
38
|
30 |
|
Event::fire('friendrequest.sent', [$this, $recipient]); |
39
|
|
|
|
40
|
30 |
|
Friendship::create([ |
41
|
30 |
|
'requester' => $this->id, |
42
|
30 |
|
'user_requested' => $recipient->id, |
43
|
|
|
]); |
44
|
|
|
} |
45
|
32 |
|
return $friendshipStatus == 'not_friends'; |
46
|
|
|
} |
47
|
|
|
|
48
|
20 |
|
public function acceptFriend($sender) |
49
|
|
|
{ |
50
|
20 |
|
$friendshipStatus = $this->checkFriendship($sender); |
51
|
|
|
|
52
|
20 |
|
if ($friendshipStatus == 'pending') { |
53
|
18 |
|
Event::fire('friendrequest.accepted', [$this, $sender]); |
54
|
|
|
|
55
|
18 |
|
Friendship::betweenUsers($this, $sender) |
56
|
18 |
|
->update(['status' => 1]); |
57
|
|
|
} |
58
|
20 |
|
return $friendshipStatus == 'pending'; |
59
|
|
|
} |
60
|
|
|
|
61
|
10 |
|
public function deleteFriend($user) |
62
|
|
|
{ |
63
|
10 |
|
$friendshipStatus = $this->checkFriendship($user); |
64
|
|
|
|
65
|
10 |
|
if ($friendshipStatus != 'not_friends') { |
66
|
10 |
|
Event::fire('friendship.deleted', [$this, $user]); |
67
|
|
|
|
68
|
10 |
|
Friendship::betweenUsers($this, $user) |
69
|
10 |
|
->delete(); |
70
|
|
|
} |
71
|
10 |
|
return $friendshipStatus != 'not_friends'; |
72
|
|
|
} |
73
|
|
|
|
74
|
4 |
|
public function friends() |
75
|
|
|
{ |
76
|
4 |
|
$recipients = Friendship::whereSender($this) |
77
|
4 |
|
->accepted(1) |
78
|
4 |
|
->get(['user_requested']) |
79
|
4 |
|
->toArray(); |
80
|
|
|
|
81
|
4 |
|
$senders = Friendship::whereRecipient($this) |
82
|
4 |
|
->accepted(1) |
83
|
4 |
|
->get(['requester']) |
84
|
4 |
|
->toArray(); |
85
|
|
|
|
86
|
4 |
|
$friendsIds = array_merge($recipients, $senders); |
87
|
|
|
|
88
|
4 |
|
return static::whereIn('id', $friendsIds) |
89
|
4 |
|
->get(); |
90
|
|
|
} |
91
|
|
|
|
92
|
14 |
View Code Duplication |
public function friendRequestsReceived() |
|
|
|
|
93
|
|
|
{ |
94
|
14 |
|
$senders = Friendship::whereRecipient($this) |
95
|
14 |
|
->accepted(0) |
96
|
14 |
|
->get(['requester']) |
97
|
14 |
|
->toArray(); |
98
|
|
|
|
99
|
14 |
|
return static::whereIn('id', $senders) |
100
|
14 |
|
->get(); |
101
|
|
|
} |
102
|
|
|
|
103
|
8 |
View Code Duplication |
public function friendRequestsSent() |
|
|
|
|
104
|
|
|
{ |
105
|
8 |
|
$recipients = Friendship::whereSender($this) |
106
|
8 |
|
->accepted(0) |
107
|
8 |
|
->get(['user_requested']) |
108
|
8 |
|
->toArray(); |
109
|
|
|
|
110
|
8 |
|
return static::whereIn('id', $recipients) |
111
|
8 |
|
->get(); |
112
|
|
|
} |
113
|
|
|
|
114
|
4 |
|
public function isFriendsWith($user) |
115
|
|
|
{ |
116
|
4 |
|
$friendshipStatus = $this->checkFriendship($user); |
117
|
|
|
|
118
|
4 |
|
return $friendshipStatus === 'friends'; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: