Skip to content

Commit

Permalink
Add helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabio Rocha committed Apr 30, 2021
1 parent df6248e commit 951c562
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 24 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"Frf\\DiscordNotification\\": "src/"
},
"files": [
"src/helpers.php"
"src/Helper/"
]
},
"extra": {
Expand Down
21 changes: 21 additions & 0 deletions src/Helper/DiscordChannel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Frf\DiscordNotification\Helper;

use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;
use Illuminate\Notifications\Notification;

class DiscordChannel
{
public function send($notifiable, Notification $notification)
{
$discordMessage = $notification->toDiscord();

$discordWebhook = $notifiable->destination['url'];

(new Client())->post($discordWebhook, [
RequestOptions::JSON => $discordMessage->toArray(),
]);
}
}
103 changes: 103 additions & 0 deletions src/Helper/DiscordMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

namespace Frf\DiscordNotification\Helper;

use Carbon\Carbon;

class DiscordMessage
{
const COLOR_SUCCESS = '#0B6623';
const COLOR_WARNING = '#FD6A02';
const COLOR_ERROR = '#ED2939';

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

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

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

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

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

public function title($title)
{
$this->title = $title;

return $this;
}

/**
* @param array|string $descriptionLines
*
* @return \App\Services\NotificationChannels\Discord\DiscordMessage
*/
public function description($descriptionLines): self
{
if (! is_array($descriptionLines)) {
$descriptionLines = [$descriptionLines];
}

$this->description = implode(PHP_EOL, $descriptionLines);

return $this;
}

public function timestamp(Carbon $carbon): self
{
$this->timestamp = $carbon->toIso8601String();

return $this;
}

public function footer(string $footer): self
{
$this->footer = $footer;

return $this;
}

public function success(): self
{
$this->color = static::COLOR_SUCCESS;

return $this;
}

public function warning(): self
{
$this->color = static::COLOR_WARNING;

return $this;
}

public function error(): self
{
$this->color = static::COLOR_ERROR;

return $this;
}

public function toArray(): array
{
return [
'embeds' => [
[
'title' => $this->title,
'type' => 'rich',
'description' => $this->description,
'color' => hexdec($this->color),
'footer' => [
'text' => $this->footer ?? '',
],
'timestamp' => $this->timestamp,
],
],
];
}
}
24 changes: 24 additions & 0 deletions src/Helper/DiscordMessageHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Frf\DiscordNotification\Helper;

use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;
use Carbon\Carbon;

class DiscordMessageHelper
{
public static function send(string $title, array $descriptions, string $footer = "Footer")
{
$discordMessage = (new DiscordMessage())
->error()
->title($title)
->description($descriptions)
->footer($footer)
->timestamp(Carbon::now());

(new Client())->post(env('DISCORD_HOOK'), [
RequestOptions::JSON => $discordMessage->toArray(),
]);
}
}
23 changes: 0 additions & 23 deletions src/helpers.php

This file was deleted.

0 comments on commit 951c562

Please sign in to comment.