diff --git a/src/Models/FriendFriendshipGroups.php b/src/Models/FriendFriendshipGroups.php index 807134b..e627450 100644 --- a/src/Models/FriendFriendshipGroups.php +++ b/src/Models/FriendFriendshipGroups.php @@ -2,7 +2,6 @@ namespace Hootlex\Friendships\Models; -use Hootlex\Friendships\Status; use Illuminate\Database\Eloquent\Model; /** @@ -15,7 +14,7 @@ class FriendFriendshipGroups extends Model /** * @var array */ - protected $fillable = ['friendship_id', 'group_id', 'friend_id', 'friend_type']; + protected $fillable = ['friendship_id', 'group_slug', 'friend_id', 'friend_type']; /** * @var bool diff --git a/src/Models/Friendship.php b/src/Models/Friendship.php index b7c8773..f0d19f1 100644 --- a/src/Models/Friendship.php +++ b/src/Models/Friendship.php @@ -2,7 +2,6 @@ namespace Hootlex\Friendships\Models; -use Hootlex\Friendships\Status; use Illuminate\Database\Eloquent\Model; /** @@ -87,23 +86,21 @@ public function scopeWhereSender($query, $model) /** * @param $query * @param Model $model - * @param string $groupSlug + * @param string $group * @return \Illuminate\Database\Eloquent\Builder */ - public function scopeWhereGroup($query, $model, $groupSlug) + public function scopeWhereGroup($query, $model, $group) { $groupsPivotTable = config('friendships.tables.fr_groups_pivot'); $friendsPivotTable = config('friendships.tables.fr_pivot'); $groupsAvailable = config('friendships.groups', []); - if ('' !== $groupSlug && isset($groupsAvailable[$groupSlug])) { + if ('' !== $group && isset($groupsAvailable[$group])) { - $groupId = $groupsAvailable[$groupSlug]; - - $query->join($groupsPivotTable, function ($join) use ($groupsPivotTable, $friendsPivotTable, $groupId, $model) { + $query->join($groupsPivotTable, function ($join) use ($groupsPivotTable, $friendsPivotTable, $group, $model) { $join->on($groupsPivotTable . '.friendship_id', '=', $friendsPivotTable . '.id') - ->where($groupsPivotTable . '.group_id', '=', $groupId) + ->where($groupsPivotTable . '.group_slug', '=', $group) ->where(function ($query) use ($groupsPivotTable, $friendsPivotTable, $model) { $query->where($groupsPivotTable . '.friend_id', '!=', $model->getKey()) ->where($groupsPivotTable . '.friend_type', '=', $model->getMorphClass()); diff --git a/src/Traits/Friendable.php b/src/Traits/Friendable.php index c6ea5bd..71c80d3 100644 --- a/src/Traits/Friendable.php +++ b/src/Traits/Friendable.php @@ -111,22 +111,22 @@ public function denyFriendRequest(Model $recipient) /** * @param Model $friend - * @param $groupSlug + * @param string $group * @return bool */ - public function groupFriend(Model $friend, $groupSlug) + public function groupFriend(Model $friend, $group) { $friendship = $this->findFriendship($friend)->whereStatus(Status::ACCEPTED)->first(); - $groupsAvailable = config('friendships.groups', []); + $groupsAvailable = config('friendships.groups', []); - if (!isset($groupsAvailable[$groupSlug]) || empty($friendship)) { - return false; + if (!isset($groupsAvailable[$group]) || empty($friendship)) { + return false; } $group = $friendship->groups()->firstOrCreate([ 'friendship_id' => $friendship->id, - 'group_id' => $groupsAvailable[$groupSlug], + 'group_slug' => $group, 'friend_id' => $friend->getKey(), 'friend_type' => $friend->getMorphClass(), ]); @@ -137,10 +137,10 @@ public function groupFriend(Model $friend, $groupSlug) /** * @param Model $friend - * @param $groupSlug + * @param string $group * @return bool */ - public function ungroupFriend(Model $friend, $groupSlug = '') + public function ungroupFriend(Model $friend, $group="") { $friendship = $this->findFriendship($friend)->first(); @@ -156,8 +156,8 @@ public function ungroupFriend(Model $friend, $groupSlug = '') 'friend_type' => $friend->getMorphClass(), ]; - if ('' !== $groupSlug && isset($groupsAvailable[$groupSlug])) { - $where['group_id'] = $groupsAvailable[$groupSlug]; + if ('' !== $group && isset($groupsAvailable[$group])) { + $where['group_slug'] = $group; } $result = $friendship->groups()->where($where)->delete(); @@ -213,34 +213,34 @@ public function getFriendship(Model $recipient) /** * @return \Illuminate\Database\Eloquent\Collection * - * @param string $groupSlug + * @param string $group * */ - public function getAllFriendships($groupSlug = '') + public function getAllFriendships($group = '') { - return $this->findFriendships(null, $groupSlug)->get(); + return $this->findFriendships(null, $group)->get(); } /** * @return \Illuminate\Database\Eloquent\Collection * - * @param string $groupSlug + * @param string $group * */ - public function getPendingFriendships($groupSlug = '') + public function getPendingFriendships($group = '') { - return $this->findFriendships(Status::PENDING, $groupSlug)->get(); + return $this->findFriendships(Status::PENDING, $group)->get(); } /** * @return \Illuminate\Database\Eloquent\Collection * - * @param string $groupSlug + * @param string $group * */ - public function getAcceptedFriendships($groupSlug = '') + public function getAcceptedFriendships($group = '') { - return $this->findFriendships(Status::ACCEPTED, $groupSlug)->get(); + return $this->findFriendships(Status::ACCEPTED, $group)->get(); } /** @@ -294,13 +294,13 @@ public function getFriendRequests() * It will return the 'friends' models. ex: App\User * * @param int $perPage Number - * @param string $groupSlug + * @param string $group * * @return \Illuminate\Database\Eloquent\Collection */ - public function getFriends($perPage = 0, $groupSlug = '') + public function getFriends($perPage = 0, $group = '') { - return $this->getOrPaginate($this->getFriendsQueryBuilder($groupSlug), $perPage); + return $this->getOrPaginate($this->getFriendsQueryBuilder($group), $perPage); } /** @@ -343,13 +343,13 @@ public function getFriendsOfFriends($perPage = 0) /** * Get the number of friends * - * @param string $groupSlug + * @param string $group * * @return integer */ - public function getFriendsCount($groupSlug = '') + public function getFriendsCount($group = '') { - $friendsCount = $this->findFriendships(Status::ACCEPTED, $groupSlug)->count(); + $friendsCount = $this->findFriendships(Status::ACCEPTED, $group)->count(); return $friendsCount; } @@ -413,11 +413,11 @@ private function findFriendship(Model $recipient) /** * @param $status - * @param string $groupSlug + * @param string $group * * @return \Illuminate\Database\Eloquent\Collection */ - private function findFriendships($status = null, $groupSlug = '') + private function findFriendships($status = null, $group = '') { $query = Friendship::where(function ($query) { @@ -426,7 +426,7 @@ private function findFriendships($status = null, $groupSlug = '') })->orWhere(function ($q) { $q->whereRecipient($this); }); - })->whereGroup($this, $groupSlug); + })->whereGroup($this, $group); //if $status is passed, add where clause if (!is_null($status)) { @@ -439,14 +439,14 @@ private function findFriendships($status = null, $groupSlug = '') /** * Get the query builder of the 'friend' model * - * @param string $groupSlug + * @param string $group * * @return \Illuminate\Database\Eloquent\Builder */ - private function getFriendsQueryBuilder($groupSlug = '') + private function getFriendsQueryBuilder($group = '') { - $friendships = $this->findFriendships(Status::ACCEPTED, $groupSlug)->get(['sender_id', 'recipient_id']); + $friendships = $this->findFriendships(Status::ACCEPTED, $group)->get(['sender_id', 'recipient_id']); $recipients = $friendships->pluck('recipient_id')->all(); $senders = $friendships->pluck('sender_id')->all(); @@ -481,11 +481,11 @@ private function getMutualFriendsQueryBuilder(Model $other) /** * Get the query builder for friendsOfFriends ('friend' model) * - * @param string $groupSlug + * @param string $group * * @return \Illuminate\Database\Eloquent\Builder */ - private function friendsOfFriendsQueryBuilder($groupSlug = '') + private function friendsOfFriendsQueryBuilder($group = '') { $friendships = $this->findFriendships(Status::ACCEPTED)->get(['sender_id', 'recipient_id']); $recipients = $friendships->pluck('recipient_id')->all(); @@ -502,7 +502,7 @@ private function friendsOfFriendsQueryBuilder($groupSlug = '') $q->whereIn('recipient_id', $friendIds); }); }) - ->whereGroup($this, $groupSlug) + ->whereGroup($this, $group) ->get(['sender_id', 'recipient_id']); $fofIds = array_unique( @@ -535,7 +535,12 @@ public function groups() { return $this->morphMany(FriendFriendshipGroups::class, 'friend'); } - + + /** + * @param $builder + * @param $perPage + * @return mixed + */ protected function getOrPaginate($builder, $perPage) { if ($perPage == 0) { @@ -543,4 +548,5 @@ protected function getOrPaginate($builder, $perPage) } return $builder->paginate($perPage); } + } diff --git a/src/config/friendships.php b/src/config/friendships.php index 19ff01f..6147cb3 100644 --- a/src/config/friendships.php +++ b/src/config/friendships.php @@ -8,9 +8,9 @@ ], 'groups' => [ - 'acquaintances' => 0, - 'close_friends' => 1, - 'family' => 2 + 'acquaintances' => 'Acquaintances', + 'close_friends' => 'Close Friends', + 'family' => 'Family' ] ]; \ No newline at end of file diff --git a/src/database/migrations/create_friendships_groups_table.php b/src/database/migrations/create_friendships_groups_table.php index 0f96440..e89193f 100644 --- a/src/database/migrations/create_friendships_groups_table.php +++ b/src/database/migrations/create_friendships_groups_table.php @@ -15,14 +15,14 @@ public function up() { $table->integer('friendship_id')->unsigned(); $table->morphs('friend'); - $table->integer('group_id')->unsigned(); + $table->string('group_slug'); $table->foreign('friendship_id') ->references('id') ->on(config('friendships.tables.fr_pivot')) ->onDelete('cascade'); - $table->unique(['friendship_id', 'friend_id', 'friend_type', 'group_id'], 'unique'); + $table->unique(['friendship_id', 'friend_id', 'friend_type', 'group_slug'], 'unique'); }); diff --git a/tests/FriendshipsGroupsTest.php b/tests/FriendshipsGroupsTest.php index 26658be..1189f8d 100644 --- a/tests/FriendshipsGroupsTest.php +++ b/tests/FriendshipsGroupsTest.php @@ -246,7 +246,6 @@ public function returns_groups_for_specific_friend() { $sender = createUser(); $recipients = createUser([], 2); - $groupsAvailable = config('friendships.groups', []); foreach ($recipients as $recipient) { $sender->befriend($recipient); @@ -267,7 +266,6 @@ public function returns_groups_for_specific_friend() { $this->assertCount(0, $recipients[1]->getGroupsFor($sender)); $this->assertInternalType('array', $rec1); - $this->assertContains($rec1[0], $groupsAvailable); } diff --git a/tests/config/friendships.php b/tests/config/friendships.php index 19ff01f..6147cb3 100644 --- a/tests/config/friendships.php +++ b/tests/config/friendships.php @@ -8,9 +8,9 @@ ], 'groups' => [ - 'acquaintances' => 0, - 'close_friends' => 1, - 'family' => 2 + 'acquaintances' => 'Acquaintances', + 'close_friends' => 'Close Friends', + 'family' => 'Family' ] ]; \ No newline at end of file