Last active
September 5, 2017 15:11
-
-
Save pbelyaev/543a3952883fb1158c07c08029212d28 to your computer and use it in GitHub Desktop.
Eloquent notifiable model that can send notifications to Slack (without any attachment to a model)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
use Illuminate\Database\Eloquent\Model; | |
use Illuminate\Notifications\Notifiable; | |
use Illuminate\Notifications\Notification; | |
class SlackNotification extends Model | |
{ | |
use Notifiable; | |
/** | |
* @var string | |
*/ | |
private $slack_webhook_url; | |
/** | |
* @param Notification $notification | |
* @param string $slackWebhookUrl | |
* @return SlackNotification | |
*/ | |
public static function sendNotification(Notification $notification, string $slackWebhookUrl): SlackNotification | |
{ | |
return tap( | |
(new static), | |
function (SlackNotification $slackNotification) use ($slackWebhookUrl, $notification) { | |
$slackNotification->slack_webhook_url = $slackWebhookUrl; | |
$slackNotification->notify($notification); | |
} | |
); | |
} | |
/** | |
* @return string | |
*/ | |
public function routeNotificationForSlack(): string | |
{ | |
return $this->slack_webhook_url; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment