PHP code example of our-edu / laravel-sqs-messaging
1. Go to this page and download the library: Download our-edu/laravel-sqs-messaging library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
public static function publishFromInstance(object $event): void
{
Container::getInstance()
->make(Publisher::class)
->publish($event);
}
declare(strict_types=1);
namespace Support\RabbitMQ;
use Illuminate\Container\Container;
use Illuminate\Contracts\Container\BindingResolutionException;
trait Publishable
{
/**
* @return void
* @throws BindingResolutionException
*/
public static function publish(): void
{
Container::getInstance()
->make(Publisher::class)
->publish(new static(...func_get_args()));
}
public static function publishFromInstance(object $event): void
{
Container::getInstance()
->make(Publisher::class)
->publish($event);
}
}
Notification::publish($queueName, $payload)
app(MessagingService::class)->publish(
event: new Notification($queueName, $payload),
eventClassReference: Notification::class
);
namespace Domain\Payments\Listeners\ReversePaymentRequest;
use App\BaseApp\Enums\RoleEnum;
use Domain\Models\User;
use Domain\Payments\Events\ReversePaymentRequest\ReversePaymentRequestCreatedEvent;
use Domain\Payments\Notifications\ReversePaymentRequestNotification;
use Illuminate\Contracts\Container\BindingResolutionException;
class ReversePaymentRequestCreatedListener
{
/**
* @throws BindingResolutionException
*/
public function handle(ReversePaymentRequestCreatedEvent $event): void
{
$users = User::query()->whereHas('roles', function ($query) {
$query->whereIn('name', [RoleEnum::ACCOUNTANT_MANAGER, RoleEnum::ACCOUNTANT_MANAGER]);
})->get();
$usersUuids = $users->pluck('uuid')->toArray();
ReversePaymentRequestNotification::publish($event->reversePaymentRequest, $usersUuids);
}
}
namespace Domain\Payments\Listeners\ReversePaymentRequest;
use App\BaseApp\Enums\RoleEnum;
use Domain\Models\User;
use Domain\Payments\Events\ReversePaymentRequest\ReversePaymentRequestCreatedEvent;
use Domain\Payments\Notifications\ReversePaymentRequestNotification;
use Illuminate\Contracts\Container\BindingResolutionException;
use OurEdu\SqsMessaging\MessagingService;
class ReversePaymentRequestCreatedListener
{
/**
* @throws BindingResolutionException
*/
public function handle(ReversePaymentRequestCreatedEvent $event): void
{
$users = User::query()->whereHas('roles', function ($query) {
$query->whereIn('name', [RoleEnum::ACCOUNTANT_MANAGER, RoleEnum::ACCOUNTANT_MANAGER]);
})->get();
$usersUuids = $users->pluck('uuid')->toArray();
// New messaging service publish
app(MessagingService::class)->publish(
event: new ReversePaymentRequestNotification($event->reversePaymentRequest, $usersUuids), // HINT: Use The Same Payload Constructor Parameters
eventClassReference: ReversePaymentRequestNotification::class
);
}
}
use App\Events\StudentEnrolled;
use OurEdu\SqsMessaging\Drivers\Sqs\SQSPublisherAdapter;
$adapter = app(SQSPublisherAdapter::class);
$adapter->publish(new StudentEnrolled($student), 'payment-service-queue');