Completed
Push — master ( 2fddbf...54b043 )
by Valentin
04:07
created

WorkflowInstance::setComment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
4
namespace Pheanstalk\Structure;
5
6
use Doctrine\Common\Collections\ArrayCollection;
7
use Pheanstalk\Exception\ServerBadFormatException;
8
9
class WorkflowInstance
10
{
11
//@running_tasks - @queued_tasks > 0
12
13
    const STATUS_QUEUED = "QUEUED";
14
    const STATUS_RUNNING = "RUNNING";
15
    const STATUS_RETRYING = "RETRYING";
16
    const STATUS_FAILED = "FAILED";
17
18
    /** @var string|null $comment */
19
    private $comment;
20
21
    /** @var \DateTime|null $endTime */
22
    private $endTime;
23
24
    /** @var int|null $errors */
25
    private $errors;
26
27
    /** @var string|null $host */
28
    private $host;
29
30
    /** @var int $id */
31
    private $id;
32
33
    /** @var string $name */
34
    private $name;
35
36
    /** @var string|null $nodeName */
37
    private $nodeName;
38
39
    /** @var int|null $scheduleId */
40
    private $scheduleId;
41
42
    /** @var \DateTime $startTime */
43
    private $startTime;
44
45
    /** @var string $status */
46
    private $status;
47
48
    /** @var int|null $evqid */
49
    private $evqid;
50
51
    /** @var int|null $queuedTasks */
52
    private $queuedTasks;
53
54
    /** @var int|null $runningTasks */
55
    private $runningTasks;
56
57
    /** @var int|null $retryingTasks */
58
    private $retryingTasks;
59
60
    /** @var ArrayCollection */
61
    private $jobInstances;
62
63
    /**
64
     * WorkflowInstance constructor.
65
     *
66
     * @param array $params
67
     *
68
     * @throws \ReflectionException
69
     */
70 33
    public function __construct(array $params)
71
    {
72 33
        $this->jobInstances = new ArrayCollection([]);
73 33
        $thisObject = new \ReflectionClass($this);
74 33
        $properties = $thisObject->getProperties();
75 33
        foreach ($properties as $property) {
76 33
            $snakeProperty = $this->fromCamelCase($property->getName());
77 33
            if (isset($params[$snakeProperty])) {
78 16
                $this->{$property->getName()} = $params[$snakeProperty];
79
            }
80
        }
81 33
        $this->updateStatus();
82
    }
83
84
    /**
85
     * @return null|string
86
     */
87 2
    public function getComment(): ?string
88
    {
89 2
        return $this->comment;
90
    }
91
92
    /**
93
     * @param string $comment
94
     *
95
     * @return WorkflowInstance
96
     */
97 1
    public function setComment(string $comment): WorkflowInstance
98
    {
99 1
        $this->comment = $comment;
100 1
        return $this;
101
    }
102
103
    /**
104
     * @return null|\DateTime
105
     */
106 2
    public function getEndTime(): ?\DateTime
107
    {
108 2
        return $this->endTime;
109
    }
110
111
    /**
112
     * @param \DateTime $endTime
113
     *
114
     * @return WorkflowInstance
115
     */
116 1
    public function setEndTime(\DateTime $endTime): WorkflowInstance
117
    {
118 1
        $this->endTime = $endTime;
119 1
        return $this;
120
    }
121
122
    /**
123
     * @return null|int
124
     */
125 33
    public function getErrors(): ?int
126
    {
127 33
        return $this->errors ?? 0;
128
    }
129
130
    /**
131
     * @param null|int $errors
132
     *
133
     * @return WorkflowInstance
134
     */
135 1
    public function setErrors(?int $errors): WorkflowInstance
136
    {
137 1
        $this->errors = $errors;
138 1
        return $this;
139
    }
140
141
    /**
142
     * @return null|string
143
     */
144 2
    public function getHost(): ?string
145
    {
146 2
        return $this->host;
147
    }
148
149
    /**
150
     * @param string $host
151
     *
152
     * @return WorkflowInstance
153
     */
154 1
    public function setHost(string $host): WorkflowInstance
155
    {
156 1
        $this->host = $host;
157 1
        return $this;
158
    }
159
160
    /**
161
     * @return int
162
     */
163 2
    public function getId(): int
164
    {
165 2
        return $this->id;
166
    }
167
168
    /**
169
     * @param int $id
170
     *
171
     * @return WorkflowInstance
172
     */
173 1
    public function setId(int $id): WorkflowInstance
174
    {
175 1
        $this->id = $id;
176 1
        return $this;
177
    }
178
179
    /**
180
     * @return string
181
     */
182 2
    public function getName(): string
183
    {
184 2
        return $this->name;
185
    }
186
187
    /**
188
     * @param string $name
189
     *
190
     * @return WorkflowInstance
191
     */
192 1
    public function setName(string $name): WorkflowInstance
193
    {
194 1
        $this->name = $name;
195 1
        return $this;
196
    }
197
198
    /**
199
     * @return null|string
200
     */
201 2
    public function getNodeName(): ?string
202
    {
203 2
        return $this->nodeName;
204
    }
205
206
    /**
207
     * @param string $nodeName
208
     *
209
     * @return WorkflowInstance
210
     */
211 1
    public function setNodeName(string $nodeName): WorkflowInstance
212
    {
213 1
        $this->nodeName = $nodeName;
214 1
        return $this;
215
    }
216
217
    /**
218
     * @return null|int
219
     */
220 2
    public function getScheduleId(): ?int
221
    {
222 2
        return $this->scheduleId;
223
    }
224
225
    /**
226
     * @param int $scheduleId
227
     *
228
     * @return WorkflowInstance
229
     */
230 1
    public function setScheduleId(int $scheduleId): WorkflowInstance
231
    {
232 1
        $this->scheduleId = $scheduleId;
233 1
        return $this;
234
    }
235
236
    /**
237
     * @return \DateTime
238
     */
239 2
    public function getStartTime(): \DateTime
240
    {
241 2
        return $this->startTime;
242
    }
243
244
    /**
245
     * @param \DateTime $startTime
246
     *
247
     * @return WorkflowInstance
248
     */
249 1
    public function setStartTime(\DateTime $startTime): WorkflowInstance
250
    {
251 1
        $this->startTime = $startTime;
252 1
        return $this;
253
    }
254
255
    /**
256
     * @return string
257
     */
258 3
    public function getStatus(): string
259
    {
260 3
        return $this->status;
261
    }
262
263
    /**
264
     *
265
     */
266 33
    public function updateStatus()
267
    {
268 33
        if ($this->getRunningTasks() > 0) {
269 2
            $this->setStatus(self::STATUS_RUNNING);
270
        }
271 33
        if ($this->getQueuedTasks() > 0) {
272 2
            $this->setStatus(self::STATUS_QUEUED);
273
        }
274 33
        if ($this->getRetryingTasks() > 0) {
275 2
            $this->setStatus(self::STATUS_RETRYING);
276
        }
277 33
        if ($this->getErrors() > 0) {
278 2
            $this->setStatus(self::STATUS_FAILED);
279
        }
280
    }
281
282
    /**
283
     * @param string $status
284
     *
285
     * @return WorkflowInstance
286
     */
287 6
    public function setStatus(string $status): WorkflowInstance
288
    {
289 6
        $this->status = $status;
290 6
        return $this;
291
    }
292
293
    /**
294
     * @return int|null
295
     */
296 2
    public function getEvqid(): ?int
297
    {
298 2
        return $this->evqid;
299
    }
300
301
    /**
302
     * @param int|null $evqid
303
     *
304
     * @return WorkflowInstance
305
     */
306 1
    public function setEvqid(?int $evqid): WorkflowInstance
307
    {
308 1
        $this->evqid = $evqid;
309 1
        return $this;
310
    }
311
312
    /**
313
     * @return int|null
314
     */
315 33
    public function getQueuedTasks(): ?int
316
    {
317 33
        return $this->queuedTasks ?? 0;
318
    }
319
320
    /**
321
     * @param int|null $queuedTasks
322
     *
323
     * @return WorkflowInstance
324
     */
325 1
    public function setQueuedTasks(?int $queuedTasks): WorkflowInstance
326
    {
327 1
        $this->queuedTasks = $queuedTasks;
328 1
        return $this;
329
    }
330
331
    /**
332
     * @return int|null
333
     */
334 33
    public function getRunningTasks(): ?int
335
    {
336 33
        return $this->runningTasks ?? 0;
337
    }
338
339
    /**
340
     * @param int|null $runningTasks
341
     *
342
     * @return WorkflowInstance
343
     */
344 1
    public function setRunningTasks(?int $runningTasks): WorkflowInstance
345
    {
346 1
        $this->runningTasks = $runningTasks;
347 1
        return $this;
348
    }
349
350
    /**
351
     * @return int|null
352
     */
353 33
    public function getRetryingTasks(): ?int
354
    {
355 33
        return $this->retryingTasks ?? 0;
356
    }
357
358
    /**
359
     * @param int|null $retryingTasks
360
     *
361
     * @return WorkflowInstance
362
     */
363 1
    public function setRetryingTasks(?int $retryingTasks): WorkflowInstance
364
    {
365 1
        $this->retryingTasks = $retryingTasks;
366 1
        return $this;
367
    }
368
369
    /**
370
     * @return ArrayCollection
371
     */
372 4
    public function getJobInstances(): ArrayCollection
373
    {
374 4
        return $this->jobInstances;
375
    }
376
377
    /**
378
     * @param ArrayCollection $jobInstances
379
     *
380
     * @return WorkflowInstance
381
     */
382 1
    public function setJobInstances(ArrayCollection $jobInstances): WorkflowInstance
383
    {
384
        $this->jobInstances = $jobInstances->filter(function(JobInstance $jobInstance) {
1 ignored issue
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
Unused Code introduced by
The parameter $jobInstance is not used and could be removed. ( Ignorable by Annotation )

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

384
        $this->jobInstances = $jobInstances->filter(function(/** @scrutinizer ignore-unused */ JobInstance $jobInstance) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
385 1
            return true;
386 1
        });
387 1
        return $this;
388
    }
389
390
    /**
391
     * @param JobInstance $jobInstance
392
     *
393
     * @return WorkflowInstance
394
     */
395 2
    public function addJobInstance(JobInstance $jobInstance): WorkflowInstance
396
    {
397 2
        $this->jobInstances[] = $jobInstance;
398 2
        return $this;
399
    }
400
401
    /**
402
     * @param JobInstance $jobInstance
403
     *
404
     * @return WorkflowInstance
405
     */
406 1
    public function removeJobInstance(JobInstance $jobInstance): WorkflowInstance
407
    {
408 1
        $this->jobInstances->removeElement($jobInstance);
409 1
        return $this;
410
    }
411
412
    /**
413
     * @param $input
414
     *
415
     * @return string
416
     */
417 33
    private function fromCamelCase($input)
418
    {
419 33
        preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches);
420 33
        $ret = $matches[0];
421 33
        foreach ($ret as &$match) {
422 33
            $match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match);
423
        }
424 33
        return implode('_', $ret);
425
    }
426
}
427