Completed
Push — develop ( 1049f6...ddb6ce )
by Mathias
88:23 queued 80:14
created

JobResult::create()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 18
ccs 0
cts 14
cp 0
rs 9.6111
c 0
b 0
f 0
cc 5
nc 8
nop 2
crap 30
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright  2013 - 2019 Cross Solution <http://cross-solution.de>
8
 */
9
10
declare(strict_types=1);
11
12
/** */
13
namespace Core\Queue\Job;
14
15
use SlmQueue\Worker\Event\ProcessJobEvent;
16
17
/**
18
 * ${CARET}
19
 * 
20
 * @author Mathias Gelhausen <[email protected]>
21
 * @todo write test 
22
 */
23
class JobResult
24
{
25
    /**
26
     *
27
     *
28
     * @var int
29
     */
30
    protected $result;
31
32
    /**
33
     *
34
     *
35
     * @var string
36
     */
37
    protected $reason;
38
39
    /**
40
     *
41
     *
42
     * @var array
43
     */
44
    protected $extra;
45
46
    /**
47
     *
48
     *
49
     * @var string|int|\DateInterval
50
     */
51
    protected $delay;
52
53
    /**
54
     *
55
     *
56
     * @var string|int|\DateTime
57
     */
58
    protected $scheduled;
59
60
    public static function success(?string $reason = null, ?array $extra = null) : self
61
    {
62
        return static::create(
63
            ProcessJobEvent::JOB_STATUS_SUCCESS,
64
            [
65
                'reason' => $reason,
66
                'extra'  => $extra,
67
            ]
68
        );
69
    }
70
71
    public static function failure(string $reason, ?array $extra = null) : self
72
    {
73
        return static::create(
74
            ProcessJobEvent::JOB_STATUS_FAILURE,
75
            [
76
                'reason' => $reason,
77
                'extra'  => $extra,
78
            ]
79
        );
80
    }
81
82
    public static function recoverable(string $reason, array $options = []) : self
83
    {
84
        return static::create(
85
            ProcessJobEvent::JOB_STATUS_FAILURE_RECOVERABLE,
86
            [ 'reason' => $reason ] + $options
87
        );
88
    }
89
90
    public static function create(int $code, $options)
91
    {
92
        $result = new static($code);
93
94
        if (is_string($options)) {
95
            $options = ['reason' => $options];
96
        }
97
98
        foreach ($options as $key => $value) {
99
            if (!$value) { continue; }
100
101
            $callback = [$result, "with$key"];
102
            if (is_callable($callback)) {
103
                $callback($value);
104
            }
105
        }
106
107
        return $result;
108
    }
109
110
    public function __construct(int $result)
111
    {
112
        $this->result = $result;
113
    }
114
115
    public function getResult() : int
116
    {
117
        return $this->result;
118
    }
119
120
    public function isSuccess() : bool
121
    {
122
        return ProcessJobEvent::JOB_STATUS_SUCCESS == $this->result;
123
    }
124
125
    public function isFailure() : bool
126
    {
127
        return ProcessJobEvent::JOB_STATUS_FAILURE == $this->result;
128
    }
129
130
    public function isRecoverable() : bool
131
    {
132
        return ProcessJobEvent::JOB_STATUS_FAILURE_RECOVERABLE == $this->result;
133
    }
134
135
    /**
136
     * @return string
137
     */
138
    public function getReason(): ?string
139
    {
140
        return $this->reason;
141
    }
142
143
    /**
144
     * @param string $message
145
     *
146
     * @return self
147
     */
148
    public function withReason($reason) : self
149
    {
150
        $this->reason = $reason;
151
152
        return $this;
153
    }
154
155
    /**
156
     * @return array
157
     */
158
    public function getExtra(): ?array
159
    {
160
        return $this->extra;
161
    }
162
163
    /**
164
     * @param array $extra
165
     *
166
     * @return self
167
     */
168
    public function withExtra(array $extra) : self
169
    {
170
        $this->extra = $extra;
171
172
        return $this;
173
    }
174
175
    /**
176
     * @return \DateInterval|int|string
177
     */
178
    public function getDelay()
179
    {
180
        return $this->delay;
181
    }
182
183
    /**
184
     * @param \DateInterval|int|string $delay
185
     *
186
     * @return self
187
     */
188
    public function withDelay($delay) : self
189
    {
190
        $this->delay = $delay;
191
192
        return $this;
193
    }
194
195
    /**
196
     * @return \DateTime|int|string
197
     */
198
    public function getDate()
199
    {
200
        return $this->scheduled;
201
    }
202
203
    /**
204
     * @param \DateTime|int|string $scheduled
205
     *
206
     * @return self
207
     */
208
    public function withDate($scheduled) : self
209
    {
210
        $this->scheduled = $scheduled;
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return Core\Queue\Job\JobResult. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
211
    }
212
}
213