Completed
Push — master ( 24883c...f76bc5 )
by Nate
07:27
created

AbstractJob::toConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * @author    Flipbox Factory
5
 * @copyright Copyright (c) 2017, Flipbox Digital
6
 * @link      https://github.com/flipbox/queue/releases/latest
7
 * @license   https://github.com/flipbox/queue/blob/master/LICENSE
8
 */
9
10
namespace flipbox\queue\jobs;
11
12
use flipbox\queue\events\AfterJobRun;
13
use flipbox\queue\events\BeforeJobRun;
14
use yii\base\Component;
15
use yii\helpers\ArrayHelper;
16
17
/**
18
 * @author Flipbox Factory <[email protected]>
19
 * @since 1.0.0
20
 */
21
abstract class AbstractJob extends Component implements JobInterface
22
{
23
24
    /**
25
     * Event executed before a job is being executed.
26
     */
27
    const EVENT_BEFORE_RUN = 'beforeRun';
28
29
    /**
30
     * Event executed after a job is being executed.
31
     */
32
    const EVENT_AFTER_RUN = 'afterRun';
33
34
    /**
35
     * The ID of the message. This should be set on the job receive.
36
     * @var integer
37
     */
38
    protected $id;
39
40
    /**
41
     * Stores the header.
42
     * This can be different for each queue provider.
43
     *
44
     * @var mixed
45
     */
46
    protected $header = [];
47
48
    /**
49
     * @return mixed
50
     */
51
    abstract protected function runInternal();
52
53
    /**
54
     * @inheritdoc
55
     */
56
    public function run()
57
    {
58
        // Before event
59
        if (!$this->beforeRun()) {
60
            return false;
61
        }
62
63
        // Run the actual job
64
        $value = $this->runInternal();
65
66
        // After event
67
        $this->afterRun($value);
68
69
        return $value;
70
    }
71
72
    /**
73
     * @inheritdoc
74
     */
75
    public function getId()
76
    {
77
        return $this->id;
78
    }
79
80
    /**
81
     * @inheritdoc
82
     */
83
    public function setId($id)
84
    {
85
        $this->id = $id;
0 ignored issues
show
Documentation Bug introduced by
It seems like $id can also be of type string. However, the property $id is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
86
        return $this;
87
    }
88
89
    /**
90
     * @inheritdoc
91
     */
92
    public function getHeader($item, $default = null)
93
    {
94
        return ArrayHelper::getValue($this->header, $item, $default);
95
    }
96
97
    /**
98
     * @inheritdoc
99
     */
100
    public function setHeader($item, $value)
101
    {
102
        $this->header[$item] = $value;
103
        return $this;
104
    }
105
106
    /**
107
     * @return bool
108
     */
109
    protected function beforeRun()
110
    {
111
        $this->trigger(
112
            self::EVENT_BEFORE_RUN,
113
            $beforeEvent = new BeforeJobRun()
114
        );
115
116
        return $beforeEvent->isValid;
117
    }
118
119
    /**
120
     * @param $value
121
     */
122
    protected function afterRun($value)
123
    {
124
        $this->trigger(
125
            self::EVENT_BEFORE_RUN,
126
            $beforeEvent = new AfterJobRun([
127
                'value' => $value
128
            ])
129
        );
130
    }
131
132
    /**
133
     * @return array
134
     */
135
    public function toConfig(): array
136
    {
137
        return [
138
            'class' => get_class($this)
139
        ];
140
    }
141
}
142