Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Foundation\Bus;
4
 
5
use Closure;
6
use Illuminate\Contracts\Bus\Dispatcher;
7
use Illuminate\Queue\CallQueuedClosure;
8
use Laravel\SerializableClosure\SerializableClosure;
9
 
10
class PendingChain
11
{
12
    /**
13
     * The class name of the job being dispatched.
14
     *
15
     * @var mixed
16
     */
17
    public $job;
18
 
19
    /**
20
     * The jobs to be chained.
21
     *
22
     * @var array
23
     */
24
    public $chain;
25
 
26
    /**
27
     * The name of the connection the chain should be sent to.
28
     *
29
     * @var string|null
30
     */
31
    public $connection;
32
 
33
    /**
34
     * The name of the queue the chain should be sent to.
35
     *
36
     * @var string|null
37
     */
38
    public $queue;
39
 
40
    /**
41
     * The number of seconds before the chain should be made available.
42
     *
43
     * @var \DateTimeInterface|\DateInterval|int|null
44
     */
45
    public $delay;
46
 
47
    /**
48
     * The callbacks to be executed on failure.
49
     *
50
     * @var array
51
     */
52
    public $catchCallbacks = [];
53
 
54
    /**
55
     * Create a new PendingChain instance.
56
     *
57
     * @param  mixed  $job
58
     * @param  array  $chain
59
     * @return void
60
     */
61
    public function __construct($job, $chain)
62
    {
63
        $this->job = $job;
64
        $this->chain = $chain;
65
    }
66
 
67
    /**
68
     * Set the desired connection for the job.
69
     *
70
     * @param  string|null  $connection
71
     * @return $this
72
     */
73
    public function onConnection($connection)
74
    {
75
        $this->connection = $connection;
76
 
77
        return $this;
78
    }
79
 
80
    /**
81
     * Set the desired queue for the job.
82
     *
83
     * @param  string|null  $queue
84
     * @return $this
85
     */
86
    public function onQueue($queue)
87
    {
88
        $this->queue = $queue;
89
 
90
        return $this;
91
    }
92
 
93
    /**
94
     * Set the desired delay in seconds for the chain.
95
     *
96
     * @param  \DateTimeInterface|\DateInterval|int|null  $delay
97
     * @return $this
98
     */
99
    public function delay($delay)
100
    {
101
        $this->delay = $delay;
102
 
103
        return $this;
104
    }
105
 
106
    /**
107
     * Add a callback to be executed on job failure.
108
     *
109
     * @param  callable  $callback
110
     * @return $this
111
     */
112
    public function catch($callback)
113
    {
114
        $this->catchCallbacks[] = $callback instanceof Closure
115
                        ? new SerializableClosure($callback)
116
                        : $callback;
117
 
118
        return $this;
119
    }
120
 
121
    /**
122
     * Get the "catch" callbacks that have been registered.
123
     *
124
     * @return array
125
     */
126
    public function catchCallbacks()
127
    {
128
        return $this->catchCallbacks ?? [];
129
    }
130
 
131
    /**
132
     * Dispatch the job with the given arguments.
133
     *
134
     * @return \Illuminate\Foundation\Bus\PendingDispatch
135
     */
136
    public function dispatch()
137
    {
138
        if (is_string($this->job)) {
139
            $firstJob = new $this->job(...func_get_args());
140
        } elseif ($this->job instanceof Closure) {
141
            $firstJob = CallQueuedClosure::create($this->job);
142
        } else {
143
            $firstJob = $this->job;
144
        }
145
 
146
        if ($this->connection) {
147
            $firstJob->chainConnection = $this->connection;
148
            $firstJob->connection = $firstJob->connection ?: $this->connection;
149
        }
150
 
151
        if ($this->queue) {
152
            $firstJob->chainQueue = $this->queue;
153
            $firstJob->queue = $firstJob->queue ?: $this->queue;
154
        }
155
 
156
        if ($this->delay) {
157
            $firstJob->delay = ! is_null($firstJob->delay) ? $firstJob->delay : $this->delay;
158
        }
159
 
160
        $firstJob->chain($this->chain);
161
        $firstJob->chainCatchCallbacks = $this->catchCallbacks();
162
 
163
        return app(Dispatcher::class)->dispatch($firstJob);
164
    }
165
}