|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PerfectOblivion\Services; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Bus\Queueable; |
|
6
|
|
|
use Illuminate\Queue\SerializesModels; |
|
7
|
|
|
use Illuminate\Queue\InteractsWithQueue; |
|
8
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue; |
|
9
|
|
|
use Illuminate\Foundation\Bus\Dispatchable; |
|
10
|
|
|
|
|
11
|
|
|
class QueuedService implements ShouldQueue |
|
12
|
|
|
{ |
|
13
|
|
|
use Queueable, SerializesModels, InteractsWithQueue, Dispatchable; |
|
14
|
|
|
|
|
15
|
|
|
/** @var string */ |
|
16
|
|
|
protected $serviceClass; |
|
17
|
|
|
|
|
18
|
|
|
/** @var array */ |
|
19
|
|
|
protected $parameters; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Construct a new QueuedService. |
|
23
|
|
|
* |
|
24
|
|
|
* @param mixed $service |
|
25
|
|
|
* @param array $parameters |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct($service, array $parameters) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->serviceClass = get_class($service); |
|
30
|
|
|
$this->parameters = $parameters; |
|
31
|
|
|
$this->resolveQueueableProperties($service); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Get the display name for the class. |
|
36
|
|
|
* |
|
37
|
|
|
* @return string |
|
38
|
|
|
*/ |
|
39
|
|
|
public function displayName() |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->serviceClass; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Handle the QueuedService. |
|
46
|
|
|
*/ |
|
47
|
|
|
public function handle() |
|
48
|
|
|
{ |
|
49
|
|
|
$service = app($this->serviceClass); |
|
50
|
|
|
$method = config('service-classes.method', 'run'); |
|
51
|
|
|
|
|
52
|
|
|
$service->{$method}(...$this->parameters); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* The tags for identifying the queued service. |
|
57
|
|
|
* |
|
58
|
|
|
* @return array |
|
59
|
|
|
*/ |
|
60
|
|
|
public function tags() |
|
61
|
|
|
{ |
|
62
|
|
|
return ['queued_service']; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Resolve the queable properties. |
|
67
|
|
|
* |
|
68
|
|
|
* @param mixed $service |
|
69
|
|
|
*/ |
|
70
|
|
|
protected function resolveQueueableProperties($service) |
|
71
|
|
|
{ |
|
72
|
|
|
$queueableProperties = [ |
|
73
|
|
|
'connection', |
|
74
|
|
|
'queue', |
|
75
|
|
|
'chainConnection', |
|
76
|
|
|
'chainQueue', |
|
77
|
|
|
'delay', |
|
78
|
|
|
'chained', |
|
79
|
|
|
]; |
|
80
|
|
|
|
|
81
|
|
|
foreach ($queueableProperties as $queueableProperty) { |
|
82
|
|
|
$this->{$queueableProperty} = $service->{$queueableProperty} ?? $this->{$queueableProperty}; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|