ActionJob   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 4
dl 0
loc 76
rs 10
c 0
b 0
f 0

6 Methods

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