master

laravel/framework

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

QueuedClosure.php

TLDR

The file QueuedClosure.php contains the definition of the QueuedClosure class in the Illuminate\Events namespace. This class is used as a queued closure event listener resolver. It allows for setting up a closure that will be executed in a queued manner, with options for specifying the connection, queue, and delay of the job. It also provides a method for specifying a callback to be invoked if the queued listener job fails.

Methods

__construct

Constructor method that initializes the QueuedClosure instance with a closure.

onConnection

Sets the desired connection for the job.

onQueue

Sets the desired queue for the job.

delay

Sets the desired delay in seconds for the job.

catch

Specifies a callback that should be invoked if the queued listener job fails.

resolve

Resolves the actual event listener callback. This method returns a closure that dispatches a CallQueuedListener job with the serialized closure and other parameters.

Classes

No additional classes in the file.

<?php

namespace Illuminate\Events;

use Closure;
use Laravel\SerializableClosure\SerializableClosure;

class QueuedClosure
{
    /**
     * The underlying Closure.
     *
     * @var \Closure
     */
    public $closure;

    /**
     * The name of the connection the job should be sent to.
     *
     * @var string|null
     */
    public $connection;

    /**
     * The name of the queue the job should be sent to.
     *
     * @var string|null
     */
    public $queue;

    /**
     * The number of seconds before the job should be made available.
     *
     * @var \DateTimeInterface|\DateInterval|int|null
     */
    public $delay;

    /**
     * All of the "catch" callbacks for the queued closure.
     *
     * @var array
     */
    public $catchCallbacks = [];

    /**
     * Create a new queued closure event listener resolver.
     *
     * @param  \Closure  $closure
     * @return void
     */
    public function __construct(Closure $closure)
    {
        $this->closure = $closure;
    }

    /**
     * Set the desired connection for the job.
     *
     * @param  string|null  $connection
     * @return $this
     */
    public function onConnection($connection)
    {
        $this->connection = $connection;

        return $this;
    }

    /**
     * Set the desired queue for the job.
     *
     * @param  string|null  $queue
     * @return $this
     */
    public function onQueue($queue)
    {
        $this->queue = $queue;

        return $this;
    }

    /**
     * Set the desired delay in seconds for the job.
     *
     * @param  \DateTimeInterface|\DateInterval|int|null  $delay
     * @return $this
     */
    public function delay($delay)
    {
        $this->delay = $delay;

        return $this;
    }

    /**
     * Specify a callback that should be invoked if the queued listener job fails.
     *
     * @param  \Closure  $closure
     * @return $this
     */
    public function catch(Closure $closure)
    {
        $this->catchCallbacks[] = $closure;

        return $this;
    }

    /**
     * Resolve the actual event listener callback.
     *
     * @return \Closure
     */
    public function resolve()
    {
        return function (...$arguments) {
            dispatch(new CallQueuedListener(InvokeQueuedClosure::class, 'handle', [
                'closure' => new SerializableClosure($this->closure),
                'arguments' => $arguments,
                'catch' => collect($this->catchCallbacks)->map(function ($callback) {
                    return new SerializableClosure($callback);
                })->all(),
            ]))->onConnection($this->connection)->onQueue($this->queue)->delay($this->delay);
        };
    }
}