Completed
Pull Request — master (#26)
by Ruben
01:25
created

ActionJob::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Spatie\QueueableAction;
4
5
use Illuminate\Bus\Queueable;
6
use Illuminate\Contracts\Queue\ShouldQueue;
7
use Illuminate\Foundation\Bus\Dispatchable;
8
use Illuminate\Queue\InteractsWithQueue;
9
use Illuminate\Queue\SerializesModels;
10
11
class ActionJob implements ShouldQueue
12
{
13
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
14
15
    /** @var string */
16
    protected $actionClass;
17
18
    /** @var array */
19
    protected $parameters;
20
21
    /** @var array */
22
    protected $tags = ['action_job'];
23
24
    public function __construct($action, array $parameters = [])
25
    {
26
        $this->actionClass = is_string($action) ? $action : get_class($action);
27
        $this->parameters = $parameters;
28
29
        if (is_object($action)) {
30
            $this->tags = $action->tags();
31
            $this->middleware = $action->middleware();
32
        }
33
34
        $this->resolveQueueableProperties($action);
35
    }
36
37
    public function displayName(): string
38
    {
39
        return $this->actionClass;
40
    }
41
42
    public function tags()
43
    {
44
        return $this->tags;
45
    }
46
47
    public function middleware()
48
    {
49
        return $this->middleware;
50
    }
51
52
    public function handle()
53
    {
54
        $action = app($this->actionClass);
55
56
        $action->execute(...$this->parameters);
57
    }
58
59
    protected function resolveQueueableProperties($action)
60
    {
61
        $queueableProperties = [
62
            'connection',
63
            'queue',
64
            'chainConnection',
65
            'chainQueue',
66
            'delay',
67
            'chained',
68
            'tries',
69
            'timeout',
70
        ];
71
72
        foreach ($queueableProperties as $queueableProperty) {
73
            if (property_exists($action, $queueableProperty)) {
74
                $this->{$queueableProperty} = $action->{$queueableProperty};
75
            }
76
        }
77
    }
78
}
79