Skip to content

Commit 603156c

Browse files
committed
Update groups config structure
1 parent e98bb42 commit 603156c

File tree

6 files changed

+55
-53
lines changed

6 files changed

+55
-53
lines changed

src/Models/FriendFriendshipGroups.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Hootlex\Friendships\Models;
44

5-
use Hootlex\Friendships\Status;
65
use Illuminate\Database\Eloquent\Model;
76

87
/**
@@ -15,7 +14,7 @@ class FriendFriendshipGroups extends Model
1514
/**
1615
* @var array
1716
*/
18-
protected $fillable = ['friendship_id', 'group_id', 'friend_id', 'friend_type'];
17+
protected $fillable = ['friendship_id', 'group_slug', 'friend_id', 'friend_type'];
1918

2019
/**
2120
* @var bool

src/Models/Friendship.php

+5-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Hootlex\Friendships\Models;
44

5-
use Hootlex\Friendships\Status;
65
use Illuminate\Database\Eloquent\Model;
76

87
/**
@@ -87,23 +86,21 @@ public function scopeWhereSender($query, $model)
8786
/**
8887
* @param $query
8988
* @param Model $model
90-
* @param string $groupSlug
89+
* @param string $group
9190
* @return \Illuminate\Database\Eloquent\Builder
9291
*/
93-
public function scopeWhereGroup($query, $model, $groupSlug)
92+
public function scopeWhereGroup($query, $model, $group)
9493
{
9594

9695
$groupsPivotTable = config('friendships.tables.fr_groups_pivot');
9796
$friendsPivotTable = config('friendships.tables.fr_pivot');
9897
$groupsAvailable = config('friendships.groups', []);
9998

100-
if ('' !== $groupSlug && isset($groupsAvailable[$groupSlug])) {
99+
if ('' !== $group && isset($groupsAvailable[$group])) {
101100

102-
$groupId = $groupsAvailable[$groupSlug];
103-
104-
$query->join($groupsPivotTable, function ($join) use ($groupsPivotTable, $friendsPivotTable, $groupId, $model) {
101+
$query->join($groupsPivotTable, function ($join) use ($groupsPivotTable, $friendsPivotTable, $group, $model) {
105102
$join->on($groupsPivotTable . '.friendship_id', '=', $friendsPivotTable . '.id')
106-
->where($groupsPivotTable . '.group_id', '=', $groupId)
103+
->where($groupsPivotTable . '.group_slug', '=', $group)
107104
->where(function ($query) use ($groupsPivotTable, $friendsPivotTable, $model) {
108105
$query->where($groupsPivotTable . '.friend_id', '!=', $model->getKey())
109106
->where($groupsPivotTable . '.friend_type', '=', $model->getMorphClass());

src/Traits/Friendable.php

+41-35
Original file line numberDiff line numberDiff line change
@@ -111,22 +111,22 @@ public function denyFriendRequest(Model $recipient)
111111

112112
/**
113113
* @param Model $friend
114-
* @param $groupSlug
114+
* @param string $group
115115
* @return bool
116116
*/
117-
public function groupFriend(Model $friend, $groupSlug)
117+
public function groupFriend(Model $friend, $group)
118118
{
119119

120120
$friendship = $this->findFriendship($friend)->whereStatus(Status::ACCEPTED)->first();
121-
$groupsAvailable = config('friendships.groups', []);
121+
$groupsAvailable = config('friendships.groups', []);
122122

123-
if (!isset($groupsAvailable[$groupSlug]) || empty($friendship)) {
124-
return false;
123+
if (!isset($groupsAvailable[$group]) || empty($friendship)) {
124+
return false;
125125
}
126126

127127
$group = $friendship->groups()->firstOrCreate([
128128
'friendship_id' => $friendship->id,
129-
'group_id' => $groupsAvailable[$groupSlug],
129+
'group_slug' => $group,
130130
'friend_id' => $friend->getKey(),
131131
'friend_type' => $friend->getMorphClass(),
132132
]);
@@ -137,10 +137,10 @@ public function groupFriend(Model $friend, $groupSlug)
137137

138138
/**
139139
* @param Model $friend
140-
* @param $groupSlug
140+
* @param string $group
141141
* @return bool
142142
*/
143-
public function ungroupFriend(Model $friend, $groupSlug = '')
143+
public function ungroupFriend(Model $friend, $group="")
144144
{
145145

146146
$friendship = $this->findFriendship($friend)->first();
@@ -156,8 +156,8 @@ public function ungroupFriend(Model $friend, $groupSlug = '')
156156
'friend_type' => $friend->getMorphClass(),
157157
];
158158

159-
if ('' !== $groupSlug && isset($groupsAvailable[$groupSlug])) {
160-
$where['group_id'] = $groupsAvailable[$groupSlug];
159+
if ('' !== $group && isset($groupsAvailable[$group])) {
160+
$where['group_slug'] = $group;
161161
}
162162

163163
$result = $friendship->groups()->where($where)->delete();
@@ -213,34 +213,34 @@ public function getFriendship(Model $recipient)
213213
/**
214214
* @return \Illuminate\Database\Eloquent\Collection
215215
*
216-
* @param string $groupSlug
216+
* @param string $group
217217
*
218218
*/
219-
public function getAllFriendships($groupSlug = '')
219+
public function getAllFriendships($group = '')
220220
{
221-
return $this->findFriendships(null, $groupSlug)->get();
221+
return $this->findFriendships(null, $group)->get();
222222
}
223223

224224
/**
225225
* @return \Illuminate\Database\Eloquent\Collection
226226
*
227-
* @param string $groupSlug
227+
* @param string $group
228228
*
229229
*/
230-
public function getPendingFriendships($groupSlug = '')
230+
public function getPendingFriendships($group = '')
231231
{
232-
return $this->findFriendships(Status::PENDING, $groupSlug)->get();
232+
return $this->findFriendships(Status::PENDING, $group)->get();
233233
}
234234

235235
/**
236236
* @return \Illuminate\Database\Eloquent\Collection
237237
*
238-
* @param string $groupSlug
238+
* @param string $group
239239
*
240240
*/
241-
public function getAcceptedFriendships($groupSlug = '')
241+
public function getAcceptedFriendships($group = '')
242242
{
243-
return $this->findFriendships(Status::ACCEPTED, $groupSlug)->get();
243+
return $this->findFriendships(Status::ACCEPTED, $group)->get();
244244
}
245245

246246
/**
@@ -294,13 +294,13 @@ public function getFriendRequests()
294294
* It will return the 'friends' models. ex: App\User
295295
*
296296
* @param int $perPage Number
297-
* @param string $groupSlug
297+
* @param string $group
298298
*
299299
* @return \Illuminate\Database\Eloquent\Collection
300300
*/
301-
public function getFriends($perPage = 0, $groupSlug = '')
301+
public function getFriends($perPage = 0, $group = '')
302302
{
303-
return $this->getOrPaginate($this->getFriendsQueryBuilder($groupSlug), $perPage);
303+
return $this->getOrPaginate($this->getFriendsQueryBuilder($group), $perPage);
304304
}
305305

306306
/**
@@ -343,13 +343,13 @@ public function getFriendsOfFriends($perPage = 0)
343343
/**
344344
* Get the number of friends
345345
*
346-
* @param string $groupSlug
346+
* @param string $group
347347
*
348348
* @return integer
349349
*/
350-
public function getFriendsCount($groupSlug = '')
350+
public function getFriendsCount($group = '')
351351
{
352-
$friendsCount = $this->findFriendships(Status::ACCEPTED, $groupSlug)->count();
352+
$friendsCount = $this->findFriendships(Status::ACCEPTED, $group)->count();
353353
return $friendsCount;
354354
}
355355

@@ -413,11 +413,11 @@ private function findFriendship(Model $recipient)
413413

414414
/**
415415
* @param $status
416-
* @param string $groupSlug
416+
* @param string $group
417417
*
418418
* @return \Illuminate\Database\Eloquent\Collection
419419
*/
420-
private function findFriendships($status = null, $groupSlug = '')
420+
private function findFriendships($status = null, $group = '')
421421
{
422422

423423
$query = Friendship::where(function ($query) {
@@ -426,7 +426,7 @@ private function findFriendships($status = null, $groupSlug = '')
426426
})->orWhere(function ($q) {
427427
$q->whereRecipient($this);
428428
});
429-
})->whereGroup($this, $groupSlug);
429+
})->whereGroup($this, $group);
430430

431431
//if $status is passed, add where clause
432432
if (!is_null($status)) {
@@ -439,14 +439,14 @@ private function findFriendships($status = null, $groupSlug = '')
439439
/**
440440
* Get the query builder of the 'friend' model
441441
*
442-
* @param string $groupSlug
442+
* @param string $group
443443
*
444444
* @return \Illuminate\Database\Eloquent\Builder
445445
*/
446-
private function getFriendsQueryBuilder($groupSlug = '')
446+
private function getFriendsQueryBuilder($group = '')
447447
{
448448

449-
$friendships = $this->findFriendships(Status::ACCEPTED, $groupSlug)->get(['sender_id', 'recipient_id']);
449+
$friendships = $this->findFriendships(Status::ACCEPTED, $group)->get(['sender_id', 'recipient_id']);
450450
$recipients = $friendships->pluck('recipient_id')->all();
451451
$senders = $friendships->pluck('sender_id')->all();
452452

@@ -481,11 +481,11 @@ private function getMutualFriendsQueryBuilder(Model $other)
481481
/**
482482
* Get the query builder for friendsOfFriends ('friend' model)
483483
*
484-
* @param string $groupSlug
484+
* @param string $group
485485
*
486486
* @return \Illuminate\Database\Eloquent\Builder
487487
*/
488-
private function friendsOfFriendsQueryBuilder($groupSlug = '')
488+
private function friendsOfFriendsQueryBuilder($group = '')
489489
{
490490
$friendships = $this->findFriendships(Status::ACCEPTED)->get(['sender_id', 'recipient_id']);
491491
$recipients = $friendships->pluck('recipient_id')->all();
@@ -502,7 +502,7 @@ private function friendsOfFriendsQueryBuilder($groupSlug = '')
502502
$q->whereIn('recipient_id', $friendIds);
503503
});
504504
})
505-
->whereGroup($this, $groupSlug)
505+
->whereGroup($this, $group)
506506
->get(['sender_id', 'recipient_id']);
507507

508508
$fofIds = array_unique(
@@ -535,12 +535,18 @@ public function groups()
535535
{
536536
return $this->morphMany(FriendFriendshipGroups::class, 'friend');
537537
}
538-
538+
539+
/**
540+
* @param $builder
541+
* @param $perPage
542+
* @return mixed
543+
*/
539544
protected function getOrPaginate($builder, $perPage)
540545
{
541546
if ($perPage == 0) {
542547
return $builder->get();
543548
}
544549
return $builder->paginate($perPage);
545550
}
551+
546552
}

src/config/friendships.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
],
99

1010
'groups' => [
11-
'acquaintances' => 0,
12-
'close_friends' => 1,
13-
'family' => 2
11+
'acquaintances' => 'Acquaintances',
12+
'close_friends' => 'Close Friends',
13+
'family' => 'Family'
1414
]
1515

1616
];

src/database/migrations/create_friendships_groups_table.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ public function up() {
1515

1616
$table->integer('friendship_id')->unsigned();
1717
$table->morphs('friend');
18-
$table->integer('group_id')->unsigned();
18+
$table->string('group_slug');
1919

2020
$table->foreign('friendship_id')
2121
->references('id')
2222
->on(config('friendships.tables.fr_pivot'))
2323
->onDelete('cascade');
2424

25-
$table->unique(['friendship_id', 'friend_id', 'friend_type', 'group_id'], 'unique');
25+
$table->unique(['friendship_id', 'friend_id', 'friend_type', 'group_slug'], 'unique');
2626

2727
});
2828

tests/config/friendships.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
],
99

1010
'groups' => [
11-
'acquaintances' => 0,
12-
'close_friends' => 1,
13-
'family' => 2
11+
'acquaintances' => 'Acquaintances',
12+
'close_friends' => 'Close Friends',
13+
'family' => 'Family'
1414
]
1515

1616
];

0 commit comments

Comments
 (0)