Skip to content
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

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

Merged
merged 22 commits into from
Aug 16, 2018
Merged
Show file tree
Hide file tree
Changes from 9 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
189 changes: 48 additions & 141 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\Categories\AppearanceHelpers;
use NotificationChannels\OneSignal\Traits\Categories\AttachmentHelpers;
use NotificationChannels\OneSignal\Traits\Categories\ButtonHelpers;
use NotificationChannels\OneSignal\Traits\Categories\DeliveryHelpers;
use NotificationChannels\OneSignal\Traits\Categories\GroupingHelpers;


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

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

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

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

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

/** @var array */
protected $buttons = [];
use AppearanceHelpers, AttachmentHelpers, ButtonHelpers, DeliveryHelpers, GroupingHelpers;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use relative trait names here - then you could remove all of the top use imports.

i.e.

class OneSignalMessage
{
    use Traits\Categories\ButtonHelpers,
        Traits\Categories\GroupingHelpers,
        Traits\Categories\DeliveryHelpers,
        Traits\Categories\AppearanceHelpers,
        Traits\Categories\AttachmentHelpers;

But that is just a style thing - and not really needed


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

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

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

/**
* Set the message body.
*
* @param string $value
* @param mixed $value
*
* @deprecated use setBody instead
*
* @return $this
*/
public function body($value)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it would be good to move these deprecated methods to Traits\Deprecated to clean things up?

{
$this->body = $value;

return $this;
return $this->setBody($value);
}

/**
* Set the message icon.
* Set the message body.
*
* @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
*
* @deprecated Use setSubject instead
*
* @return $this
*/
public function subject($value)
{
$this->subject = $value;

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

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

return $this;
return $this->setSubject($value);
}

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

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

/**
* Set the iOS badge count.
* @param string $value
*
* @param int $count
* @deprecated use setUrl Instead
*
* @return $this
*/
public function setIosBadgeCount($count)
public function url($value)
{
return $this->setParameter('ios_badgeType', 'SetTo')
->setParameter('ios_badgeCount', $count);
return $this->setUrl($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;
}
}
Loading