Skip to content

[WIP]Refactor to Traits and add some little more Parameters #58

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

Merged
merged 22 commits into from
Aug 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
715521e
Refactor to Traits and add some little more Parameters
LKaemmerling Mar 14, 2018
3c7ae6a
Add Grouping Helpers
LKaemmerling Mar 14, 2018
7d42ea1
Add Button Helpers
LKaemmerling Mar 14, 2018
e00b7cc
Removed all unused protected variables
LKaemmerling Mar 14, 2018
95fe263
Make the setData Method call setParameter directly & allow the dot no…
LKaemmerling Mar 14, 2018
b926b13
Apply fixes from StyleCI (#59)
Mar 14, 2018
7101895
Working on Code with suggestions from timacdonald
LKaemmerling Mar 14, 2018
5095faf
Working on Code with suggestions from timacdonald #2
LKaemmerling Mar 14, 2018
3be802c
Add Deprecated DocTypes & remove Trait the contains only other Traits
LKaemmerling Mar 14, 2018
ad05ee0
Add Deprecated Trait that contains all deprecated methods
LKaemmerling Mar 15, 2018
d14ac9a
i really love travis :D
LKaemmerling Mar 15, 2018
52bfc9e
Add support for silent messages (#67)
kjostling May 22, 2018
b1e9d1b
Add method to set the template_id of the message (#77)
dyegonery Aug 9, 2018
a14695b
Fix Style CI
LKaemmerling Aug 16, 2018
860ce55
Fix Style CI Issues
LKaemmerling Aug 16, 2018
64bb529
Fix Style CI Issues
LKaemmerling Aug 16, 2018
32521dd
implemented sending of notifications based on segments (included/excl…
rylxes Aug 16, 2018
83c1c00
ability of user to send notification , filtering for multiple tags (#73)
rylxes Aug 16, 2018
7d7edcb
Pass Notification Object to Route
LKaemmerling Aug 16, 2018
785705b
Merge remote-tracking branch 'origin/refactor-to-traits' into refacto…
LKaemmerling Aug 16, 2018
5431c23
Fix Style CI Issue
LKaemmerling Aug 16, 2018
d5a94ab
Fix Style CI Issue
LKaemmerling Aug 16, 2018
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
2 changes: 0 additions & 2 deletions .styleci.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
preset: laravel

linting: true
2 changes: 1 addition & 1 deletion src/OneSignalButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function text($value)
public function toArray()
{
return [
'id' => $this->id,
'id' => $this->id,
'text' => $this->text,
'icon' => $this->icon,
];
Expand Down
2 changes: 1 addition & 1 deletion src/OneSignalChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct(OneSignalClient $oneSignal)
*/
public function send($notifiable, Notification $notification)
{
if (! $userIds = $notifiable->routeNotificationFor('OneSignal')) {
if (! $userIds = $notifiable->routeNotificationFor('OneSignal', $notification)) {
return;
}

Expand Down
197 changes: 38 additions & 159 deletions src/OneSignalMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,20 @@
namespace NotificationChannels\OneSignal;

use Illuminate\Support\Arr;
use NotificationChannels\OneSignal\Traits\Deprecated;
use NotificationChannels\OneSignal\Traits\Categories\ButtonHelpers;
use NotificationChannels\OneSignal\Traits\Categories\SilentHelpers;
use NotificationChannels\OneSignal\Traits\Categories\DeliveryHelpers;
use NotificationChannels\OneSignal\Traits\Categories\GroupingHelpers;
use NotificationChannels\OneSignal\Traits\Categories\AppearanceHelpers;
use NotificationChannels\OneSignal\Traits\Categories\AttachmentHelpers;

class OneSignalMessage
{
/** @var string */
protected $body;

/** @var string */
protected $subject;

/** @var string */
protected $url;

/** @var string */
protected $icon;

/** @var array */
protected $data = [];
use AppearanceHelpers, AttachmentHelpers, ButtonHelpers, DeliveryHelpers, GroupingHelpers, SilentHelpers, Deprecated;

/** @var array */
protected $buttons = [];

/** @var array */
protected $webButtons = [];

/** @var array */
protected $extraParameters = [];
protected $payload = [];

/**
* @param string $body
Expand All @@ -45,212 +33,103 @@ public static function create($body = '')
*/
public function __construct($body = '')
{
$this->body = $body;
$this->setBody($body);
}

/**
* Set the message body.
*
* @param string $value
*
* @return $this
*/
public function body($value)
{
$this->body = $value;

return $this;
}

/**
* Set the message icon.
*
* @param string $value
* @param mixed $value
*
* @return $this
*/
public function icon($value)
public function setBody($value)
{
$this->icon = $value;

return $this;
return $this->setParameter('contents', $this->parseValueToArray($value));
}

/**
* Set the message subject.
*
* @param string $value
* @param mixed $value
*
* @return $this
*/
public function subject($value)
public function setSubject($value)
{
$this->subject = $value;

return $this;
return $this->setParameter('headings', $this->parseValueToArray($value));
}

/**
* Set the message url.
* Set the message template_id.
*
* @param string $value
*
* @return $this
*/
public function url($value)
{
$this->url = $value;

return $this;
}

/**
* Set the iOS badge increment count.
*
* @param int $count
*
* @return $this
*/
public function incrementIosBadgeCount($count = 1)
public function setTemplate($value)
{
return $this->setParameter('ios_badgeType', 'Increase')
->setParameter('ios_badgeCount', $count);
}
Arr::forget($this->payload, 'contents');

/**
* Set the iOS badge decrement count.
*
* @param int $count
*
* @return $this
*/
public function decrementIosBadgeCount($count = 1)
{
return $this->setParameter('ios_badgeType', 'Increase')
->setParameter('ios_badgeCount', -1 * $count);
return $this->setParameter('template_id', $value);
}

/**
* Set the iOS badge count.
* @param mixed $value
*
* @param int $count
*
* @return $this
* @return array
*/
public function setIosBadgeCount($count)
protected function parseValueToArray($value)
{
return $this->setParameter('ios_badgeType', 'SetTo')
->setParameter('ios_badgeCount', $count);
return (is_array($value)) ? $value : ['en' => $value];
}

/**
* Set additional data.
*
* @param string $key
* @param string $value
* @param mixed $value
*
* @return $this
*/
public function setData($key, $value)
public function setData(string $key, $value)
{
$this->data[$key] = $value;

return $this;
return $this->setParameter("data.{$key}", $value);
}

/**
* Set additional parameters.
* Set parameters.
*
* @param string $key
* @param string $value
*
* @return $this
*/
public function setParameter($key, $value)
{
$this->extraParameters[$key] = $value;

return $this;
}

/**
* Add a web button to the message.
*
* @param OneSignalWebButton $button
* @param mixed $value
*
* @return $this
*/
public function webButton(OneSignalWebButton $button)
public function setParameter(string $key, $value)
{
$this->webButtons[] = $button->toArray();
Arr::set($this->payload, $key, $value);

return $this;
}

/**
* Add a native button to the message.
* Get parameters.
*
* @param OneSignalButton $button
*
* @return $this
*/
public function button(OneSignalButton $button)
{
$this->buttons[] = $button->toArray();

return $this;
}

/**
* Set an image to all possible attachment variables.
* @param string $imageUrl
* @param string $key
* @param mixed $default
*
* @return $this
* @return mixed
*/
public function setImageAttachments($imageUrl)
public function getParameter(string $key, $default = null)
{
$this->extraParameters['ios_attachments']['id1'] = $imageUrl;
$this->extraParameters['big_picture'] = $imageUrl;
$this->extraParameters['adm_big_picture'] = $imageUrl;
$this->extraParameters['chrome_big_picture'] = $imageUrl;

return $this;
return Arr::get($this->payload, $key, $default);
}

/**
* @return array
*/
public function toArray()
{
$message = [
'contents' => ['en' => $this->body],
'headings' => $this->subjectToArray(),
'url' => $this->url,
'buttons' => $this->buttons,
'web_buttons' => $this->webButtons,
'chrome_web_icon' => $this->icon,
'chrome_icon' => $this->icon,
'adm_small_icon' => $this->icon,
'small_icon' => $this->icon,
];

foreach ($this->extraParameters as $key => $value) {
Arr::set($message, $key, $value);
}

foreach ($this->data as $data => $value) {
Arr::set($message, 'data.'.$data, $value);
}

return $message;
}

protected function subjectToArray()
{
if ($this->subject === null) {
return [];
}

return ['en' => $this->subject];
return $this->payload;
}
}
34 changes: 32 additions & 2 deletions src/OneSignalPayloadFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,51 @@ class OneSignalPayloadFactory
*
* @return array
*/
public static function make($notifiable, Notification $notification, $targeting) : array
public static function make($notifiable, Notification $notification, $targeting): array
{
$payload = $notification->toOneSignal($notifiable)->toArray();

if (static::isTargetingEmail($targeting)) {
$payload['filters'] = collect([['field' => 'email', 'value' => $targeting['email']]]);
} elseif (static::isTargetingTags($targeting)) {
$payload['tags'] = collect([$targeting['tags']]);
$array = $targeting['tags'];
$res = count($array) == count($array, COUNT_RECURSIVE);
if ($res) {
$payload['tags'] = collect([$targeting['tags']]);
} else {
$payload['tags'] = collect($targeting['tags']);
}
} elseif (static::isTargetingIncludedSegments($targeting)) {
$payload['included_segments'] = collect($targeting['included_segments']);
} elseif (static::isTargetingExcludedSegments($targeting)) {
$payload['excluded_segments'] = collect($targeting['excluded_segments']);
} else {
$payload['include_player_ids'] = collect($targeting);
}

return $payload;
}

/**
* @param mixed $targeting
*
* @return bool
*/
protected static function isTargetingIncludedSegments($targeting)
{
return is_array($targeting) && array_key_exists('included_segments', $targeting);
}

/**
* @param mixed $targeting
*
* @return bool
*/
protected static function isTargetingExcludedSegments($targeting)
{
return is_array($targeting) && array_key_exists('excluded_segments', $targeting);
}

/**
* @param mixed $targeting
*
Expand Down
4 changes: 2 additions & 2 deletions src/OneSignalWebButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ public function url($value)
public function toArray()
{
return [
'id' => $this->id,
'id' => $this->id,
'text' => $this->text,
'icon' => $this->icon,
'url' => $this->url,
'url' => $this->url,
];
}
}
Loading