Skip to content

Commit

Permalink
SOL-185: Add SQS channel (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
datashaman authored Mar 5, 2024
1 parent 06767eb commit 5a5734a
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 33 deletions.
12 changes: 8 additions & 4 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
; This file is for unifying the coding style for different editors and IDEs.
; More information at http://editorconfig.org

# EditorConfig is awesome: http://EditorConfig.org
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = space
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[{Makefile,*.mak}]
indent_style = tab

[*.md]
trim_trailing_whitespace = false

[{Makefile,*.mak,*.sh,*.yml}]
indent_size = 2
1 change: 1 addition & 0 deletions .envrc.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
layout php
12 changes: 2 additions & 10 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,10 +1,2 @@
# Path-based git attributes
# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html

# Ignore all test and documentation with "export-ignore".
/.gitattributes export-ignore
/.gitignore export-ignore
/.travis.yml export-ignore
/phpunit.xml.dist export-ignore
/.scrutinizer.yml export-ignore
/tests export-ignore
* text=auto eol=lf whitespace=tab-in-indent,blank-at-eol,tabwidth=4
makefile text whitespace=-tab-in-indent,blank-at-eol,tabwidth=4
48 changes: 48 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Build

on:
pull_request:

jobs:
test:
strategy:
matrix:
illuminate-version:
- 9
- 10
php-version:
- 8.0
- 8.1
- 8.2
exclude:
- illuminate-version: 10
php-version: 8.0

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
tools: composer:v2

- name: Install dependencies
run: |
composer require \
--no-ansi \
--no-interaction \
--no-progress \
--no-scripts \
--prefer-dist \
illuminate/support:^${{ matrix.illuminate-version }}
- name: Lint Code
run: vendor/bin/tlint

- name: Run tests
run: vendor/bin/phpunit
9 changes: 4 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/.envrc
/.idea/
/.phpunit.result.cache
/composer.lock
/vendor
build
composer.lock
composer.phar
.idea/
.phpunit.result.cache
23 changes: 10 additions & 13 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,25 @@
}
],
"require": {
"php": ">=7.2 || ^8.0 || ^8.1",
"php": "^8",
"ext-json": "*",
"ext-pcre": "*",
"ext-simplexml": "*",
"guzzlehttp/guzzle": "^5.3 || ^6 || ^7",
"guzzlehttp/promises": "~1.0",
"guzzlehttp/psr7": "^1.4.1",
"illuminate/notifications": "^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0",
"illuminate/support": "^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0",
"ramsey/uuid": "^3.7 || ^4.1"
"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": "*",
"behat/behat": "~3.0",
"doctrine/cache": "~1.4",
"mockery/mockery": "^1.0",
"phpunit/phpunit": "^8.0 || ^9.5",
"psr/cache": "^1.0",
"psr/simple-cache": "^1.0"
"mockery/mockery": "^1",
"phpunit/phpunit": "^9.5",
"tightenco/tlint": "^8 || ^9"
},
"extra": {
"include_files": [
Expand Down
2 changes: 1 addition & 1 deletion src/ServiceBusEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ public function getParams(): array
return [
'events' => [$this->eventType],
'reference' => $this->reference,
'from' => $this->config['node_id'],
'from' => $this->config['from'] ?? $this->config['node_id'],
'created_at' => $this->createdAt->toISOString(),
'version' => $this->config['version'],
'route' => $this->route,
Expand Down
70 changes: 70 additions & 0 deletions src/ServiceBusSQSChannel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Ringierimu\ServiceBusNotificationsChannel;

use Aws\Sqs\SqsClient;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;

class ServiceBusSQSChannel
{
protected SqsClient $sqs;

public function __construct(array $config = [])
{
$this->config = $config ?: config('services.service_bus');

$this->sqs = new SqsClient([
'region' => Arr::get($this->config, 'sqs.region'),
'version' => 'latest',
'credentials' => [
'key' => Arr::get($this->config, 'sqs.key'),
'secret' => Arr::get($this->config, 'sqs.secret'),
],
]);
}

public function send($notifiable, Notification $notification)
{
/** @var ServiceBusEvent $event */
$event = $notification->toServiceBus($notifiable);
$eventType = $event->getEventType();
$params = $event->getParams();
$dontReport = Arr::get($this->config, 'dont_report', []);

if (Arr::get($this->config, 'enabled') == false) {
if (!in_array($eventType, $dontReport)) {
Log::debug(
"$eventType service bus notification [disabled]",
[
'event' => $eventType,
'params' => $params,
'tags' => [
'service-bus',
],
]
);
}

return;
}

$message = $notification
->toServiceBus($notifiable)
->getParams();

$response = $this->sqs->sendMessage([
'QueueUrl' => Arr::get($this->config, 'sqs.queue_url'),
'MessageBody' => json_encode($message),
'MessageGroupId' => $message['from'],
]);

$event = $message['events'][0];

Log::info("{$event} sent to bus queue", [
'message_id' => $response->get('MessageId'),
'message' => $message,
]);
}
}
10 changes: 10 additions & 0 deletions tlint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"preset": "laravel",
"disabled": [
"SpaceAfterSoleNotOperator",
"NoParensEmptyInstantiations",
"OneLineBetweenClassVisibilityChanges",
"ModelMethodOrder",
"ConcatenationNoSpacing"
]
}

0 comments on commit 5a5734a

Please sign in to comment.