Skip to content

Commit cf96dc5

Browse files
authored
Merge pull request #119 from 4irik/dispatch-event-on-exception
2 parents c489bb8 + 9b5f581 commit cf96dc5

File tree

4 files changed

+80
-15
lines changed

4 files changed

+80
-15
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"php": "^7.2 || ^8.0",
2323
"ext-json": "*",
2424
"guzzlehttp/guzzle": "^6.2 || ^7.0",
25+
"illuminate/contracts": "^5.5 || ^6.0 || ^7.0 || ^8.0",
2526
"illuminate/notifications": "^5.5 || ^6.0 || ^7.0 || ^8.0",
2627
"illuminate/support": "^5.5 || ^6.0 || ^7.0 || ^8.0"
2728
},

src/TelegramChannel.php

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace NotificationChannels\Telegram;
44

5+
use Illuminate\Contracts\Events\Dispatcher;
6+
use Illuminate\Notifications\Events\NotificationFailed;
57
use Illuminate\Notifications\Notification;
68
use NotificationChannels\Telegram\Exceptions\CouldNotSendNotification;
79

@@ -15,14 +17,21 @@ class TelegramChannel
1517
*/
1618
protected $telegram;
1719

20+
/**
21+
* @var Dispatcher
22+
*/
23+
private $dispatcher;
24+
1825
/**
1926
* Channel constructor.
2027
*
2128
* @param Telegram $telegram
29+
* @param Dispatcher $dispatcher
2230
*/
23-
public function __construct(Telegram $telegram)
31+
public function __construct(Telegram $telegram, Dispatcher $dispatcher)
2432
{
2533
$this->telegram = $telegram;
34+
$this->dispatcher = $dispatcher;
2635
}
2736

2837
/**
@@ -56,16 +65,28 @@ public function send($notifiable, Notification $notification): ?array
5665

5766
$params = $message->toArray();
5867

59-
if ($message instanceof TelegramMessage) {
60-
$response = $this->telegram->sendMessage($params);
61-
} elseif ($message instanceof TelegramLocation) {
62-
$response = $this->telegram->sendLocation($params);
63-
} elseif ($message instanceof TelegramFile) {
64-
$response = $this->telegram->sendFile($params, $message->type, $message->hasFile());
65-
} else {
66-
return null;
68+
try{
69+
if ($message instanceof TelegramMessage) {
70+
$response = $this->telegram->sendMessage($params);
71+
} elseif ($message instanceof TelegramLocation) {
72+
$response = $this->telegram->sendLocation($params);
73+
} elseif ($message instanceof TelegramFile) {
74+
$response = $this->telegram->sendFile($params, $message->type, $message->hasFile());
75+
} else {
76+
return null;
77+
}
78+
} catch (CouldNotSendNotification $exception) {
79+
$this->dispatcher->dispatch(new NotificationFailed(
80+
$notifiable,
81+
$notification,
82+
'telegram',
83+
[]
84+
));
85+
86+
throw $exception;
6787
}
6888

89+
6990
return json_decode($response->getBody()->getContents(), true);
7091
}
7192
}

src/TelegramServiceProvider.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace NotificationChannels\Telegram;
44

55
use GuzzleHttp\Client as HttpClient;
6+
use Illuminate\Contracts\Events\Dispatcher;
67
use Illuminate\Notifications\ChannelManager;
78
use Illuminate\Support\Facades\Notification;
89
use Illuminate\Support\ServiceProvider;
@@ -35,11 +36,14 @@ public function register()
3536
{
3637
Notification::resolved(function (ChannelManager $service) {
3738
$service->extend('telegram', function ($app) {
38-
return new TelegramChannel(new Telegram(
39-
config('services.telegram-bot-api.token'),
40-
app(HttpClient::class),
41-
config('services.telegram-bot-api.base_uri')
42-
));
39+
return new TelegramChannel(
40+
new Telegram(
41+
config('services.telegram-bot-api.token'),
42+
app(HttpClient::class),
43+
config('services.telegram-bot-api.base_uri')
44+
),
45+
app(Dispatcher::class)
46+
);
4347
});
4448
});
4549
}

tests/TelegramChannelTest.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
namespace NotificationChannels\Telegram\Test;
44

55
use GuzzleHttp\Psr7\Response;
6+
use Illuminate\Contracts\Events\Dispatcher;
7+
use Illuminate\Notifications\Events\NotificationFailed;
68
use Illuminate\Notifications\Notifiable;
79
use Illuminate\Notifications\Notification;
810
use Mockery;
11+
use NotificationChannels\Telegram\Exceptions\CouldNotSendNotification;
912
use NotificationChannels\Telegram\Telegram;
1013
use NotificationChannels\Telegram\TelegramChannel;
1114
use NotificationChannels\Telegram\TelegramMessage;
@@ -19,14 +22,18 @@ class TelegramChannelTest extends TestCase
1922
/** @var Mockery\Mock */
2023
protected $telegram;
2124

25+
/** @var \PHPUnit\Framework\MockObject\MockObject */
26+
private $dispatcher;
27+
2228
/** @var TelegramChannel */
2329
protected $channel;
2430

2531
public function setUp(): void
2632
{
2733
parent::setUp();
2834
$this->telegram = Mockery::mock(Telegram::class);
29-
$this->channel = new TelegramChannel($this->telegram);
35+
$this->dispatcher = $this->createMock(Dispatcher::class);
36+
$this->channel = new TelegramChannel($this->telegram, $this->dispatcher);
3037
}
3138

3239
public function tearDown(): void
@@ -51,6 +58,38 @@ public function it_can_send_a_message(): void
5158

5259
self::assertSame($expectedResponse, $actualResponse);
5360
}
61+
62+
/**
63+
* @test
64+
*/
65+
public function notification_failed_event(): void
66+
{
67+
self::expectException($exception_class = CouldNotSendNotification::class);
68+
self::expectExceptionMessage($exception_message = 'Some exception');
69+
70+
$notifiable = new TestNotifiable();
71+
$notification = new TestNotification();
72+
73+
$this->telegram
74+
->shouldReceive('sendMessage')
75+
->andThrow($exception_class , $exception_message)
76+
;
77+
78+
$this->dispatcher
79+
->expects($this->once())
80+
->method('dispatch')
81+
->with(
82+
new NotificationFailed(
83+
$notifiable,
84+
$notification,
85+
'telegram',
86+
[]
87+
)
88+
)
89+
;
90+
91+
$this->channel->send($notifiable, $notification);
92+
}
5493
}
5594

5695
/**

0 commit comments

Comments
 (0)