Skip to content

Commit

Permalink
Update groups config structure
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolaynesov committed Oct 12, 2016
1 parent e98bb42 commit 86f725c
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 55 deletions.
3 changes: 1 addition & 2 deletions src/Models/FriendFriendshipGroups.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Hootlex\Friendships\Models;

use Hootlex\Friendships\Status;
use Illuminate\Database\Eloquent\Model;

/**
Expand All @@ -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
Expand Down
13 changes: 5 additions & 8 deletions src/Models/Friendship.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Hootlex\Friendships\Models;

use Hootlex\Friendships\Status;
use Illuminate\Database\Eloquent\Model;

/**
Expand Down Expand Up @@ -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());
Expand Down
76 changes: 41 additions & 35 deletions src/Traits/Friendable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
]);
Expand All @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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)) {
Expand All @@ -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();

Expand Down Expand Up @@ -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();
Expand 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(
Expand Down Expand Up @@ -535,12 +535,18 @@ public function groups()
{
return $this->morphMany(FriendFriendshipGroups::class, 'friend');
}


/**
* @param $builder
* @param $perPage
* @return mixed
*/
protected function getOrPaginate($builder, $perPage)
{
if ($perPage == 0) {
return $builder->get();
}
return $builder->paginate($perPage);
}

}
6 changes: 3 additions & 3 deletions src/config/friendships.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
],

'groups' => [
'acquaintances' => 0,
'close_friends' => 1,
'family' => 2
'acquaintances' => 'Acquaintances',
'close_friends' => 'Close Friends',
'family' => 'Family'
]

];
4 changes: 2 additions & 2 deletions src/database/migrations/create_friendships_groups_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

});

Expand Down
2 changes: 0 additions & 2 deletions tests/FriendshipsGroupsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);

}

Expand Down
6 changes: 3 additions & 3 deletions tests/config/friendships.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
],

'groups' => [
'acquaintances' => 0,
'close_friends' => 1,
'family' => 2
'acquaintances' => 'Acquaintances',
'close_friends' => 'Close Friends',
'family' => 'Family'
]

];

0 comments on commit 86f725c

Please sign in to comment.