Skip to content

Commit fdec78e

Browse files
timacdonaldLukas Kämmerling
authored and
Lukas Kämmerling
committed
Add payload factory (#50)
* add payload factory * add doc block * add new tags logic to factory
1 parent 7411350 commit fdec78e

File tree

2 files changed

+62
-13
lines changed

2 files changed

+62
-13
lines changed

src/OneSignalChannel.php

+15-13
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,27 @@ public function send($notifiable, Notification $notification)
3232
return;
3333
}
3434

35-
$payload = $notification->toOneSignal($notifiable)->toArray();
36-
37-
if (is_array($userIds)) {
38-
if (array_key_exists('email', $userIds)) {
39-
$payload['filters'] = collect([['field' => 'email', 'value' => $userIds['email']]]);
40-
} elseif (array_key_exists('tags', $userIds)) {
41-
$payload['tags'] = collect([$userIds['tags']]);
42-
}
43-
} else {
44-
$payload['include_player_ids'] = collect($userIds);
45-
}
46-
4735
/** @var ResponseInterface $response */
48-
$response = $this->oneSignal->sendNotificationCustom($payload);
36+
$response = $this->oneSignal->sendNotificationCustom(
37+
$this->payload($notifiable, $notification, $userIds)
38+
);
4939

5040
if ($response->getStatusCode() !== 200) {
5141
throw CouldNotSendNotification::serviceRespondedWithAnError($response);
5242
}
5343

5444
return $response;
5545
}
46+
47+
/**
48+
* @param mixed $notifiable
49+
* @param \Illuminate\Notifications\Notification $notification
50+
* @param mixed $targeting
51+
*
52+
* @return array
53+
*/
54+
protected function payload($notifiable, $notification, $userIds)
55+
{
56+
return OneSignalPayloadFactory::make($notifiable, $notification, $userIds);
57+
}
5658
}

src/OneSignalPayloadFactory.php

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace NotificationChannels\OneSignal;
4+
5+
use Illuminate\Notifications\Notification;
6+
7+
class OneSignalPayloadFactory
8+
{
9+
/**
10+
* Make a one signal notification payload.
11+
*
12+
* @param mixed $notifiable
13+
* @param \Illuminate\Notifications\Notification $notification
14+
* @param mixed $targeting
15+
*
16+
* @return array
17+
*/
18+
public static function make($notifiable, Notification $notification, $targeting) : array
19+
{
20+
$payload = $notification->toOneSignal($notifiable)->toArray();
21+
22+
if (static::isTargetingEmail($targeting)) {
23+
$payload['filters'] = collect([['field' => 'email', 'value' => $targeting['email']]]);
24+
} elseif (static::isTargetingTags($targeting)) {
25+
$payload['tags'] = collect([$targeting['tags']]);
26+
} else {
27+
$payload['include_player_ids'] = collect($targeting);
28+
}
29+
30+
return $payload;
31+
}
32+
33+
/**
34+
* @param mixed $targeting
35+
*
36+
* @return bool
37+
*/
38+
protected static function isTargetingEmail($targeting)
39+
{
40+
return is_array($targeting) && array_key_exists('email', $targeting);
41+
}
42+
43+
protected static function isTargetingTags($targeting)
44+
{
45+
return is_array($targeting) && array_key_exists('tags', $targeting);
46+
}
47+
}

0 commit comments

Comments
 (0)