Skip to content

Commit f981e9e

Browse files
authored
Publishing to users (#57)
1 parent 23af65e commit f981e9e

File tree

3 files changed

+147
-29
lines changed

3 files changed

+147
-29
lines changed

README.md

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,21 @@ Also please note that prior to version 2.0, this package integrated with Pusher'
1717

1818
## Contents
1919

20-
- [Installation](#installation)
21-
- [Setting up your Pusher account](#setting-up-your-pusher-account)
22-
- [Usage](#usage)
23-
- [Available Message methods](#available-message-methods)
24-
- [Changelog](#changelog)
25-
- [Testing](#testing)
26-
- [Security](#security)
27-
- [Contributing](#contributing)
28-
- [Credits](#credits)
29-
- [License](#license)
20+
- [Pusher Beams push notifications channel for Laravel 5.5+, 6.x, 7.x & 8.x](#pusher-beams-push-notifications-channel-for-laravel-55-6x-7x--8x)
21+
- [Contents](#contents)
22+
- [Installation](#installation)
23+
- [Setting up your Pusher account](#setting-up-your-pusher-account)
24+
- [Usage](#usage)
25+
- [Available Message methods](#available-message-methods)
26+
- [Sending to multiple platforms](#sending-to-multiple-platforms)
27+
- [Routing a message](#routing-a-message)
28+
- [Publish to users](#publish-to-users)
29+
- [Changelog](#changelog)
30+
- [Testing](#testing)
31+
- [Security](#security)
32+
- [Contributing](#contributing)
33+
- [Credits](#credits)
34+
- [License](#license)
3035

3136

3237
## Installation
@@ -124,7 +129,7 @@ public function toPushNotification($notifiable)
124129
125130
### Routing a message
126131

127-
By default, the pusher "interest" messages will be sent to will be defined using the {notifiable}.{id} convention, for example `App.User.1`,
132+
By default, the pusher "interest" messages will be sent to will be defined using the {notifiable}.{id} convention, for example `App.User.1`,
128133
however you can change this behaviour by including a `routeNotificationFor()` in the notifiable class.
129134

130135
I.e. if you are pushing notification on ``User`` model, you can go to `App\User` class and implement method:
@@ -141,10 +146,21 @@ public function routeNotificationFor($channel)
141146
return $class.'.'.$this->getKey();
142147
}
143148
```
144-
145-
146-
PusherPushNotifications()` in the notifiable class method that
147-
returns the interest name.
149+
150+
`PusherPushNotifications()` in the notifiable class method returns the interest name.
151+
152+
### Publish to users
153+
154+
You can publish to users in the same way that you publish to interests but you must add the following variable to the notifiable model:
155+
156+
```
157+
class Client extends Model
158+
{
159+
use Notifiable;
160+
161+
public $pushNotificationType = 'users';
162+
}
163+
```
148164

149165
## Changelog
150166

src/PusherChannel.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,17 @@
66
use Illuminate\Notifications\Events\NotificationFailed;
77
use Illuminate\Notifications\Notification;
88
use Illuminate\Support\Arr;
9+
use Illuminate\Support\Str;
910
use Pusher\PushNotifications\PushNotifications;
1011
use Throwable;
1112

1213
class PusherChannel
1314
{
15+
/**
16+
* @var string
17+
*/
18+
const INTERESTS = 'interests';
19+
1420
/**
1521
* @var PushNotifications
1622
*/
@@ -41,12 +47,16 @@ public function __construct(PushNotifications $beamsClient, Dispatcher $events)
4147
*/
4248
public function send($notifiable, Notification $notification)
4349
{
44-
$interest = $notifiable->routeNotificationFor('PusherPushNotifications')
45-
?: $this->interestName($notifiable);
50+
$type = $notifiable->pushNotificationType ?? self::INTERESTS;
51+
52+
$data = $notifiable->routeNotificationFor('PusherPushNotifications')
53+
?: $this->defaultName($notifiable);
4654

4755
try {
48-
$this->beamsClient->publishToInterests(
49-
Arr::wrap($interest),
56+
$notificationType = sprintf('publishTo%s', Str::ucfirst($type));
57+
58+
$this->beamsClient->{$notificationType}(
59+
Arr::wrap($data),
5060
$notification->toPushNotification($notifiable)->toArray()
5161
);
5262
} catch (Throwable $exception) {
@@ -57,13 +67,13 @@ public function send($notifiable, Notification $notification)
5767
}
5868

5969
/**
60-
* Get the interest name for the notifiable.
70+
* Get the default name for the notifiable.
6171
*
6272
* @param $notifiable
6373
*
6474
* @return string
6575
*/
66-
protected function interestName($notifiable)
76+
protected function defaultName($notifiable)
6777
{
6878
$class = str_replace('\\', '.', get_class($notifiable));
6979

tests/ChannelTest.php

Lines changed: 100 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,37 +25,95 @@ public function setUp(): void
2525

2626
$this->notification = new TestNotification;
2727

28-
$this->notifiable = new TestNotifiable;
28+
$this->notifiableInterest = new TestNotifiableInterest;
29+
$this->notifiableInterests = new TestNotifiableInterests;
30+
31+
$this->notifiableUser = new TestNotifiableUser;
32+
$this->notifiableUsers = new TestNotifiableUsers;
2933
}
3034

3135
/** @test */
32-
public function it_can_send_a_notification()
36+
public function it_can_send_a_notification_to_interest()
3337
{
34-
$message = $this->notification->toPushNotification($this->notifiable);
38+
$message = $this->notification->toPushNotification($this->notifiableInterest);
3539

3640
$data = $message->toArray();
3741

3842
$this->pusher->shouldReceive('publishToInterests')->once()->with(['interest_name'], $data);
3943

40-
$this->channel->send($this->notifiable, $this->notification);
44+
$this->channel->send($this->notifiableInterest, $this->notification);
45+
}
46+
47+
/** @test */
48+
public function it_can_send_a_notification_to_interests()
49+
{
50+
$message = $this->notification->toPushNotification($this->notifiableInterests);
51+
52+
$data = $message->toArray();
53+
54+
$this->pusher->shouldReceive('publishToInterests')->once()->with([
55+
'interest_one', 'interest_two', 'interest_three',
56+
], $data);
57+
58+
$this->channel->send($this->notifiableInterests, $this->notification);
4159
}
4260

4361
/** @test */
44-
public function it_fires_failure_event_on_failure()
62+
public function it_fires_failure_event_on_interest_failure()
4563
{
46-
$message = $this->notification->toPushNotification($this->notifiable);
64+
$message = $this->notification->toPushNotification($this->notifiableInterest);
4765

4866
$data = $message->toArray();
4967

5068
$this->pusher->shouldReceive('publishToInterests')->once()->with(['interest_name'], $data)->andThrow(new Exception('Something happened'));
5169

5270
$this->events->shouldReceive('dispatch')->once()->with(Mockery::type(NotificationFailed::class));
5371

54-
$this->channel->send($this->notifiable, $this->notification);
72+
$this->channel->send($this->notifiableInterest, $this->notification);
73+
}
74+
75+
/** @test */
76+
public function it_can_send_a_notification_to_user()
77+
{
78+
$message = $this->notification->toPushNotification($this->notifiableUser);
79+
80+
$data = $message->toArray();
81+
82+
$this->pusher->shouldReceive('publishToUsers')->once()->with(['user_1'], $data);
83+
84+
$this->channel->send($this->notifiableUser, $this->notification);
85+
}
86+
87+
/** @test */
88+
public function it_can_send_a_notification_to_users()
89+
{
90+
$message = $this->notification->toPushNotification($this->notifiableUsers);
91+
92+
$data = $message->toArray();
93+
94+
$this->pusher->shouldReceive('publishToUsers')->once()->with([
95+
'user_1', 'user_2', 'user_3',
96+
], $data);
97+
98+
$this->channel->send($this->notifiableUsers, $this->notification);
99+
}
100+
101+
/** @test */
102+
public function it_fires_failure_event_on_user_failure()
103+
{
104+
$message = $this->notification->toPushNotification($this->notifiableUser);
105+
106+
$data = $message->toArray();
107+
108+
$this->pusher->shouldReceive('publishToUsers')->once()->with(['user_1'], $data)->andThrow(new Exception('Something happened'));
109+
110+
$this->events->shouldReceive('dispatch')->once()->with(Mockery::type(NotificationFailed::class));
111+
112+
$this->channel->send($this->notifiableUser, $this->notification);
55113
}
56114
}
57115

58-
class TestNotifiable
116+
class TestNotifiableInterest
59117
{
60118
use Notifiable;
61119

@@ -65,6 +123,40 @@ public function routeNotificationForPusherPushNotifications()
65123
}
66124
}
67125

126+
class TestNotifiableInterests
127+
{
128+
use Notifiable;
129+
130+
public function routeNotificationForPusherPushNotifications()
131+
{
132+
return ['interest_one', 'interest_two', 'interest_three'];
133+
}
134+
}
135+
136+
class TestNotifiableUser
137+
{
138+
use Notifiable;
139+
140+
public $pushNotificationType = 'users';
141+
142+
public function routeNotificationForPusherPushNotifications()
143+
{
144+
return 'user_1';
145+
}
146+
}
147+
148+
class TestNotifiableUsers
149+
{
150+
use Notifiable;
151+
152+
public $pushNotificationType = 'users';
153+
154+
public function routeNotificationForPusherPushNotifications()
155+
{
156+
return ['user_1', 'user_2', 'user_3'];
157+
}
158+
}
159+
68160
class TestNotification extends Notification
69161
{
70162
public function toPushNotification($notifiable)

0 commit comments

Comments
 (0)