Skip to content

Commit 90d041f

Browse files
authored
Merge pull request #4 from nipwaayoni/convert-to-events
Convert to events
2 parents 5a9ae21 + 5ca9b2e commit 90d041f

19 files changed

+318
-8036
lines changed

.github/workflows/run-tests.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@ jobs:
99
strategy:
1010
matrix:
1111
php: [7.4, 7.3]
12-
laravel: [8.*, 7.*]
12+
laravel: [8.*]
1313
include:
1414
- laravel: 8.*
1515
testbench: 6.*
16-
- laravel: 7.*
17-
testbench: ^5.2
1816

1917
name: PHP${{ matrix.php }} - L${{ matrix.laravel }}
2018

.gitlab-ci.yml

Lines changed: 0 additions & 17 deletions
This file was deleted.

README.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#Introduction
2+
This package provides an easy way of adding AWS SNS message handling to your Laravel application as a REST endpoint. The package can automatically confirm subscription requests and dispatches events when a message is received.
3+
4+
##How to create an SNS topic
5+
[Instructions for creating an SNS topic in AWS can be found here](https://docs.aws.amazon.com/sns/latest/dg/sns-create-topic.html)
6+
7+
##How to add SNS Handler to your Application
8+
**Note: This package currently requires Laravel >= 8.0. Laravel 7 is no longer supported due to security concerns.**
9+
10+
Use composer to require the package:
11+
12+
```bash
13+
composer require nipwaayoni/laravel-aws-sns
14+
```
15+
16+
##Handling events
17+
Please review the [Laravel documentation on Events.](https://laravel.com/docs/8.x/events) You will need to write a listener to handle the events associated with the ARNs you register. Your listener class must handle the `SnsMessageReceived` event type, which is provided with this package. It should then be registered in the `EventServiceProvider` as mentioned in the Events documentation:
18+
```php
19+
use Nipwaayoni\SnsHandler\Events\SnsMessageReceived;
20+
use App\Listeners\MySnsMessageHandler;
21+
22+
/**
23+
* The event listener mappings for the application.
24+
*
25+
* @var array
26+
*/
27+
protected $listen = [
28+
SnsMessageReceived::class => [
29+
MySnsMessageHandler::class,
30+
],
31+
];
32+
In your listener, the handle method will receive an SnsMessageReceived object which can be used to access the SnsMessage.
33+
34+
public function handle(SnsMessageReceived $event)
35+
{
36+
$message = $event->message();
37+
38+
// do stuff with message
39+
$content = $message->content();
40+
}
41+
```
42+
The message content will always be a string. You are responsible for any deserialization or other steps required to interpret the message content.
43+
44+
##Write a feature test to test the expected SNS feature
45+
The ReceivesSnsMessagesTrait facilitates testing. Use the trait in your test
46+
47+
Sample test:
48+
```php
49+
public function testSnsRequestMapping(): void
50+
{
51+
Bus::fake();
52+
$data = "Bob";
53+
$this->sendSnsMessage($data);
54+
55+
Bus::assertDispatched(SayHelloJob::class);
56+
57+
}
58+
```
59+
Disable message signature validation when sending events from sources other than SNS (and during feature tests). Add to .env:
60+
```
61+
VALIDATE_SNS_MESSAGES=false
62+
```
63+
64+
##How do I send SNS messages to my app?
65+
The SNS message route responds to POST requests and expects content consistent with an SNS message from AWS.
66+
67+
The message content of an SNS message must be provided as a string value. If your payload is something other than a simple string (e.g. an array or some other sort of object), you will need to serialize your data before sending it using something like json_encode().
68+
69+
###POSTing directly to your app
70+
[Amazon has an example of a POST request available here.](https://docs.aws.amazon.com/sns/latest/dg/sns-http-https-endpoint-as-subscriber.html)
71+
You can add your serialized message in the message field. Note that manually POSTed messages cannot be validated (See above to disable message validation). We recommend only attempting this during testing, never in your production environment.
72+
73+
74+
##How to subscribe your endpoint in AWS
75+
[Follow these instructions to subscribe your endpoint using HTTPS](https://docs.aws.amazon.com/sns/latest/dg/sns-http-https-endpoint-as-subscriber.html)
76+
77+
**Note: You will only be able to subscribe your endpoint if it can be reached from the AWS SNS service**
78+

0 commit comments

Comments
 (0)