Skip to content

Commit

Permalink
Merge branch 'Add-Events'
Browse files Browse the repository at this point in the history
  • Loading branch information
skybluesofa committed Mar 8, 2020
2 parents d8f7201 + 674b640 commit f800f44
Show file tree
Hide file tree
Showing 13 changed files with 614 additions and 19 deletions.
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

Gives Eloquent models the ability to manage their followers.

##Models can:
## Models can:
- Send Follow Requests
- Accept Follow Requests
- Deny Follow Requests
Expand Down Expand Up @@ -54,32 +54,46 @@ class User extends Model
## How to use
[Check the Test file to see the package in action](https://github.com/skybluesofa/laravel-followers/blob/master/tests/FollowersTest.php)

### Methods

#### Send a Follow Request

Will trigger a `Skybluesofa\LaravelFollowers\Events\FollowRequest` event.
```php
$user->follow($recipient);
```

#### Accept a Follow Request

Will trigger a `Skybluesofa\LaravelFollowers\Events\FollowRequestAccepted` event.
```php
$recipient->acceptFollowRequestFrom($user);
```

#### Deny a Follow Request

Will trigger a `Skybluesofa\LaravelFollowers\Events\FollowRequestDenied` event.
```php
$recipient->denyFollowRequestFrom($user);
```

#### Remove Follow

Will trigger a `Skybluesofa\LaravelFollowers\Events\Unfollow` event.
```php
$user->unfollow($recipient);
```

#### Block a User

Will trigger a `Skybluesofa\LaravelFollowers\Events\FollowingBlocked` event.
```php
$user->blockBeingFollowedBy($recipient);
```

#### Unblock a User

Will trigger a `Skybluesofa\LaravelFollowers\Events\FollowingUnblocked` event.
```php
$user->unblockBeingFollowedBy($recipient);
```
Expand Down Expand Up @@ -166,6 +180,21 @@ $user->getFriends();
$user->getFriends($perPage = 20);
```

### Events

These events are triggered during the lifecycle of following/unfollowing/accept/deny followers:

```php
Skybluesofa\LaravelFollowers\Events\FollowingBlocked(Model $recipient, Model $sender);
Skybluesofa\LaravelFollowers\Events\FollowingUnblocked(Model $recipient, Model $sender);
Skybluesofa\LaravelFollowers\Events\FollowRequest(Model $recipient, Model $sender);
Skybluesofa\LaravelFollowers\Events\FollowRequestAccepted(Model $recipient, Model $sender);
Skybluesofa\LaravelFollowers\Events\FollowRequestDenied(Model $recipient, Model $sender);
Skybluesofa\LaravelFollowers\Events\Unfollow(Model $recipient, Model $sender);
```

To listen for and react to these events, follow the [instructions available in the Laravel Documentation](https://laravel.com/docs/7.x/events#defining-listeners).

## Thank you
The basis of this code was garnered from [https://github.com/hootlex/laravel-friendships](https://github.com/hootlex/laravel-friendships). Although it was a jumping off point, much of the code has been rewritten to allow for Following as opposed to Mutual Friendship.

Expand Down
13 changes: 5 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,6 @@
"email": "[email protected]"
}
],
"autoload-dev": {
"classmap": [
"tests/TestCase.php"
],
"files": [
"tests/helpers.php"
]
},
"require": {
"php": ">=7.1.0"
},
Expand All @@ -30,5 +22,10 @@
"Skybluesofa\\Followers\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Skybluesofa\\Followers\\Tests\\": "tests/"
}
},
"minimum-stability": "stable"
}
26 changes: 26 additions & 0 deletions src/Events/FollowRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
namespace Skybluesofa\Followers\Events;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Queue\SerializesModels;

class FollowRequest
{
use SerializesModels;

public $recipient;
public $sender;

/**
* Create a new event instance.
*
* @param Model $sender
* @param Model $recipient
* @return void
*/
public function __construct(Model $recipient, Model $sender)
{
$this->recipient = $recipient;
$this->sender = $sender;
}
}
26 changes: 26 additions & 0 deletions src/Events/FollowRequestAccepted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
namespace Skybluesofa\Followers\Events;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Queue\SerializesModels;

class FollowRequestAccepted
{
use SerializesModels;

public $recipient;
public $sender;

/**
* Create a new event instance.
*
* @param Model $sender
* @param Model $recipient
* @return void
*/
public function __construct(Model $recipient, Model $sender)
{
$this->recipient = $recipient;
$this->sender = $sender;
}
}
26 changes: 26 additions & 0 deletions src/Events/FollowRequestDenied.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
namespace Skybluesofa\Followers\Events;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Queue\SerializesModels;

class FollowRequestDenied
{
use SerializesModels;

public $recipient;
public $sender;

/**
* Create a new event instance.
*
* @param Model $sender
* @param Model $recipient
* @return void
*/
public function __construct(Model $recipient, Model $sender)
{
$this->recipient = $recipient;
$this->sender = $sender;
}
}
26 changes: 26 additions & 0 deletions src/Events/FollowingBlocked.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
namespace Skybluesofa\Followers\Events;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Queue\SerializesModels;

class FollowingBlocked
{
use SerializesModels;

public $recipient;
public $sender;

/**
* Create a new event instance.
*
* @param Model $sender
* @param Model $recipient
* @return void
*/
public function __construct(Model $recipient, Model $sender)
{
$this->recipient = $recipient;
$this->sender = $sender;
}
}
26 changes: 26 additions & 0 deletions src/Events/FollowingUnblocked.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
namespace Skybluesofa\Followers\Events;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Queue\SerializesModels;

class FollowingUnblocked
{
use SerializesModels;

public $recipient;
public $sender;

/**
* Create a new event instance.
*
* @param Model $sender
* @param Model $recipient
* @return void
*/
public function __construct(Model $recipient, Model $sender)
{
$this->recipient = $recipient;
$this->sender = $sender;
}
}
26 changes: 26 additions & 0 deletions src/Events/Unfollow.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
namespace Skybluesofa\Followers\Events;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Queue\SerializesModels;

class Unfollow
{
use SerializesModels;

public $recipient;
public $sender;

/**
* Create a new event instance.
*
* @param Model $sender
* @param Model $recipient
* @return void
*/
public function __construct(Model $recipient, Model $sender)
{
$this->recipient = $recipient;
$this->sender = $sender;
}
}
12 changes: 12 additions & 0 deletions src/Traits/CanBeFollowed.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
use Skybluesofa\Followers\Models\Follower;
use Skybluesofa\Followers\Status;
use Illuminate\Database\Eloquent\Model;
use Skybluesofa\Followers\Events\FollowingBlocked;
use Skybluesofa\Followers\Events\FollowingUnblocked;
use Skybluesofa\Followers\Events\FollowRequestDenied;
use Skybluesofa\Followers\Events\FollowRequestAccepted;

/**
* Class Followable
Expand Down Expand Up @@ -67,6 +71,8 @@ public function hasFollowRequestFrom(Model $sender)
*/
public function acceptFollowRequestFrom(Model $sender)
{
event(new FollowRequestAccepted($this, $sender));

return $sender->whenFollowing($this)->update([
'status' => Status::ACCEPTED,
]);
Expand All @@ -79,6 +85,8 @@ public function acceptFollowRequestFrom(Model $sender)
*/
public function denyFollowRequestFrom(Model $sender)
{
event(new FollowRequestDenied($this, $sender));

return $sender->whenFollowing($this)->update([
'status' => Status::DENIED,
]);
Expand Down Expand Up @@ -115,6 +123,8 @@ public function blockBeingFollowedBy(Model $sender)
$this->whenFollowedBy($sender)->delete();
}

event(new FollowingBlocked($this, $sender));

return (new Follower)->fillSender($sender)->fillRecipient($this)->fill([
'status' => Status::BLOCKED,
])->save();
Expand All @@ -127,6 +137,8 @@ public function blockBeingFollowedBy(Model $sender)
*/
public function unblockBeingFollowedBy(Model $sender)
{
event(new FollowingUnblocked($this, $sender));

return $this->whenFollowedBy($sender)->delete();
}

Expand Down
6 changes: 6 additions & 0 deletions src/Traits/CanFollow.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
use Skybluesofa\Followers\Models\Follower;
use Skybluesofa\Followers\Status;
use Illuminate\Database\Eloquent\Model;
use Skybluesofa\Followers\Events\FollowRequest;
use Skybluesofa\Followers\Events\Unfollow;

/**
* Class Followable
Expand Down Expand Up @@ -34,6 +36,8 @@ public function follow(Model $recipient)
return false;
}

event(new FollowRequest($recipient, $this));

$following = (new Follower)->fillRecipient($recipient)->fill([
'status' => Status::PENDING,
]);
Expand Down Expand Up @@ -65,6 +69,8 @@ private function followingCountReached()
*/
public function unfollow(Model $recipient)
{
event(new Unfollow($recipient, $this));

return $this->whenFollowing($recipient)->delete();
}

Expand Down
4 changes: 3 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php
namespace Skybluesofa\Followers\Tests;

use Illuminate\Database\Eloquent\Factory;
use Orchestra\Testbench\TestCase as BaseTestCase;

abstract class TestCase extends Orchestra\Testbench\TestCase
abstract class TestCase extends BaseTestCase
{

/**
Expand Down
Loading

0 comments on commit f800f44

Please sign in to comment.