Skip to content

Commit

Permalink
Making world a better place
Browse files Browse the repository at this point in the history
  • Loading branch information
Alireza Mirsepassi committed Nov 11, 2018
1 parent f651abb commit 254c785
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 13 deletions.
7 changes: 7 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@
"NotificationChannels\\SendPulse\\Test\\": "tests"
}
},
"extra": {
"laravel": {
"providers": [
"NotificationChannels\\SendPulse\\SendPulseServiceProvider"
]
}
},
"scripts": {
"test": "vendor/bin/phpunit"
},
Expand Down
11 changes: 11 additions & 0 deletions src/Exceptions/InvalidToSendPulseReturnType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace NotificationChannels\SendPulse\Exceptions;

class InvalidToSendPulseReturnType extends \Exception
{
public static function serviceRespondedWithAnError($response)
{
return new static("Return type must be instance of SendPulseMessage");
}
}
25 changes: 15 additions & 10 deletions src/SendPulseChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@


use Sendpulse\RestApi\ApiClient;
use Sendpulse\RestApi\Storage\FileStorage;
use NotificationChannels\SendPulse\Exceptions\CouldNotSendNotification;
use NotificationChannels\SendPulse\Events\MessageWasSent;
use NotificationChannels\SendPulse\Events\SendingMessage;
use Illuminate\Notifications\Notification;
use NotificationChannels\SendPulse\Exceptions\InvalidToSendPulseReturnType;

class SendPulseChannel
{
/** @var ApiClient */
protected $sendPulse;

/**
* SendPulseChannel constructor.
*
* @param ApiClient $apiClient
*/
public function __construct(ApiClient $apiClient)
{
$this->sendPulse = $apiClient;
Expand All @@ -23,17 +25,20 @@ public function __construct(ApiClient $apiClient)
/**
* Send the given notification.
*
* @param mixed $notifiable
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
*
* @throws \NotificationChannels\:channel_namespace\Exceptions\CouldNotSendNotification
* @return \stdClass
* @throws InvalidToSendPulseReturnType
*/
public function send($notifiable, Notification $notification)
{
//$response = [a call to the api of your notification send]
/** @var SendPulseMessage $message */
$message = $notification->toSendPulse($notifiable);
if (! $message instanceof SendPulseMessage) {
throw new InvalidToSendPulseReturnType();
}

// if ($response->error) { // replace this by the code need to check for errors
// throw CouldNotSendNotification::serviceRespondedWithAnError($response);
// }
return $this->sendPulse->createPushTask($message->payload['task'], $message->payload['additionalParams']);
}
}
49 changes: 48 additions & 1 deletion src/SendPulseMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,55 @@

class SendPulseMessage
{
/** @var \Illuminate\Support\Collection */
public $payload;

/**
* SendPulseMessage constructor.
*/
public function __construct()
{
$this->payload = ['task' => [], 'additionalParams' => []];
}

public static function create()
{

return new static();
}

public function setTitle($title)
{
$this->payload['task']['title'] = $title;
return $this;
}

public function setBody($body)
{
$this->payload['task']['body'] = $body;
return $this;
}

public function setWebsiteId($websiteId)
{
$this->payload['task']['website_id'] = $websiteId;
return $this;
}

public function setTtl($ttl)
{
$this->payload['task']['ttl'] = $ttl;
return $this;
}

public function setStretchTime($stretch_time)
{
$this->payload['task']['stretch_time'] = $stretch_time;
return $this;
}

public function setAdditionalParameters(Array $parameters)
{
$this->payload['additionalParams'] = $parameters;
return $this;
}
}
3 changes: 1 addition & 2 deletions src/SendPulseServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ class SendPulseServiceProvider extends ServiceProvider
public function boot()
{
// Bootstrap code here.

$this->app->when(SendPulseChannel::class)
->needs(ApiClient::class)
->give(function () {
$sendPulseConfig = config('broadcasting.connections.sendpulse');
$sendPulseConfig = config('services.sendpulse');

return new ApiClient($sendPulseConfig['API_USER_ID'], $sendPulseConfig['API_SECRET'], new FileStorage());
});
Expand Down

0 comments on commit 254c785

Please sign in to comment.