Completed
Push — develop ( 488b09...0de0ee )
by Mathias
131:05 queued 122:54
created

JobResult   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 18
eloc 35
dl 0
loc 169
ccs 0
cts 80
cp 0
rs 10
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A failure() 0 5 1
A withExtra() 0 5 1
A getReason() 0 3 1
A isSuccess() 0 3 1
A recoverable() 0 13 3
A getDelay() 0 3 1
A withDate() 0 3 1
A withReason() 0 5 1
A getResult() 0 3 1
A getExtra() 0 3 1
A getDate() 0 3 1
A isRecoverable() 0 3 1
A __construct() 0 3 1
A withDelay() 0 5 1
A success() 0 5 1
A isFailure() 0 3 1
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 (new static(ProcessJobEvent::JOB_STATUS_SUCCESS))
63
            ->withReason($reason)
64
            ->withExtra($extra)
0 ignored issues
show
Bug introduced by
It seems like $extra can also be of type null; however, parameter $extra of Core\Queue\Job\JobResult::withExtra() does only seem to accept array, 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

64
            ->withExtra(/** @scrutinizer ignore-type */ $extra)
Loading history...
65
        ;
66
    }
67
68
    public static function failure(string $reason, ?array $extra = null) : self
69
    {
70
        return (new static(ProcessJobEvent::JOB_STATUS_FAILURE))
71
            ->withReason($reason)
72
            ->withExtra($extra)
0 ignored issues
show
Bug introduced by
It seems like $extra can also be of type null; however, parameter $extra of Core\Queue\Job\JobResult::withExtra() does only seem to accept array, 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

72
            ->withExtra(/** @scrutinizer ignore-type */ $extra)
Loading history...
73
        ;
74
    }
75
76
    public static function recoverable(string $reason, array $options = []) : self
77
    {
78
        $result = (new static(ProcessJobEvent::JOB_STATUS_FAILURE_RECOVERABLE))
79
            ->withReason($reason);
80
81
        foreach ($options as $key => $value) {
82
            $callback = [$result, "with$key"];
83
            if (is_callable($callback)) {
84
                $callback($value);
85
            }
86
        }
87
88
        return $result;
89
    }
90
91
    public function __construct(int $result)
92
    {
93
        $this->result = $result;
94
    }
95
96
    public function getResult() : int
97
    {
98
        return $this->result;
99
    }
100
101
    public function isSuccess() : bool
102
    {
103
        return ProcessJobEvent::JOB_STATUS_SUCCESS == $this->result;
104
    }
105
106
    public function isFailure() : bool
107
    {
108
        return ProcessJobEvent::JOB_STATUS_FAILURE == $this->result;
109
    }
110
111
    public function isRecoverable() : bool
112
    {
113
        return ProcessJobEvent::JOB_STATUS_FAILURE_RECOVERABLE == $this->result;
114
    }
115
116
    /**
117
     * @return string
118
     */
119
    public function getReason(): ?string
120
    {
121
        return $this->reason;
122
    }
123
124
    /**
125
     * @param string $message
126
     *
127
     * @return self
128
     */
129
    public function withReason($reason) : self
130
    {
131
        $this->reason = $reason;
132
133
        return $this;
134
    }
135
136
    /**
137
     * @return array
138
     */
139
    public function getExtra(): ?array
140
    {
141
        return $this->extra;
142
    }
143
144
    /**
145
     * @param array $extra
146
     *
147
     * @return self
148
     */
149
    public function withExtra(array $extra) : self
150
    {
151
        $this->extra = $extra;
152
153
        return $this;
154
    }
155
156
    /**
157
     * @return \DateInterval|int|string
158
     */
159
    public function getDelay()
160
    {
161
        return $this->delay;
162
    }
163
164
    /**
165
     * @param \DateInterval|int|string $delay
166
     *
167
     * @return self
168
     */
169
    public function withDelay($delay) : self
170
    {
171
        $this->delay = $delay;
172
173
        return $this;
174
    }
175
176
    /**
177
     * @return \DateTime|int|string
178
     */
179
    public function getDate()
180
    {
181
        return $this->scheduled;
182
    }
183
184
    /**
185
     * @param \DateTime|int|string $scheduled
186
     *
187
     * @return self
188
     */
189
    public function withDate($scheduled) : self
190
    {
191
        $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...
192
    }
193
}
194