Skip to content

Commit

Permalink
Laravel 7 (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewnessworthy authored Aug 4, 2020
1 parent 903ca09 commit 79bdee5
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 180 deletions.
15 changes: 11 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
}
],
"require": {
"php": ">=7.2",
"illuminate/notifications": "~5.5 || ~6.0",
"illuminate/support": "~5.5 || ~6.0",
"php": "^7.2",
"illuminate/notifications": "^5.5 || ^6.0 || ^7.0",
"illuminate/support": "^5.5 || ^6.0 || ^7.0",
"sailthru/sailthru-php5-client": "^1.2"
},
"require-dev": {
"mockery/mockery": "^1.0",
"phpunit/phpunit": "^8.0"
"phpunit/phpunit": "^9.0"
},
"autoload": {
"psr-4": {
Expand All @@ -34,6 +34,13 @@
"scripts": {
"test": "vendor/bin/phpunit"
},
"extra": {
"laravel": {
"providers": [
"NotificationChannels\\Sailthru\\SailthruServiceProvider"
]
}
},
"config": {
"sort-packages": true
}
Expand Down
38 changes: 0 additions & 38 deletions src/Events/MessageFailedToSend.php

This file was deleted.

38 changes: 0 additions & 38 deletions src/Events/MessageWasSent.php

This file was deleted.

11 changes: 0 additions & 11 deletions src/Exceptions/CouldNotSendNotification.php

This file was deleted.

125 changes: 92 additions & 33 deletions src/SailthruChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,27 @@

namespace NotificationChannels\Sailthru;

use Exception;
use Illuminate\Notifications\Events\NotificationFailed;
use Illuminate\Notifications\Events\NotificationSent;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Log;
use NotificationChannels\Sailthru\Events\MessageFailedToSend;
use NotificationChannels\Sailthru\Events\MessageWasSent;
use Sailthru_Client_Exception;

class SailthruChannel
{
/**
* SailthruChannel constructor.
*
* @param \Sailthru_Client $sailthru
* @var SailthruClient
*/
public function __construct(\Sailthru_Client $sailthru)
{
protected $sailthru;

/**
* @param SailthruClient $sailthru
*/
public function __construct(
SailthruClient $sailthru
) {
$this->sailthru = $sailthru;
}

Expand All @@ -37,9 +44,11 @@ public static function getDefaultVars(): array
*
* @return array
*/
public function send($notifiable, Notification $notification)
{
if (!config('services.sailthru.enabled', true)) {
public function send(
$notifiable,
Notification $notification
) {
if (config('services.sailthru.enabled') === false) {
Log::info(
'Sending Sailthru message',
[
Expand All @@ -54,17 +63,39 @@ public function send($notifiable, Notification $notification)
try {
/** @var SailthruMessage $message */
$message = $notification->toSailthru($notifiable);
$message->mergeDefaultVars(self::getDefaultVars());
$message->mergeDefaultVars(
static::getDefaultVars()
);

$response = $message->isMultiSend()
? $this->multiSend($message)
: $this->singleSend($message);

event(new MessageWasSent($message, $response));
Event::dispatch(
new NotificationSent(
$notifiable,
$notification,
'sailthru',
[
'message' => $message,
'response' => $response,
]
)
);

return $response;
} catch (\Sailthru_Client_Exception $e) {
event(new MessageFailedToSend($message, $e));
} catch (Exception $e) {
Event::dispatch(
new NotificationFailed(
$notifiable,
$notification,
'sailthru',
[
'message' => $message,
'exception' => $e,
]
)
);

return [];
}
Expand All @@ -73,45 +104,73 @@ public function send($notifiable, Notification $notification)
/**
* @param SailthruMessage $sailthruMessage
*
* @throws \Sailthru_Client_Exception
* @throws Sailthru_Client_Exception
*
* @return array
*/
protected function multiSend(SailthruMessage $sailthruMessage)
{
protected function multiSend(
SailthruMessage $sailthruMessage
) {
$template = $sailthruMessage->getTemplate();
$toEmail = $sailthruMessage->getToEmail();
$vars = $sailthruMessage->getVars();
$eVars = $sailthruMessage->getEVars();
$options = $sailthruMessage->getOptions();

if (config('services.sailthru.log_payload') === true) {
Log::debug(
'Sailthru Payload',
[
'template' => $template,
'email' => $toEmail,
'vars' => $vars,
'eVars' => $eVars,
'options' => $options,
]
);
}

return $this->sailthru->multisend(
$sailthruMessage->getTemplate(),
$sailthruMessage->getToEmail(),
$sailthruMessage->getVars(),
$sailthruMessage->getEVars(),
$sailthruMessage->getOptions()
$template,
$toEmail,
$vars,
$eVars,
$options
);
}

/**
* @param SailthruMessage $sailthruMessage
*
* @throws Sailthru_Client_Exception
*
* @return array
*/
protected function singleSend(SailthruMessage $sailthruMessage)
{
if (config('services.sailthru.log_payload', false)) {
protected function singleSend(
SailthruMessage $sailthruMessage
) {
$template = $sailthruMessage->getTemplate();
$toEmail = $sailthruMessage->getToEmail();
$vars = $sailthruMessage->getVars();
$options = $sailthruMessage->getOptions();

if (config('services.sailthru.log_payload') === true) {
Log::debug(
'Sailthru Payload',
[
'template' => $sailthruMessage->getTemplate(),
'email' => $sailthruMessage->getToEmail(),
'vars' => $sailthruMessage->getVars(),
'options' => $sailthruMessage->getOptions(),
'template' => $template,
'email' => $toEmail,
'vars' => $vars,
'options' => $options,
]
);
}

return $this->sailthru->send(
$sailthruMessage->getTemplate(),
$sailthruMessage->getToEmail(),
$sailthruMessage->getVars(),
$sailthruMessage->getOptions()
$template,
$toEmail,
$vars,
$options
);
}
}
9 changes: 9 additions & 0 deletions src/SailthruClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace NotificationChannels\Sailthru;

use Sailthru_Client;

class SailthruClient extends Sailthru_Client
{
}
Loading

0 comments on commit 79bdee5

Please sign in to comment.