Skip to content

Commit 84d6e05

Browse files
author
Lukas Kämmerling
authored
Refactor to Traits and add some little more Parameters (#58)
This PR aims to provide a cleaner Codebase and make it possible to natively set more Parameters on the Notification Creation. Since this PR isn't ready yet, it should be a little bit a discussion between all users. Feel free to checkout the branch and make Changes too or just test it :) Actually, i think this could be a complete rewrite known as 2.0 or what do you think? CC @timacdonald @Lloople // StyleCi will be fixed after this PR is feature complete
1 parent 0477a89 commit 84d6e05

19 files changed

+820
-218
lines changed

.styleci.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
11
preset: laravel
2-
3-
linting: true

src/OneSignalButton.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function text($value)
6565
public function toArray()
6666
{
6767
return [
68-
'id' => $this->id,
68+
'id' => $this->id,
6969
'text' => $this->text,
7070
'icon' => $this->icon,
7171
];

src/OneSignalChannel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function __construct(OneSignalClient $oneSignal)
2828
*/
2929
public function send($notifiable, Notification $notification)
3030
{
31-
if (! $userIds = $notifiable->routeNotificationFor('OneSignal')) {
31+
if (! $userIds = $notifiable->routeNotificationFor('OneSignal', $notification)) {
3232
return;
3333
}
3434

src/OneSignalMessage.php

Lines changed: 38 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,20 @@
33
namespace NotificationChannels\OneSignal;
44

55
use Illuminate\Support\Arr;
6+
use NotificationChannels\OneSignal\Traits\Deprecated;
7+
use NotificationChannels\OneSignal\Traits\Categories\ButtonHelpers;
8+
use NotificationChannels\OneSignal\Traits\Categories\SilentHelpers;
9+
use NotificationChannels\OneSignal\Traits\Categories\DeliveryHelpers;
10+
use NotificationChannels\OneSignal\Traits\Categories\GroupingHelpers;
11+
use NotificationChannels\OneSignal\Traits\Categories\AppearanceHelpers;
12+
use NotificationChannels\OneSignal\Traits\Categories\AttachmentHelpers;
613

714
class OneSignalMessage
815
{
9-
/** @var string */
10-
protected $body;
11-
12-
/** @var string */
13-
protected $subject;
14-
15-
/** @var string */
16-
protected $url;
17-
18-
/** @var string */
19-
protected $icon;
20-
21-
/** @var array */
22-
protected $data = [];
16+
use AppearanceHelpers, AttachmentHelpers, ButtonHelpers, DeliveryHelpers, GroupingHelpers, SilentHelpers, Deprecated;
2317

2418
/** @var array */
25-
protected $buttons = [];
26-
27-
/** @var array */
28-
protected $webButtons = [];
29-
30-
/** @var array */
31-
protected $extraParameters = [];
19+
protected $payload = [];
3220

3321
/**
3422
* @param string $body
@@ -45,212 +33,103 @@ public static function create($body = '')
4533
*/
4634
public function __construct($body = '')
4735
{
48-
$this->body = $body;
36+
$this->setBody($body);
4937
}
5038

5139
/**
5240
* Set the message body.
5341
*
54-
* @param string $value
55-
*
56-
* @return $this
57-
*/
58-
public function body($value)
59-
{
60-
$this->body = $value;
61-
62-
return $this;
63-
}
64-
65-
/**
66-
* Set the message icon.
67-
*
68-
* @param string $value
42+
* @param mixed $value
6943
*
7044
* @return $this
7145
*/
72-
public function icon($value)
46+
public function setBody($value)
7347
{
74-
$this->icon = $value;
75-
76-
return $this;
48+
return $this->setParameter('contents', $this->parseValueToArray($value));
7749
}
7850

7951
/**
8052
* Set the message subject.
8153
*
82-
* @param string $value
54+
* @param mixed $value
8355
*
8456
* @return $this
8557
*/
86-
public function subject($value)
58+
public function setSubject($value)
8759
{
88-
$this->subject = $value;
89-
90-
return $this;
60+
return $this->setParameter('headings', $this->parseValueToArray($value));
9161
}
9262

9363
/**
94-
* Set the message url.
64+
* Set the message template_id.
9565
*
9666
* @param string $value
9767
*
9868
* @return $this
9969
*/
100-
public function url($value)
101-
{
102-
$this->url = $value;
103-
104-
return $this;
105-
}
106-
107-
/**
108-
* Set the iOS badge increment count.
109-
*
110-
* @param int $count
111-
*
112-
* @return $this
113-
*/
114-
public function incrementIosBadgeCount($count = 1)
70+
public function setTemplate($value)
11571
{
116-
return $this->setParameter('ios_badgeType', 'Increase')
117-
->setParameter('ios_badgeCount', $count);
118-
}
72+
Arr::forget($this->payload, 'contents');
11973

120-
/**
121-
* Set the iOS badge decrement count.
122-
*
123-
* @param int $count
124-
*
125-
* @return $this
126-
*/
127-
public function decrementIosBadgeCount($count = 1)
128-
{
129-
return $this->setParameter('ios_badgeType', 'Increase')
130-
->setParameter('ios_badgeCount', -1 * $count);
74+
return $this->setParameter('template_id', $value);
13175
}
13276

13377
/**
134-
* Set the iOS badge count.
78+
* @param mixed $value
13579
*
136-
* @param int $count
137-
*
138-
* @return $this
80+
* @return array
13981
*/
140-
public function setIosBadgeCount($count)
82+
protected function parseValueToArray($value)
14183
{
142-
return $this->setParameter('ios_badgeType', 'SetTo')
143-
->setParameter('ios_badgeCount', $count);
84+
return (is_array($value)) ? $value : ['en' => $value];
14485
}
14586

14687
/**
14788
* Set additional data.
14889
*
14990
* @param string $key
150-
* @param string $value
91+
* @param mixed $value
15192
*
15293
* @return $this
15394
*/
154-
public function setData($key, $value)
95+
public function setData(string $key, $value)
15596
{
156-
$this->data[$key] = $value;
157-
158-
return $this;
97+
return $this->setParameter("data.{$key}", $value);
15998
}
16099

161100
/**
162-
* Set additional parameters.
101+
* Set parameters.
163102
*
164103
* @param string $key
165-
* @param string $value
166-
*
167-
* @return $this
168-
*/
169-
public function setParameter($key, $value)
170-
{
171-
$this->extraParameters[$key] = $value;
172-
173-
return $this;
174-
}
175-
176-
/**
177-
* Add a web button to the message.
178-
*
179-
* @param OneSignalWebButton $button
104+
* @param mixed $value
180105
*
181106
* @return $this
182107
*/
183-
public function webButton(OneSignalWebButton $button)
108+
public function setParameter(string $key, $value)
184109
{
185-
$this->webButtons[] = $button->toArray();
110+
Arr::set($this->payload, $key, $value);
186111

187112
return $this;
188113
}
189114

190115
/**
191-
* Add a native button to the message.
116+
* Get parameters.
192117
*
193-
* @param OneSignalButton $button
194-
*
195-
* @return $this
196-
*/
197-
public function button(OneSignalButton $button)
198-
{
199-
$this->buttons[] = $button->toArray();
200-
201-
return $this;
202-
}
203-
204-
/**
205-
* Set an image to all possible attachment variables.
206-
* @param string $imageUrl
118+
* @param string $key
119+
* @param mixed $default
207120
*
208-
* @return $this
121+
* @return mixed
209122
*/
210-
public function setImageAttachments($imageUrl)
123+
public function getParameter(string $key, $default = null)
211124
{
212-
$this->extraParameters['ios_attachments']['id1'] = $imageUrl;
213-
$this->extraParameters['big_picture'] = $imageUrl;
214-
$this->extraParameters['adm_big_picture'] = $imageUrl;
215-
$this->extraParameters['chrome_big_picture'] = $imageUrl;
216-
217-
return $this;
125+
return Arr::get($this->payload, $key, $default);
218126
}
219127

220128
/**
221129
* @return array
222130
*/
223131
public function toArray()
224132
{
225-
$message = [
226-
'contents' => ['en' => $this->body],
227-
'headings' => $this->subjectToArray(),
228-
'url' => $this->url,
229-
'buttons' => $this->buttons,
230-
'web_buttons' => $this->webButtons,
231-
'chrome_web_icon' => $this->icon,
232-
'chrome_icon' => $this->icon,
233-
'adm_small_icon' => $this->icon,
234-
'small_icon' => $this->icon,
235-
];
236-
237-
foreach ($this->extraParameters as $key => $value) {
238-
Arr::set($message, $key, $value);
239-
}
240-
241-
foreach ($this->data as $data => $value) {
242-
Arr::set($message, 'data.'.$data, $value);
243-
}
244-
245-
return $message;
246-
}
247-
248-
protected function subjectToArray()
249-
{
250-
if ($this->subject === null) {
251-
return [];
252-
}
253-
254-
return ['en' => $this->subject];
133+
return $this->payload;
255134
}
256135
}

src/OneSignalPayloadFactory.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,51 @@ class OneSignalPayloadFactory
1515
*
1616
* @return array
1717
*/
18-
public static function make($notifiable, Notification $notification, $targeting) : array
18+
public static function make($notifiable, Notification $notification, $targeting): array
1919
{
2020
$payload = $notification->toOneSignal($notifiable)->toArray();
2121

2222
if (static::isTargetingEmail($targeting)) {
2323
$payload['filters'] = collect([['field' => 'email', 'value' => $targeting['email']]]);
2424
} elseif (static::isTargetingTags($targeting)) {
25-
$payload['tags'] = collect([$targeting['tags']]);
25+
$array = $targeting['tags'];
26+
$res = count($array) == count($array, COUNT_RECURSIVE);
27+
if ($res) {
28+
$payload['tags'] = collect([$targeting['tags']]);
29+
} else {
30+
$payload['tags'] = collect($targeting['tags']);
31+
}
32+
} elseif (static::isTargetingIncludedSegments($targeting)) {
33+
$payload['included_segments'] = collect($targeting['included_segments']);
34+
} elseif (static::isTargetingExcludedSegments($targeting)) {
35+
$payload['excluded_segments'] = collect($targeting['excluded_segments']);
2636
} else {
2737
$payload['include_player_ids'] = collect($targeting);
2838
}
2939

3040
return $payload;
3141
}
3242

43+
/**
44+
* @param mixed $targeting
45+
*
46+
* @return bool
47+
*/
48+
protected static function isTargetingIncludedSegments($targeting)
49+
{
50+
return is_array($targeting) && array_key_exists('included_segments', $targeting);
51+
}
52+
53+
/**
54+
* @param mixed $targeting
55+
*
56+
* @return bool
57+
*/
58+
protected static function isTargetingExcludedSegments($targeting)
59+
{
60+
return is_array($targeting) && array_key_exists('excluded_segments', $targeting);
61+
}
62+
3363
/**
3464
* @param mixed $targeting
3565
*

src/OneSignalWebButton.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,10 @@ public function url($value)
8282
public function toArray()
8383
{
8484
return [
85-
'id' => $this->id,
85+
'id' => $this->id,
8686
'text' => $this->text,
8787
'icon' => $this->icon,
88-
'url' => $this->url,
88+
'url' => $this->url,
8989
];
9090
}
9191
}

0 commit comments

Comments
 (0)