Skip to content

Commit 8a0db14

Browse files
authored
Merge pull request #4 from descom-es/events
Notification Manager Send, change throws by events
2 parents 68015e8 + 23524a8 commit 8a0db14

File tree

3 files changed

+55
-15
lines changed

3 files changed

+55
-15
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace DescomLib\Services\NotificationManager\Events;
4+
5+
use Exception;
6+
7+
class NotificationFailed
8+
{
9+
public $exception;
10+
11+
public $data;
12+
13+
14+
public function __construct(array $data, Exception $exception)
15+
{
16+
$this->data = $data;
17+
18+
$this->exception = $exception;
19+
}
20+
}

src/Services/NotificationManager/NotificationManager.php

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use GuzzleHttp\Exception\RequestException;
77
use DescomLib\Exceptions\PermanentException;
88
use DescomLib\Exceptions\TemporaryException;
9+
use DescomLib\Services\NotificationManager\Events\NotificationFailed;
10+
use Illuminate\Support\Facades\Event;
911

1012
class NotificationManager
1113
{
@@ -37,18 +39,15 @@ public function setClient(Client $client)
3739
}
3840

3941
/**
40-
* Send request
42+
* Send request to Notification Manager service
4143
*
4244
* @param array $data
43-
* @throws DescomLib\Exceptions\TemporaryException
44-
* @throws DescomLib\Exceptions\PermanentException
45-
* @return object
45+
* @return object|null
4646
*/
47-
public function send($data)
47+
public function send(array $data): ?object
4848
{
4949
try {
50-
$response = $this->client->request(
51-
'POST',
50+
$response = $this->client->post(
5251
$this->url,
5352
[
5453
'headers' => [
@@ -66,12 +65,23 @@ public function send($data)
6665
}
6766

6867
if ($response->getStatusCode() == 503) {
69-
throw new TemporaryException("Temporal error", 503);
68+
Event::dispatch(new NotificationFailed(
69+
$data,
70+
new TemporaryException("Temporal error", $response->getStatusCode())
71+
));
72+
} else {
73+
Event::dispatch(new NotificationFailed(
74+
$data,
75+
new PermanentException("Permanent error", $response->getStatusCode())
76+
));
7077
}
71-
72-
throw new PermanentException("Permanent error", $response->getStatusCode());
7378
} catch (RequestException $e) {
74-
throw new TemporaryException($e->getMessage(), $e->getCode());
79+
Event::dispatch(new NotificationFailed(
80+
$data,
81+
new TemporaryException($e->getMessage(), $e->getCode())
82+
));
7583
}
84+
85+
return null;
7686
}
7787
}

tests/Services/NotificationManager/NotificationManagerTest.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
use GuzzleHttp\Handler\MockHandler;
1010
use DescomLib\Exceptions\PermanentException;
1111
use DescomLib\Exceptions\TemporaryException;
12+
use DescomLib\Services\NotificationManager\Events\NotificationFailed;
1213
use DescomLib\Services\NotificationManager\NotificationManager;
14+
use Illuminate\Support\Facades\Event;
1315

1416
class NotificationManagerTest extends TestCase
1517
{
@@ -34,7 +36,7 @@ public function testLoggedEmail()
3436

3537
public function testLoggedEmailPermanentException()
3638
{
37-
$this->expectException(PermanentException::class);
39+
Event::fake();
3840

3941
$data = [];
4042

@@ -45,12 +47,16 @@ public function testLoggedEmailPermanentException()
4547
$notificationManager = new NotificationManager;
4648
$notificationManager->setClient($client);
4749

48-
$response = $notificationManager->send($data);
50+
$notificationManager->send($data);
51+
52+
Event::assertDispatched(NotificationFailed::class, function (NotificationFailed $event) {
53+
return $event->exception instanceof PermanentException;
54+
});
4955
}
5056

5157
public function testLoggedEmailTemporaryException()
5258
{
53-
$this->expectException(TemporaryException::class);
59+
Event::fake();
5460

5561
$data = [];
5662

@@ -61,6 +67,10 @@ public function testLoggedEmailTemporaryException()
6167
$notificationManager = new NotificationManager;
6268
$notificationManager->setClient($client);
6369

64-
$response = $notificationManager->send($data);
70+
$notificationManager->send($data);
71+
72+
Event::assertDispatched(NotificationFailed::class, function (NotificationFailed $event) {
73+
return $event->exception instanceof TemporaryException;
74+
});
6575
}
6676
}

0 commit comments

Comments
 (0)