master

laravel/framework

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

Batchable.php

TLDR

This file contains a trait called Batchable which provides methods for working with job batches. It includes methods for getting the batch instance, checking if the batch is still active, setting the batch ID on the job, and using a fake batch for testing purposes.

Methods

batch

This method returns the batch instance for the job, if applicable. It checks if there is a fake batch set, and if so, returns it. Otherwise, it uses the BatchRepository to find and return the batch associated with the job's batch ID.

batching

This method determines if the batch is still active and processing. It calls the batch method to get the batch instance and checks if the batch is not cancelled.

withBatchId

This method sets the batch ID on the job. It takes a string parameter $batchId and assigns it to the batchId property of the job. It returns the job instance.

withFakeBatch

This method indicates that the job should use a fake batch for testing purposes. It takes multiple parameters to set up the fake batch, including the ID, name, total jobs, pending jobs, failed jobs, failed job IDs, options, creation date, cancellation date, and finish date. It creates a new instance of BatchFake with the provided parameters and assigns it to the fakeBatch property of the job. It returns an array containing the job instance and the fake batch instance.

Classes

None

<?php

namespace Illuminate\Bus;

use Carbon\CarbonImmutable;
use Illuminate\Container\Container;
use Illuminate\Support\Str;
use Illuminate\Support\Testing\Fakes\BatchFake;

trait Batchable
{
    /**
     * The batch ID (if applicable).
     *
     * @var string
     */
    public $batchId;

    /**
     * The fake batch, if applicable.
     *
     * @var \Illuminate\Support\Testing\Fakes\BatchFake
     */
    private $fakeBatch;

    /**
     * Get the batch instance for the job, if applicable.
     *
     * @return \Illuminate\Bus\Batch|null
     */
    public function batch()
    {
        if ($this->fakeBatch) {
            return $this->fakeBatch;
        }

        if ($this->batchId) {
            return Container::getInstance()->make(BatchRepository::class)->find($this->batchId);
        }
    }

    /**
     * Determine if the batch is still active and processing.
     *
     * @return bool
     */
    public function batching()
    {
        $batch = $this->batch();

        return $batch && ! $batch->cancelled();
    }

    /**
     * Set the batch ID on the job.
     *
     * @param  string  $batchId
     * @return $this
     */
    public function withBatchId(string $batchId)
    {
        $this->batchId = $batchId;

        return $this;
    }

    /**
     * Indicate that the job should use a fake batch.
     *
     * @param  string  $id
     * @param  string  $name
     * @param  int  $totalJobs
     * @param  int  $pendingJobs
     * @param  int  $failedJobs
     * @param  array  $failedJobIds
     * @param  array  $options
     * @param  \Carbon\CarbonImmutable  $createdAt
     * @param  \Carbon\CarbonImmutable|null  $cancelledAt
     * @param  \Carbon\CarbonImmutable|null  $finishedAt
     * @return array{0: $this, 1: \Illuminate\Support\Testing\Fakes\BatchFake}
     */
    public function withFakeBatch(string $id = '',
                                  string $name = '',
                                  int $totalJobs = 0,
                                  int $pendingJobs = 0,
                                  int $failedJobs = 0,
                                  array $failedJobIds = [],
                                  array $options = [],
                                  CarbonImmutable $createdAt = null,
                                  ?CarbonImmutable $cancelledAt = null,
                                  ?CarbonImmutable $finishedAt = null)
    {
        $this->fakeBatch = new BatchFake(
            empty($id) ? (string) Str::uuid() : $id,
            $name,
            $totalJobs,
            $pendingJobs,
            $failedJobs,
            $failedJobIds,
            $options,
            $createdAt ?? CarbonImmutable::now(),
            $cancelledAt,
            $finishedAt,
        );

        return [$this, $this->fakeBatch];
    }
}