|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Merodiro\Friendships; |
|
4
|
|
|
|
|
5
|
|
|
use Merodiro\Friendships\Exceptions\AddFriendFailed; |
|
6
|
|
|
use Merodiro\Friendships\Exceptions\AcceptFriendFailed; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
|
8
|
|
|
use Illuminate\Support\Collection; |
|
9
|
|
|
|
|
10
|
|
|
trait Friendable |
|
11
|
|
|
{ |
|
12
|
38 |
|
public function checkFriendship($user): string |
|
13
|
|
|
{ |
|
14
|
38 |
|
if ($this->id == $user->id) { |
|
|
|
|
|
|
15
|
2 |
|
return 'SAME_USER'; |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
36 |
|
$friendship = Friendship::betweenUsers($this, $user); |
|
19
|
|
|
|
|
20
|
36 |
|
if ($friendship->count() == 0) { |
|
21
|
36 |
|
return 'NOT_FRIENDS'; |
|
22
|
32 |
|
} elseif ($friendship->count() == 2) { |
|
23
|
8 |
|
return 'FRIENDS'; |
|
24
|
32 |
|
} elseif ($friendship->first()->user_id == $this->id) { |
|
25
|
8 |
|
return 'WAITING'; |
|
26
|
|
|
} else { |
|
27
|
28 |
|
return 'PENDING'; |
|
28
|
|
|
} |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
38 |
|
public function addFriend($recipient): void |
|
32
|
|
|
{ |
|
33
|
38 |
|
$friendshipStatus = $this->checkFriendship($recipient); |
|
34
|
|
|
|
|
35
|
38 |
|
if ($friendshipStatus === 'NOT_FRIENDS') { |
|
36
|
36 |
|
$this->friends()->attach($recipient); |
|
37
|
36 |
|
event('friendrequest.sent', [$this, $recipient]); |
|
38
|
|
|
} else { |
|
39
|
8 |
|
throw new AddFriendFailed($friendshipStatus); |
|
40
|
|
|
} |
|
41
|
36 |
|
} |
|
42
|
|
|
|
|
43
|
24 |
|
public function acceptFriend($sender): void |
|
44
|
|
|
{ |
|
45
|
24 |
|
$friendshipStatus = $this->checkFriendship($sender); |
|
46
|
|
|
|
|
47
|
24 |
|
if ($friendshipStatus == 'PENDING') { |
|
48
|
22 |
|
$this->friends()->attach($sender, ['status' => 1]); |
|
49
|
22 |
|
$sender->friends()->updateExistingPivot($this, ['status' => 1]); |
|
50
|
|
|
|
|
51
|
22 |
|
event('friendrequest.accepted', [$this, $sender]); |
|
52
|
|
|
} else { |
|
53
|
2 |
|
throw new AcceptFriendFailed($friendshipStatus); |
|
54
|
|
|
} |
|
55
|
22 |
|
} |
|
56
|
|
|
|
|
57
|
10 |
|
public function deleteFriend($user): void |
|
58
|
|
|
{ |
|
59
|
10 |
|
$friendshipStatus = $this->checkFriendship($user); |
|
60
|
|
|
|
|
61
|
10 |
|
if ($friendshipStatus != 'NOT_FRIENDS') { |
|
62
|
10 |
|
$this->friends()->detach($user); |
|
63
|
10 |
|
$user->friends()->detach($this); |
|
64
|
|
|
|
|
65
|
10 |
|
event('friendship.deleted', [$this, $user]); |
|
66
|
|
|
} |
|
67
|
10 |
|
} |
|
68
|
|
|
|
|
69
|
36 |
|
public function friends(): BelongsToMany |
|
70
|
|
|
{ |
|
71
|
36 |
|
return $this->belongsToMany(config('friendships.user_model'), 'friendships', 'user_id', 'friend_id') |
|
|
|
|
|
|
72
|
36 |
|
->where('status', 1); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
10 |
|
public function friendRequestsReceived(): BelongsToMany |
|
76
|
|
|
{ |
|
77
|
10 |
|
return $this->belongsToMany(config('friendships.user_model'), 'friendships', 'friend_id', 'user_id') |
|
|
|
|
|
|
78
|
10 |
|
->where('status', 0); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
6 |
|
public function friendRequestsSent(): BelongsToMany |
|
82
|
|
|
{ |
|
83
|
6 |
|
return $this->belongsToMany(config('friendships.user_model'), 'friendships', 'user_id', 'friend_id') |
|
|
|
|
|
|
84
|
6 |
|
->where('status', 0); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
4 |
|
public function isFriendsWith($user): bool |
|
88
|
|
|
{ |
|
89
|
4 |
|
return $this->friends->contains($user); |
|
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
2 |
|
public function mutualFriendsCount($user): int |
|
93
|
|
|
{ |
|
94
|
2 |
|
$userFriends = $user->friends->pluck('id'); |
|
95
|
2 |
|
$friends = $this->friends->pluck('id'); |
|
96
|
|
|
|
|
97
|
2 |
|
return $userFriends->intersect($friends)->count(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
2 |
|
public function mutualFriends($user): Collection |
|
101
|
|
|
{ |
|
102
|
2 |
|
$userFriends = $user->friends->pluck('id'); |
|
103
|
2 |
|
$friends = $this->friends->pluck('id'); |
|
104
|
|
|
|
|
105
|
2 |
|
$mutualIds = $userFriends->intersect($friends); |
|
106
|
|
|
|
|
107
|
2 |
|
return static::whereIn('id', $mutualIds)->get(); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
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: