Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add access to query builder with requests() and friendships() method in friendable trait #16

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,32 +102,50 @@ $user->getFriendship($recipient);

#### Get a list of all Friendships
```php
// (return a collection of friendships)
$user->getAllFriendships();
// or with the new method (return query builder):
$user->requests();
```

#### Get a list of pending Friendships
```php
// (return a collection of friendships)
$user->getPendingFriendships();
// or with the new method (return query builder):
$user->requests()->pending();
```

#### Get a list of accepted Friendships
```php
// (return a collection of friendships)
$user->getAcceptedFriendships();
// or with the new method (return query builder):
$user->requests()->accepted();
```

#### Get a list of denied Friendships
```php
// (return a collection of friendships)
$user->getDeniedFriendships();
// or with the new method (return query builder):
$user->requests()->denied();
```

#### Get a list of blocked Friendships
```php
// (return a collection of friendships)
$user->getBlockedFriendships();
// or with the new method (return query builder):
$user->requests()->blocked();
```

#### Get a list of pending Friend Requests
```php
// (return a collection of friendships)
$user->getFriendRequests();
// or with the new method (return query builder):
$user->requests(Direction::INCOMING)->pending();
```

#### Get the number of Friends
Expand All @@ -140,6 +158,8 @@ $user->getFriendsCount();
#### Get Friends
```php
$user->getFriends();
// or with the new method (return query builder):
$user->friendships();
```

#### Get Friends Paginated
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hootlex/laravel-friendships",
"description": "This package gives Eloqent models the ability to manage their friendships.",
"description": "This package gives Eloquent models the ability to manage their friendships.",
"keywords": ["laravel", "friendships", "friend-system", "friends", "eloqent"],
"license": "MIT",
"authors": [
Expand Down
13 changes: 13 additions & 0 deletions src/Direction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Hootlex\Friendships;

/**
* Class Direction.
*/
class Direction
{
const INCOMING = 1;
const OUTGOING = 2;
const ALL = 3;
}
45 changes: 45 additions & 0 deletions src/Models/Friendship.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
use Hootlex\Friendships\Status;
use Illuminate\Database\Eloquent\Model;

/**
* Class Friendship
* @package Hootlex\Friendships\Models
*
* @method \Illuminate\Database\Eloquent\Builder|Friendship pending() Get pending friendships
* @method \Illuminate\Database\Eloquent\Builder|Friendship accepted() Get accepted friendships
* @method \Illuminate\Database\Eloquent\Builder|Friendship blocked() Get blocked friendships
* @method \Illuminate\Database\Eloquent\Builder|Friendship denied() Get denied friendships
*/
class Friendship extends Model
{
/**
Expand Down Expand Up @@ -67,6 +76,42 @@ public function scopeWhereSender($query, $model)
->where('sender_type', $model->getMorphClass());
}

/**
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder|Friendship
*/
public function scopePending($query)
{
return $query->whereStatus(Status::PENDING);
}

/**
* @param \Illuminate\Database\Eloquent\Builder $query
* @return Friendship
*/
public function scopeAccepted($query)
{
return $query->whereStatus(Status::ACCEPTED);
}

/**
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder|Friendship
*/
public function scopeBlocked($query)
{
return $query->whereStatus(Status::BLOCKED);
}

/**
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder|Friendship
*/
public function scopeDenied($query)
{
return $query->whereStatus(Status::DENIED);
}

/**
* @param $query
* @param Model $sender
Expand Down
97 changes: 64 additions & 33 deletions src/Traits/Friendable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Hootlex\Friendships\Traits;

use Hootlex\Friendships\Direction;
use Hootlex\Friendships\Models\Friendship;
use Hootlex\Friendships\Status;
use Illuminate\Database\Eloquent\Model;
Expand All @@ -23,7 +24,7 @@ public function befriend(Model $recipient)
'status' => Status::PENDING,
]);

$this->friends()->save($friendship);
$this->friendshipRequests()->save($friendship);

return $friendship;

Expand Down Expand Up @@ -98,7 +99,7 @@ public function blockFriend(Model $recipient)
'status' => Status::BLOCKED,
]);

return $this->friends()->save($friendship);
return $this->friendshipRequests()->save($friendship);
}

/**
Expand Down Expand Up @@ -126,7 +127,7 @@ public function getFriendship(Model $recipient)
*/
public function getAllFriendships()
{
return $this->findFriendships()->get();
return $this->findRequests()->get();
}

/**
Expand All @@ -135,15 +136,15 @@ public function getAllFriendships()
*/
public function getPendingFriendships()
{
return $this->findFriendships(Status::PENDING)->get();
return $this->findRequests(Status::PENDING)->get();
}

/**
* @return \Illuminate\Database\Eloquent\Collection
*/
public function getAcceptedFriendships()
{
return $this->findFriendships(Status::ACCEPTED)->get();
return $this->findRequests(Status::ACCEPTED)->get();
}

/**
Expand All @@ -152,7 +153,7 @@ public function getAcceptedFriendships()
*/
public function getDeniedFriendships()
{
return $this->findFriendships(Status::DENIED)->get();
return $this->findRequests(Status::DENIED)->get();
}

/**
Expand All @@ -161,7 +162,7 @@ public function getDeniedFriendships()
*/
public function getBlockedFriendships()
{
return $this->findFriendships(Status::BLOCKED)->get();
return $this->findRequests(Status::BLOCKED)->get();
}

/**
Expand All @@ -171,7 +172,7 @@ public function getBlockedFriendships()
*/
public function hasBlocked(Model $recipient)
{
return $this->friends()->whereRecipient($recipient)->whereStatus(Status::BLOCKED)->exists();
return $this->friendshipRequests()->whereRecipient($recipient)->whereStatus(Status::BLOCKED)->exists();
}

/**
Expand Down Expand Up @@ -203,9 +204,9 @@ public function getFriendRequests()
public function getFriends($perPage = 0)
{
if ($perPage == 0) {
return $this->getFriendsQueryBuilder()->get();
return $this->friendships()->get();
} else {
return $this->getFriendsQueryBuilder()->paginate($perPage);
return $this->friendships()->paginate($perPage);
}
}

Expand Down Expand Up @@ -234,7 +235,7 @@ public function getFriendsOfFriends($perPage = 0)
*/
public function getFriendsCount()
{
$friendsCount = $this->findFriendships(Status::ACCEPTED)->count();
$friendsCount = $this->findRequests(Status::ACCEPTED)->count();
return $friendsCount;
}

Expand Down Expand Up @@ -272,19 +273,26 @@ private function findFriendship(Model $recipient)
}

/**
* @param $status
* @param int $status
* @param int $direction
*
* @return \Illuminate\Database\Eloquent\Collection
*/
private function findFriendships($status = null)
private function findRequests($status = null, $direction = Direction::ALL)
{
$query = Friendship::where(function ($query) {
$query = Friendship::where(function ($query) use ($direction) {
if (($direction & Direction::OUTGOING) == Direction::OUTGOING) {
$query->where(function ($q) {
$q->whereSender($this);
})->orWhere(function ($q) {
});
}

if (($direction & Direction::INCOMING) == Direction::INCOMING) {
$query->orWhere(function ($q) {
$q->whereRecipient($this);
});
});
}
});

//if $status is passed, add where clause
if(!is_null($status)){
Expand All @@ -294,29 +302,14 @@ private function findFriendships($status = null)
return $query;
}


/**
* Get the query builder of the 'friend' model
*
* @return \Illuminate\Database\Eloquent\Builder
*/
private function getFriendsQueryBuilder()
{
$friendships = $this->findFriendships(Status::ACCEPTED)->get(['sender_id', 'recipient_id']);
$recipients = $friendships->lists('recipient_id')->all();
$senders = $friendships->lists('sender_id')->all();

return $this->where('id', '!=', $this->getKey())->whereIn('id', array_merge($recipients, $senders));
}

/**
* Get the query builder for friendsOfFriends ('friend' model)
*
* @return \Illuminate\Database\Eloquent\Builder
*/
private function friendsOfFriendsQueryBuilder()
{
$friendships = $this->findFriendships(Status::ACCEPTED)->get(['sender_id', 'recipient_id']);
$friendships = $this->findRequests(Status::ACCEPTED)->get(['sender_id', 'recipient_id']);
$recipients = $friendships->lists('recipient_id')->all();
$senders = $friendships->lists('sender_id')->all();

Expand Down Expand Up @@ -350,9 +343,47 @@ private function friendsOfFriendsQueryBuilder()
/**
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function friends()
protected function friendshipRequests()
{
return $this->morphMany(Friendship::class, 'sender');
}

/**
* Get friendships of this user.
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function friendships()
{
$friendships = $this->findRequests(Status::ACCEPTED)->get(['sender_id', 'recipient_id']);
$recipients = $friendships->lists('recipient_id')->all();
$senders = $friendships->lists('sender_id')->all();

return $this->where('id', '!=', $this->getKey())->whereIn('id', array_merge($recipients, $senders));
}

/**
* Get requests of this user.
*
* @param int $direction
*
* @return Friendship|\Illuminate\Database\Eloquent\Builder
*/
public function requests($direction = Direction::ALL)
{
$friendships = $this->findRequests(null, $direction);

return $friendships;
}

/**
* @deprecated Will be removed in version 2
*
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function friends()
{
@trigger_error(sprintf('The '.__METHOD__.' method was deprecated in version 1.1 and will be removed in version 2.0. You should implement this method yourself in %s.', get_class($this)), E_USER_DEPRECATED);
return $this->morphMany(Friendship::class, 'sender');
}
}
Loading