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

Conversation

LKaemmerling
Copy link
Collaborator

@LKaemmerling LKaemmerling commented Mar 14, 2018

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

@timacdonald
Copy link
Contributor

timacdonald commented Mar 14, 2018

You know I was thinking something similar to this when I was adding my PR's. I was thinking you could ditch all the protected properties and just having a $payload variable that was built. The idea being that toArray() wouldn't need to do anything but return the $payload variable.

public function icon($value)
{
    $this->icon = $value;

    return $this;
}

becomes

public function icon($value)
{
    return $this->setParameter('chrome_web_icon', $value)
                ->setParameter('chrome_icon', $value)
                ->setParameter('adm_small_icon', $value)
                ->setParameter('small_icon', $value);
}

Because now that you've added the parameter helper, its like there is these "special" parameters that have instance properties - and everything else would just be set via setParameter which feels weird to me. I think standardising the way the payload is built up would feel much nicer IMO

Cause at the end of the day - they are all just parameters - even the $data could just be on the payload..

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

then in the setParameter method would just use Illuminate\Support\Arr to set values, allowing for "dot notation"

@timacdonald
Copy link
Contributor

haha, judging by the PR's coming in - I spoke too soon haha - your all over it 😅

@LKaemmerling
Copy link
Collaborator Author

LKaemmerling commented Mar 14, 2018

Yeah, your right :)

I've refactored it, so there aren't any unused protected variables there anymore. I'd leave the "setData" Method on the Class, since it could be usefull for some people to set the data and because auf the Backward Compability i'd leave the body, subject & url method

Edit: Ha what a good idea with the data :D, did it :D

Copy link
Collaborator

@Lloople Lloople left a comment

Choose a reason for hiding this comment

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

Looks pretty good! This code organization is very beatufiul 🙂

@timacdonald
Copy link
Contributor

I'll pull it down and check it out - looks nice :)

Copy link
Contributor

@timacdonald timacdonald left a comment

Choose a reason for hiding this comment

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

I hope you don't mind the feedback. I know I'm not a maintainer - so it's probably not my place - but thought I'd throw my thoughts in the mix.


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

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

Choose a reason for hiding this comment

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

can ditch this var now as we're going straight onto the parameter in the setData method

public function icon($value)
{
$this->icon = $value;
if (is_array($value)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could ditch the else here if you wanted to with an early return

    public function body($value)
    {
        if (is_array($value)) {
            return $this->setParameter('contents', $value);
        }
            
        return $this->setParameter('contents', ['en' => $value]);
    }

$this->setParameter('headings', $value);
} else {
$this->setParameter('headings', ['en' => $value]);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here as with body - however if going with the array check version, you might want to introduce a parseLocale() method to remove the duplicate code...

protected function parseLocale($value)
{
    return is_array($value) ? $value : ['en' => $value];
}

// then subject / body etc with localisation could just be

public function subject()
{
    $this->setParameter('headings', $this->parseLocale($value));
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah thats a better solution!

*
* @return $this
*/
public function setIosSound(string $soundUrl)
Copy link
Contributor

Choose a reason for hiding this comment

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

I kinda feel like "sound" is not "appearance" ...maybe

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I've used the "categories" from the OneSignal Documentation for the "grouping" of the methods:
https://documentation.onesignal.com/reference
And there are the sound Settings under "Appearance" :)

Copy link
Contributor

Choose a reason for hiding this comment

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

oh - that makes way more sense haha - sorry should have checked that :) I like that 👍

*/
public function setWindowsSound(string $soundUrl)
{
return $this->setParameter('wp_sound', $soundUrl)->setParameter('wp_wns_sound', $soundUrl);
Copy link
Contributor

Choose a reason for hiding this comment

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

->setParameter('wp_wns_sound', $soundUrl); on a new line here for consistency.

*
* @return $this
*/
public function button(OneSignalButton $button)
Copy link
Contributor

Choose a reason for hiding this comment

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

do you think maybe this should be prefixed. For example - take the notifications conversation we had:

incrementBadgeCount() sound like it will do it everywhere.

incrementIosBadgeCount() sound like it will do it on iOS.

webButtons sounds like it is for the web (which is it)

buttons sounds like it will do it everywhere - web + native

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah i agree with you, but i think this is something that should clarify first, since they named it "web buttons" and "buttons"
https://documentation.onesignal.com/reference#section-action-buttons

Copy link
Contributor

Choose a reason for hiding this comment

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

uh! yea - thats tough because I think matching their docs is good!

*
* @return $this
*/
public function setTTL(int $ttl)
Copy link
Contributor

Choose a reason for hiding this comment

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

As we are going with Ios of iOS, might want to do Ttl. I hate this capitalisation stuff with acronyms like that i.e Html etc. always feels weird.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah you are right :)

{
/**
* Set the Android Grouping Parameters.
* @param string $group
Copy link
Contributor

Choose a reason for hiding this comment

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

new line between description and @param


/**
* Set the Amazon (FireOS) Grouping Parameters.
* @param string $group
Copy link
Contributor

Choose a reason for hiding this comment

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

new line between description and @param

@@ -0,0 +1,14 @@
<?php
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you are probably better off just useing these in the OneSignalMessage class rather than using a trait that is only there to use other traits IMO

@timacdonald
Copy link
Contributor

Please note - I don't maintain anything like this - so I feel bad for leaving so much feedback as just a random contributor - but I want to say this is awesome and will really clean things up

@timacdonald
Copy link
Contributor

As all the introduced methods are actionThing i.e. setImage incrementIosBadgeCount perhaps we should introduce setX methods for the following - just to standardise the API:

icon()
webButton()
webButtons()
button()
buttons()
body()
subject()

@LKaemmerling
Copy link
Collaborator Author

@timacdonald the problem with this methods is the backward compability... i don't want to break alle applications. But you are right,
a better solution were to deprecate the old method and make them an alias for the new.

@LKaemmerling
Copy link
Collaborator Author

@timacdonald should be done now :)

Thank you really for all your help and ideas on this!

@timacdonald
Copy link
Contributor

No worries - thanks for keeping this package alive and kicking. I’ll take another looksy with fresh eyes this morning.

Yea I was thinking alias the old methods to the new ones to keep that backwards compat is a good idea.

Copy link
Contributor

@timacdonald timacdonald left a comment

Choose a reason for hiding this comment

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

This is totally fine - but I made a couple of comments. They are probably just personal preference things - so might not really benefit the code at all. Looking good!

*
* @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?

*/
public function setWebButtons(array $buttons)
{
return $this->setParameter('web_buttons', collect($buttons)->map(function ($button) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is probably fine - but you could just make use of the existing method here:

collect($buttons)->each(function ($button) {
    $this->setWebButton($button);
});

That way if the key every changes you only have one place to manage it - but again, probably not needed - just an idea :)

Could also apply to setButtons?


/** @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

@timacdonald
Copy link
Contributor

timacdonald commented Mar 14, 2018

Thats probably the extent of my feedback - it's looking good and tests are passing!

Edit: Maybe one last thing to consider is if there are any other methods that you previously could chain to ensure you still can.

@LKaemmerling
Copy link
Collaborator Author

LKaemmerling commented Mar 15, 2018

@timacdonald Thank you for all your comments & feedback! I'v edit the points. The next days i'll add tests for all of the new methods and i'll try to refactor the tests as well

@timacdonald
Copy link
Contributor

No probs at all!

Fixed Maximum function nesting level of '256' reached, aborting!
@LKaemmerling
Copy link
Collaborator Author

Sorry guys, that i currently do nothing here. This week was a little bit crazy and i haven't time for it.

A defensive change in message class adding support for silent push notifications (in the traits branch now).
@LKaemmerling
Copy link
Collaborator Author

@kjostling Can you test this branch? When you can't find an error too, we can release it as a rc :)

@LKaemmerling
Copy link
Collaborator Author

@timacdonald @kjostling have you tested this? Can we release it :)?

@timacdonald
Copy link
Contributor

Hey,

I haven't had a chance to test this myself - but I trust you 👍

* Add method to set the template_id of the message

* Fix typo on method for setting template

* Remove contents when using a template

Remove any contents set by the constructor when using the `setTemplate()` method.
@kjostling
Copy link

Sorry for late reply, im already using this in a production env. Believe its tested from my point of view.

@LKaemmerling
Copy link
Collaborator Author

Okay good :) Then i will fix the styleci errors and then i will prepare the release

LKaemmerling and others added 9 commits August 16, 2018 09:58
…uded) (#72)

* implemented sending of notifications based on segments (included and excluded)

* styleci linting

* Apply fixes from StyleCI
* ability of user to send notification , filtering for multiple tags

* added linting

* Apply fixes from StyleCI
@LKaemmerling LKaemmerling merged commit 84d6e05 into master Aug 16, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants