Passed
Push — master ( 6b0e6b...43789e )
by Stephen
03:30
created

TrackActionJob::handle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 9
rs 10
1
<?php
2
3
namespace Sfneal\Tracking\Jobs;
4
5
use Exception;
6
use Illuminate\Database\Eloquent\Collection;
7
use Illuminate\Database\Eloquent\Model;
8
use Sfneal\Queueables\AbstractJob;
9
use Sfneal\Tracking\Actions\TrackActionAction;
10
11
// TODO: create package Tracking
12
class TrackActionJob extends AbstractJob
13
{
14
    /**
15
     * Delete the job if its models no longer exist.
16
     *
17
     * @var bool
18
     */
19
    public $deleteWhenMissingModels = true;
20
21
    /**
22
     * @var string Queue to use
23
     */
24
    public $queue = 'tracking';
25
26
    public $action;
27
    public $model;
28
    public $model_changes;
29
30
    /**
31
     * Track a user's action.
32
     *
33
     * @param string           $action
34
     * @param Model|Collection $model
35
     */
36
    public function __construct(string $action, $model)
37
    {
38
        $this->action = $action;
39
        $this->model = $model;
40
41
        try {
42
            $this->model_changes = $model->getChanges();
43
        } catch (Exception $exception) {
44
            $this->model_changes = [];
45
        }
46
    }
47
48
    /**
49
     * Execute the job.
50
     *
51
     * @return void
52
     */
53
    public function handle()
54
    {
55
        if ($this->model->exists) {
56
            (new TrackActionAction(
57
                $this->action,
58
                $this->model,
0 ignored issues
show
Bug introduced by
It seems like $this->model can also be of type Illuminate\Database\Eloquent\Collection; however, parameter $model of Sfneal\Tracking\Actions\...onAction::__construct() does only seem to accept Illuminate\Database\Eloquent\Model, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

58
                /** @scrutinizer ignore-type */ $this->model,
Loading history...
59
                $this->model_changes
60
            )
61
            )->execute();
62
        }
63
    }
64
}
65