PHP code example of gabebritto / laravel-simple-sqs

1. Go to this page and download the library: Download gabebritto/laravel-simple-sqs 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/ */

    

gabebritto / laravel-simple-sqs example snippets




return [
    // The default SQS queue URL. If null, it falls back to config/queue.php configurations.
    'queue_url' => env('SQS_MESSENGER_QUEUE_URL'),

    // Map the incoming JSON 'job' alias keys to their respective Job classes
    'handlers' => [
        'external-event' => \App\Jobs\ConsolidateJob::class,
    ],
];

use App\Jobs\ConsolidateJob;

ConsolidateJob::dispatchSqsJson(
    payload: [
        'event' => 'OrderCreated',
        'order_id' => 123,
        'total' => 150.00
    ]
);

$job = new ConsolidateJob();
$job->orderId = 123;
$job->total = 150.00;
$job->event = 'OrderCreated'; // Set event directly as a normal property

$job->publish(); // Dispatches the message to SQS

use Gabebritto\LaravelSimpleSqs\Facades\SqsPublisher;

SqsPublisher::dispatch(
    jobAlias: 'external-event',
    payload: [
        'event' => 'OrderCreated',
        'order_id' => 123,
        'total' => 150.00
    ]
);



namespace App\Jobs;

use Gabebritto\LaravelSimpleSqs\Traits\SqsJsonJob;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\Queueable;
use Illuminate\Queue\SerializesModels;

class ConsolidateJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, SqsJsonJob;

    // These properties are automatically hydrated from the SQS JSON payload keys
    public ?string $event = null;
    public ?int $orderId = null;
    public ?float $total = null;

    /**
     * Execute the job.
     * Supports Dependency Injection through Laravel's Service Container.
     */
    public function handle(PaymentService $paymentService): void
    {
        // 1. Direct access to the auto-hydrated properties
        $orderId = $this->orderId; 
        $total = $this->total;

        // 2. Perform actions based on the event name (which is now just a normal payload key)
        if ($this->event === 'OrderCreated') {
            $paymentService->process($orderId, $total);
        }

        // NOTE: The SqsJsonJob trait automatically deletes the job from the SQS queue
        // once this method successfully completes.
    }
}

$this->app->bind('external-event', \App\Jobs\ConsolidateJob::class);
bash
php artisan vendor:publish --tag=sqs-messenger-config