Skip to content

Commit

Permalink
Update to L11, add readme (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
datashaman authored Oct 25, 2024
1 parent 5a5734a commit ddf27c6
Show file tree
Hide file tree
Showing 10 changed files with 328 additions and 227 deletions.
10 changes: 8 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@ jobs:
illuminate-version:
- 9
- 10
- 11
php-version:
- 8.0
- 8.1
- 8.2
- 8.3
exclude:
- illuminate-version: 10
php-version: 8.0
- illuminate-version: 11
php-version: 8.0
- illuminate-version: 11
php-version: 8.1

runs-on: ubuntu-latest

Expand All @@ -42,7 +48,7 @@ jobs:
illuminate/support:^${{ matrix.illuminate-version }}
- name: Lint Code
run: vendor/bin/tlint
run: composer lint

- name: Run tests
run: vendor/bin/phpunit
run: composer test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/.envrc
/.idea/
/.phpunit.cache/
/.phpunit.result.cache
/composer.lock
/vendor
4 changes: 4 additions & 0 deletions .phpactor.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"$schema": "/phpactor.schema.json",
"language_server_phpstan.enabled": true
}
149 changes: 149 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,150 @@
# Service Bus Notifications Channel

This is a Laravel package that provides notification channels for
sending notifications to the _RingierSA Service Bus_.

## Installation

Install the package into your project via composer:

```bash
composer require ringiersa/service-bus-notifications-channel
```

## Configuration

Add the following to `config/services.php` file:

```php
'service_bus' => [
'enabled' => env('SERVICE_BUS_ENABLED', true),
'from' => env('SERVICE_BUS_FROM'),
'username' => env('SERVICE_BUS_USERNAME'),
'password' => env('SERVICE_BUS_PASSWORD'),
'version' => env('SERVICE_BUS_VERSION', '2.0.0'),
'endpoint' => env('SERVICE_BUS_ENDPOINT', 'https://bus.staging.ritdu.tech/v1/'),
],
```

Add the following to the `.env` file:

```dotenv
SERVICE_BUS_ENABLED=true
SERVICE_BUS_FROM=bus-node-id
SERVICE_BUS_USERNAME=bus-username
SERVICE_BUS_PASSWORD=bus-password
SERVICE_BUS_VERSION=2.0.0
SERVICE_BUS_ENDPOINT=https://bus.staging.ritdu.tech/v1/
```

You can get the `bus-node-id`, `bus-username` and `bus-password` from the _RingierSA_ Service Bus team.

## usage

Add something like the following example to a notification class:

```php
use App\Models\Article;
use Illuminate\Notifications\Notifiable;
use Illuminate\Notifications\Notification;
use Illuminate\Queue\SerializesModels;
use RingierSA\ServiceBusNotificationsChannel\ServiceBusChannel;
use RingierSA\ServiceBusNotificationsChannel\ServiceBusEvent;

class ArticleCreatedNotification extends Notification
{
use SerializesModels;

public function __construct(protected Article $article)
{
//
}

public function toServiceBus(Notifiable $notifiable): ServiceBusEvent
{
return ServiceBusEvent::create('ArticleCreated')
->withAction('user', $this->article->user_id)
->withCulture('en')
->withReference(uniqid())
->withPayload([
'article' => $this->article->toServiceBus(),
]);
}

public function via($notifiable)
{
return [ServiceBusChannel::class];
}
}
```

Then use an anonymous notifiable to send the notification:

```php
use Illuminate\Notifications\AnonymousNotifiable;
use Illuminate\Support\Facades\Notification;

$article = Article::create([
'title' => 'My Article',
'body' => 'This is my article content',
'user_id' => 1,
// ...
]);

(new AnonymousNotifiable)->notify(new MyNotification($article));
```

That will use the `ServiceBusChannel` to send the notification to the _RingierSA_ Service Bus.

## sqs usage

The API endpoint is rate limited, so it's not suitable for high volume notifications.

For high volume notifications, you can send directly to an `SQS` queue in the service bus.

This removes the need to queue it in your app, and provides a more reliable way to send high volume notifications.

Add the following to your config:

```php
'service_bus' => [
'...',
'sqs' => [
'region' => env('SERVICE_BUS_SQS_REGION', 'eu-west-1'),
'queue_url' => env('SERVICE_BUS_SQS_QUEUE_URL'),
'key' => env('SERVICE_BUS_SQS_KEY'),
'secret' => env('SERVICE_BUS_SQS_SECRET'),
],
],
```

Also add the following to the `.env` file:

```dotenv
SERVICE_BUS_SQS_REGION=eu-west-1
SERVICE_BUS_SQS_QUEUE_URL=queue-url
SERVICE_BUS_SQS_KEY=key
SERVICE_BUS_SQS_SECRET=secret
```

The values for `queue-url`, `key` and `secret` can be obtained from the _RingierSA_ Service Bus team.

The next change is to send the service bus notifications via the `ServiceBusSQSChannel` by changing the notification class:

```php
use RingierSA\ServiceBusNotificationsChannel\ServiceBusSQSChannel;

class ArticleCreatedNotification extends Notification
{
// Everything else is the same as before

public function via($notifiable)
{
return [ServiceBusSQSChannel::class];
}
}
```

Now the notification will be sent directly to an `SQS` queue in the service bus, instead of via the API endpoint.

We recommend you do not queue the notification. Send it `afterResponse`, the time to send the notification to `SQS` is minimal.
116 changes: 59 additions & 57 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,61 +1,63 @@
{
"name": "ringierimu/service-bus-notifications-channel",
"description": "Service Bus Notifications Channel",
"homepage": "https://github.com/RingierIMU/service-bus-notifications-channel",
"license": "MIT",
"authors": [
{
"name": "RIMU Core",
"email": "tools@roam.africa",
"homepage": "http://ringier.tech",
"role": "Developer"
}
"name": "ringierimu/service-bus-notifications-channel",
"description": "Service Bus Notifications Channel",
"homepage": "https://github.com/RingierIMU/service-bus-notifications-channel",
"license": "MIT",
"authors": [
{
"name": "RIMU Core",
"email": "tools@roam.africa",
"homepage": "http://ringier.tech",
"role": "Developer"
}
],
"require": {
"php": "^8",
"ext-json": "*",
"ext-pcre": "*",
"ext-simplexml": "*",
"illuminate/notifications": "^9 || ^10 || ^11",
"illuminate/support": "^9 || ^10 || ^11",
"guzzlehttp/guzzle": "^7",
"guzzlehttp/promises": "^2",
"guzzlehttp/psr7": "^1 || ^2"
},
"require-dev": {
"ext-dom": "*",
"ext-openssl": "*",
"ext-pcntl": "*",
"ext-sockets": "*",
"mockery/mockery": "^1",
"phpunit/phpunit": "^9 || ^10 || ^11",
"tightenco/tlint": "^8 || ^9",
"orchestra/testbench": "^7 || ^8 || ^9",
"nunomaduro/collision": "^6 || ^7 || ^8"
},
"extra": {
"include_files": [
"tests/Fixtures/Helpers.php"
],
"require": {
"php": "^8",
"ext-json": "*",
"ext-pcre": "*",
"ext-simplexml": "*",
"guzzlehttp/guzzle": "^7.2",
"guzzlehttp/promises": "^2",
"guzzlehttp/psr7": "^1 || ^2",
"illuminate/notifications": "^9 || ^10",
"illuminate/support": "^9 || ^10",
"ramsey/uuid": "^4"
},
"require-dev": {
"ext-dom": "*",
"ext-openssl": "*",
"ext-pcntl": "*",
"ext-sockets": "*",
"mockery/mockery": "^1",
"phpunit/phpunit": "^9.5",
"tightenco/tlint": "^8 || ^9"
},
"extra": {
"include_files": [
"tests/Fixtures/Helpers.php"
],
"laravel": {
"providers": [
"Ringierimu\\ServiceBusNotificationsChannel\\ServiceBusServiceProvider"
]
}
},
"autoload": {
"psr-4": {
"Ringierimu\\ServiceBusNotificationsChannel\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Ringierimu\\ServiceBusNotificationsChannel\\Tests\\": "tests/"
},
"files": [
"tests/helpers.php"
]
},
"scripts": {
"test": "vendor/bin/phpunit"
"laravel": {
"providers": [
"Ringierimu\\ServiceBusNotificationsChannel\\ServiceBusServiceProvider"
]
}
},
"autoload": {
"psr-4": {
"Ringierimu\\ServiceBusNotificationsChannel\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Ringierimu\\ServiceBusNotificationsChannel\\Tests\\": "tests/"
},
"files": [
"tests/helpers.php"
]
},
"scripts": {
"lint": "vendor/bin/tlint",
"test": "vendor/bin/testbench package:test"
}
}
25 changes: 6 additions & 19 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,23 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
>
<coverage>
<include>
<directory suffix=".php">src/</directory>
</include>
</coverage>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.4/phpunit.xsd" bootstrap="vendor/autoload.php" backupGlobals="false" colors="true" processIsolation="false" stopOnFailure="false" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<testsuites>
<testsuite name="RIMU Service Bus Test Suite">
<directory suffix="Test.php">tests</directory>
Expand All @@ -30,4 +12,9 @@
<env name="QUEUE_CONNECTION" value="sync"/>
<env name="SESSION_DRIVER" value="array"/>
</php>
<source>
<include>
<directory suffix=".php">src/</directory>
</include>
</source>
</phpunit>
Loading

0 comments on commit ddf27c6

Please sign in to comment.