Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen Revision | Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Bus;
4
 
5
use Carbon\CarbonImmutable;
6
use Illuminate\Container\Container;
7
use Illuminate\Support\Str;
8
use Illuminate\Support\Testing\Fakes\BatchFake;
9
 
10
trait Batchable
11
{
12
    /**
13
     * The batch ID (if applicable).
14
     *
15
     * @var string
16
     */
17
    public $batchId;
18
 
19
    /**
20
     * The fake batch, if applicable.
21
     *
22
     * @var \Illuminate\Support\Testing\BatchFake
23
     */
24
    private $fakeBatch;
25
 
26
    /**
27
     * Get the batch instance for the job, if applicable.
28
     *
29
     * @return \Illuminate\Bus\Batch|null
30
     */
31
    public function batch()
32
    {
33
        if ($this->fakeBatch) {
34
            return $this->fakeBatch;
35
        }
36
 
37
        if ($this->batchId) {
38
            return Container::getInstance()->make(BatchRepository::class)->find($this->batchId);
39
        }
40
    }
41
 
42
    /**
43
     * Determine if the batch is still active and processing.
44
     *
45
     * @return bool
46
     */
47
    public function batching()
48
    {
49
        $batch = $this->batch();
50
 
51
        return $batch && ! $batch->cancelled();
52
    }
53
 
54
    /**
55
     * Set the batch ID on the job.
56
     *
57
     * @param  string  $batchId
58
     * @return $this
59
     */
60
    public function withBatchId(string $batchId)
61
    {
62
        $this->batchId = $batchId;
63
 
64
        return $this;
65
    }
66
 
67
    /**
68
     * Indicate that the job should use a fake batch.
69
     *
70
     * @param  string  $id
71
     * @param  string  $name
72
     * @param  int  $totalJobs
73
     * @param  int  $pendingJobs
74
     * @param  int  $failedJobs
75
     * @param  array  $failedJobIds
76
     * @param  array  $options
77
     * @param  \Carbon\CarbonImmutable  $createdAt
78
     * @param  \Carbon\CarbonImmutable|null  $cancelledAt
79
     * @param  \Carbon\CarbonImmutable|null  $finishedAt
80
     * @return array{0: $this, 1: \Illuminate\Support\Testing\BatchFake}
81
     */
82
    public function withFakeBatch(string $id = '',
83
                                  string $name = '',
84
                                  int $totalJobs = 0,
85
                                  int $pendingJobs = 0,
86
                                  int $failedJobs = 0,
87
                                  array $failedJobIds = [],
88
                                  array $options = [],
89
                                  CarbonImmutable $createdAt = null,
90
                                  ?CarbonImmutable $cancelledAt = null,
91
                                  ?CarbonImmutable $finishedAt = null)
92
    {
93
        $this->fakeBatch = new BatchFake(
94
            empty($id) ? (string) Str::uuid() : $id,
95
            $name,
96
            $totalJobs,
97
            $pendingJobs,
98
            $failedJobs,
99
            $failedJobIds,
100
            $options,
101
            $createdAt ?? CarbonImmutable::now(),
102
            $cancelledAt,
103
            $finishedAt,
104
        );
105
 
106
        return [$this, $this->fakeBatch];
107
    }
108
}