Skip to content

Commit 26842cb

Browse files
Refactor NotifierMailTest and NotifierDiscordTest
1 parent 56d31b1 commit 26842cb

10 files changed

+235
-71
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ This is the contents of the published config file:
4444

4545
```php
4646
return [
47+
// Default notifier client to send HTTP request, can be `stream`, `curl` or `guzzle`.
48+
// `guzzle` is not included in this package, you need to install it manually.
49+
'client' => env('NOTIFIER_CLIENT', 'stream'),
50+
4751
'discord' => [
4852
// Default Discord webhook URL.
4953
'webhook' => env('NOTIFIER_DISCORD_WEBHOOK', null),

config/notifier.php

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
<?php
22

33
return [
4+
// Default notifier client to send HTTP request, can be `stream`, `curl` or `guzzle`.
5+
// `guzzle` is not included in this package, you need to install it manually.
6+
'client' => env('NOTIFIER_CLIENT', 'stream'),
7+
48
'discord' => [
59
// Default Discord webhook URL.
610
'webhook' => env('NOTIFIER_DISCORD_WEBHOOK', null),

src/Notifier/Discord/NotifierDiscordContainer.php

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Kiwilan\Notifier\Notifier\Discord;
44

5+
use Illuminate\Support\Facades\Log;
56
use Kiwilan\Notifier\Utils\NotifierRequest;
67

78
abstract class NotifierDiscordContainer
@@ -30,6 +31,12 @@ public function send(): static
3031

3132
$this->isSuccess = $this->request->getStatusCode() === 204;
3233

34+
if ($this->isSuccess) {
35+
Log::error("Notifier: discord notification failed with HTTP {$this->request->getStatusCode()}", [
36+
$this->request->toArray(),
37+
]);
38+
}
39+
3340
return $this;
3441
}
3542
}

src/Notifier/NotifierSlack.php

+4-65
Original file line numberDiff line numberDiff line change
@@ -25,70 +25,11 @@ public function message(array|string $message): self
2525
return $this;
2626
}
2727

28-
/**
29-
* Prepare message for sending.
30-
*/
31-
private function createBody(): self
32-
{
33-
$this->requestData = [
34-
'text' => $this->message ?? '',
35-
];
36-
37-
return $this;
38-
}
39-
4028
public function send(): bool
4129
{
42-
$this->createBody();
43-
// $spatie->message($this->message);
44-
// $spatie->blocks([
45-
// [
46-
// 'type' => 'section',
47-
// 'text' => [
48-
// 'type' => 'mrkdwn',
49-
// 'text' => $this->message,
50-
// ],
51-
// ],
52-
// ]);
53-
// // $payload = $this->text
54-
// // ? ['type' => 'mrkdwn', 'text' => $this->text]
55-
// // : ['blocks' => $this->blocks];
56-
57-
// // if ($this->channel) {
58-
// // $payload['channel'] = $this->channel;
59-
// // }
60-
61-
// // Http::post($this->webhookUrl, $payload)->throw();
62-
// $this->request = NotifierRequest::make($this->webhook)
63-
// // ->requestData([
64-
// // 'type' => 'mrkdwn',
65-
// // 'text' => 'Hello, Slack!',
66-
// // ])
67-
// ->requestData([
68-
// 'blocks' => [
69-
// [
70-
// 'type' => 'section',
71-
// 'text' => [
72-
// 'type' => 'mrkdwn',
73-
// 'text' => 'Danny Torrence left the following review for your property:',
74-
// ],
75-
// ],
76-
// [
77-
// 'type' => 'section',
78-
// 'block_id' => 'section567',
79-
// 'text' => [
80-
// 'type' => 'mrkdwn',
81-
// 'text' => "<https://example.com|Overlook Hotel> \n :star: \n Doors had too many axe holes, guest in room 237 was far too rowdy, whole place felt stuck in the 1920s.",
82-
// ],
83-
// 'accessory' => [
84-
// 'type' => 'image',
85-
// 'image_url' => 'https://is5-ssl.mzstatic.com/image/thumb/Purple3/v4/d3/72/5c/d3725c8f-c642-5d69-1904-aa36e4297885/source/256x256bb.jpg',
86-
// 'alt_text' => 'Haunted hotel image',
87-
// ],
88-
// ],
89-
// ],
90-
// ])
91-
// ->send();
30+
$this->request = NotifierRequest::make($this->webhook)
31+
->requestData($this->toArray())
32+
->send();
9233

9334
if ($this->request->getStatusCode() !== 200) {
9435
$this->logError("status code {$this->request->getStatusCode()}", [
@@ -104,9 +45,7 @@ public function send(): bool
10445
public function toArray(): array
10546
{
10647
return [
107-
'webhook' => $this->webhook,
108-
'message' => $this->message,
109-
'request' => $this->request->toArray(),
48+
'text' => $this->message ?? '',
11049
];
11150
}
11251
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Kiwilan\Notifier\Notifier\Slack;
4+
5+
use Illuminate\Support\Facades\Log;
6+
use Kiwilan\Notifier\Utils\NotifierRequest;
7+
8+
abstract class NotifierSlackContainer
9+
{
10+
protected function __construct(
11+
protected ?string $webhook = null,
12+
protected ?NotifierRequest $request = null,
13+
protected bool $isSuccess = false,
14+
) {
15+
}
16+
17+
abstract public static function create(string $webhook, string $description): self;
18+
19+
abstract public function toArray(): array;
20+
21+
public function isSuccess(): bool
22+
{
23+
return $this->isSuccess;
24+
}
25+
26+
public function send(): static
27+
{
28+
$this->request = NotifierRequest::make($this->webhook)
29+
->requestData($this->toArray())
30+
->send();
31+
32+
$this->isSuccess = $this->request->getStatusCode() === 200;
33+
34+
if ($this->isSuccess) {
35+
Log::error("Notifier: slack notification failed with HTTP {$this->request->getStatusCode()}", [
36+
$this->request->toArray(),
37+
]);
38+
}
39+
40+
return $this;
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace Kiwilan\Notifier\Notifier\Slack;
4+
5+
class NotifierSlackMessage extends NotifierSlackContainer
6+
{
7+
protected function __construct(
8+
protected ?string $message = null,
9+
protected ?string $username = null,
10+
protected ?string $avatarUrl = null,
11+
) {
12+
}
13+
14+
public static function create(string $webhook, string $message): self
15+
{
16+
if (strlen($message) > 2000) {
17+
$message = substr($message, 0, 1980).'...';
18+
}
19+
20+
$self = new self($message);
21+
$self->webhook = $webhook;
22+
23+
return $self;
24+
}
25+
26+
public function user(string $username, ?string $avatarUrl = null): self
27+
{
28+
$this->username = $username;
29+
30+
if ($avatarUrl) {
31+
$this->avatarUrl = $avatarUrl;
32+
}
33+
34+
return $this;
35+
}
36+
37+
public function toArray(): array
38+
{
39+
$data = [];
40+
41+
if ($this->username) {
42+
$data['username'] = $this->username;
43+
}
44+
45+
if ($this->avatarUrl) {
46+
$data['avatar_url'] = $this->avatarUrl;
47+
}
48+
49+
$data['content'] = $this->message ?? 'Empty message.';
50+
51+
return $data;
52+
}
53+
}

src/Utils/NotifierRequest.php

+8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class NotifierRequest
77
protected function __construct(
88
protected string $url,
99
protected string $mode = 'stream',
10+
protected bool $modeAuto = true,
1011
protected array $headers = [],
1112
protected array $request_data = [],
1213
protected ?array $response_body = [],
@@ -32,6 +33,7 @@ public static function make(string $url)
3233
public function useStream(): self
3334
{
3435
$this->mode = 'stream';
36+
$this->modeAuto = false;
3537

3638
return $this;
3739
}
@@ -42,6 +44,7 @@ public function useStream(): self
4244
public function useCurl(): self
4345
{
4446
$this->mode = 'curl';
47+
$this->modeAuto = false;
4548

4649
return $this;
4750
}
@@ -52,6 +55,7 @@ public function useCurl(): self
5255
public function useGuzzle(): self
5356
{
5457
$this->mode = 'guzzle';
58+
$this->modeAuto = false;
5559

5660
return $this;
5761
}
@@ -103,6 +107,10 @@ public function asForm(): self
103107
public function send(): self
104108
{
105109
try {
110+
if ($this->modeAuto) {
111+
$this->mode = config('notifier.client');
112+
}
113+
106114
if ($this->mode === 'stream') {
107115
$this->stream();
108116
} elseif ($this->mode === 'curl') {

tests/NotifierDiscordTest.php

+24-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,30 @@
1919
expect($facade)->toBeInstanceOf(NotifierNotifier::class);
2020
});
2121

22-
it('can use discord', function () {
22+
it('can use clients', function () {
23+
Config::set('notifier.client', 'stream');
24+
25+
$notifier = Notifier::discord()
26+
->message('Hello, Discord!')
27+
->send();
28+
expect($notifier->isSuccess())->toBeTrue();
29+
30+
Config::set('notifier.client', 'curl');
31+
32+
$notifier = Notifier::discord()
33+
->message('Hello, Discord!')
34+
->send();
35+
expect($notifier->isSuccess())->toBeTrue();
36+
37+
Config::set('notifier.client', 'guzzle');
38+
39+
$notifier = Notifier::discord()
40+
->message('Hello, Discord!')
41+
->send();
42+
expect($notifier->isSuccess())->toBeTrue();
43+
});
44+
45+
it('can use', function () {
2346
$notifier = Notifier::discord()
2447
->message('Hello, Discord!')
2548
->user('Notifier', 'https://raw.githubusercontent.com/kiwilan/notifier-laravel/main/docs/banner.jpg')

tests/NotifierMailTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
Config::set('notifier.mail.to.name', dotenv()['NOTIFIER_MAIL_TO_NAME']);
1818
});
1919

20-
it('can use mail', function () {
20+
it('can use', function () {
2121
$notifier = Notifier::mail()
2222
->subject('Hello, Mail!')
2323
->message('Hello, Mail!')

0 commit comments

Comments
 (0)