Completed
Push — master ( b34895...f1480e )
by Valentin
03:44
created

WorkflowInstance::getHost()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
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;
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
     * @return WorkflowInstance
265
     */
266 33
    public function updateStatus(): WorkflowInstance
267
    {
268 33
        if (!is_null($this->getRunningTasks()) && !is_null($this->getQueuedTasks())
269 33
            && $this->getRunningTasks() - $this->getQueuedTasks() > 0) {
270 1
            $this->setStatus(self::STATUS_RUNNING);
271
        }
272 33
        if (!is_null($this->getQueuedTasks()) && $this->getQueuedTasks() > 0) {
273 2
            $this->setStatus(self::STATUS_QUEUED);
274
        }
275 33
        if (!is_null($this->getRetryingTasks()) && $this->getRetryingTasks() > 0) {
276 2
            $this->setStatus(self::STATUS_RETRYING);
277
        }
278 33
        if (!is_null($this->getErrors()) && $this->getErrors() > 0) {
279 2
            $this->setStatus(self::STATUS_FAILED);
280
        }
281 33
        return $this;
282
    }
283
284
    /**
285
     * @param string $status
286
     *
287
     * @return WorkflowInstance
288
     */
289 5
    public function setStatus(string $status): WorkflowInstance
290
    {
291 5
        $this->status = $status;
292 5
        return $this;
293
    }
294
295
    /**
296
     * @return int|null
297
     */
298 2
    public function getEvqid(): ?int
299
    {
300 2
        return $this->evqid;
301
    }
302
303
    /**
304
     * @param int|null $evqid
305
     *
306
     * @return WorkflowInstance
307
     */
308 1
    public function setEvqid(?int $evqid): WorkflowInstance
309
    {
310 1
        $this->evqid = $evqid;
311 1
        return $this;
312
    }
313
314
    /**
315
     * @return int|null
316
     */
317 33
    public function getQueuedTasks(): ?int
318
    {
319 33
        return $this->queuedTasks;
320
    }
321
322
    /**
323
     * @param int|null $queuedTasks
324
     *
325
     * @return WorkflowInstance
326
     */
327 1
    public function setQueuedTasks(?int $queuedTasks): WorkflowInstance
328
    {
329 1
        $this->queuedTasks = $queuedTasks;
330 1
        return $this;
331
    }
332
333
    /**
334
     * @return int|null
335
     */
336 33
    public function getRunningTasks(): ?int
337
    {
338 33
        return $this->runningTasks;
339
    }
340
341
    /**
342
     * @param int|null $runningTasks
343
     *
344
     * @return WorkflowInstance
345
     */
346 1
    public function setRunningTasks(?int $runningTasks): WorkflowInstance
347
    {
348 1
        $this->runningTasks = $runningTasks;
349 1
        return $this;
350
    }
351
352
    /**
353
     * @return int|null
354
     */
355 33
    public function getRetryingTasks(): ?int
356
    {
357 33
        return $this->retryingTasks;
358
    }
359
360
    /**
361
     * @param int|null $retryingTasks
362
     *
363
     * @return WorkflowInstance
364
     */
365 1
    public function setRetryingTasks(?int $retryingTasks): WorkflowInstance
366
    {
367 1
        $this->retryingTasks = $retryingTasks;
368 1
        return $this;
369
    }
370
371
    /**
372
     * @return ArrayCollection
373
     */
374 4
    public function getJobInstances(): ArrayCollection
375
    {
376 4
        return $this->jobInstances;
377
    }
378
379
    /**
380
     * @param ArrayCollection $jobInstances
381
     *
382
     * @return WorkflowInstance
383
     */
384 1
    public function setJobInstances(ArrayCollection $jobInstances): WorkflowInstance
385
    {
386
        $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

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