master

laravel/framework

Last updated at: 29/12/2023 09:25

SqsConnector.php

TLDR

The SqsConnector.php file is a part of the Illuminate\Queue\Connectors namespace in the Demo Projects project. It contains a class called SqsConnector which implements the ConnectorInterface. The class has two methods: connect and getDefaultConfiguration.

Methods

connect

This method is responsible for establishing a connection to the queue. It takes an array of configuration options as an argument and returns an instance of the SqsQueue class. The method first applies default values to the configuration options, and then creates a new instance of the SqsQueue class with the provided configuration.

getDefaultConfiguration

This method returns the default configuration options for the SQS (Simple Queue Service). It takes an array of configuration options as an argument and merges it with the default values.

<?php

namespace Illuminate\Queue\Connectors;

use Aws\Sqs\SqsClient;
use Illuminate\Queue\SqsQueue;
use Illuminate\Support\Arr;

class SqsConnector implements ConnectorInterface
{
    /**
     * Establish a queue connection.
     *
     * @param  array  $config
     * @return \Illuminate\Contracts\Queue\Queue
     */
    public function connect(array $config)
    {
        $config = $this->getDefaultConfiguration($config);

        if (! empty($config['key']) && ! empty($config['secret'])) {
            $config['credentials'] = Arr::only($config, ['key', 'secret', 'token']);
        }

        return new SqsQueue(
            new SqsClient(
                Arr::except($config, ['token'])
            ),
            $config['queue'],
            $config['prefix'] ?? '',
            $config['suffix'] ?? '',
            $config['after_commit'] ?? null
        );
    }

    /**
     * Get the default configuration for SQS.
     *
     * @param  array  $config
     * @return array
     */
    protected function getDefaultConfiguration(array $config)
    {
        return array_merge([
            'version' => 'latest',
            'http' => [
                'timeout' => 60,
                'connect_timeout' => 60,
            ],
        ], $config);
    }
}