Test Failed
Push — main ( a2096a...c7561b )
by Bingo
15:21
created

JobQueryImpl::hasExcludingDueDateParameters()   B

Complexity

Conditions 9
Paths 16

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 20
rs 8.0555
c 0
b 0
f 0
cc 9
nc 16
nop 0
1
<?php
2
3
namespace Jabe\Engine\Impl;
4
5
use Jabe\Engine\ProcessEngineException;
6
use Jabe\Engine\Impl\Interceptor\{
7
    CommandContext,
8
    CommandExecutorInterface
9
};
10
use Jabe\Engine\Impl\Persistence\Entity\SuspensionState;
11
use Jabe\Engine\Impl\Util\{
12
    ClockUtil,
13
    CompareUtil,
14
    EnsureUtil,
15
    ImmutablePair
16
};
17
use Jabe\Engine\Runtime\{
18
    JobInterface,
19
    JobQueryInterface
20
};
21
22
class JobQueryImpl extends AbstractQuery implements JobQueryInterface, \Serializable
23
{
24
    protected $activityId;
25
    protected $id;
26
    protected $ids = [];
27
    protected $jobDefinitionId;
28
    protected $processInstanceId;
29
    protected $processInstanceIds = [];
30
    protected $executionId;
31
    protected $processDefinitionId;
32
    protected $processDefinitionKey;
33
    protected $retriesLeft;
34
    protected $executable;
35
    protected $onlyTimers;
36
    protected $onlyMessages;
37
    protected $duedateHigherThan;
38
    protected $duedateLowerThan;
39
    protected $duedateHigherThanOrEqual;
40
    protected $duedateLowerThanOrEqual;
41
    protected $createdBefore;
42
    protected $createdAfter;
43
    protected $priorityHigherThanOrEqual;
44
    protected $priorityLowerThanOrEqual;
45
    protected $withException;
46
    protected $exceptionMessage;
47
    protected $failedActivityId;
48
    protected $noRetriesLeft;
49
    protected $suspensionState;
50
    protected $isTenantIdSet = false;
51
    protected $tenantIds = [];
52
    protected $includeJobsWithoutTenantId = false;
53
54
    public function __construct(CommandExecutorInterface $commandExecutor)
55
    {
56
        parent::__construct($commandExecutor);
57
    }
58
59
    public function serialize()
60
    {
61
        return json_encode([
62
            'id' => $this->id,
63
            'activityId' => $this->activityId,
64
            'ids' => $this->ids,
65
            'jobDefinitionId' => $this->jobDefinitionId,
66
            'processInstanceId' => $this->processInstanceId,
67
            'processInstanceIds' => $this->processInstanceIds,
68
            'executionId' => $this->executionId,
69
            'processDefinitionId' => $this->processDefinitionId,
70
            'processDefinitionKey' => $this->processDefinitionKey,
71
            'retriesLeft' => $this->retriesLeft,
72
            'executable' => $this->executable,
73
            'onlyTimers' => $this->onlyTimers,
74
            'onlyMessages' => $this->onlyMessages,
75
            'duedateHigherThan' => $this->duedateHigherThan,
76
            'duedateLowerThan' => $this->duedateLowerThan,
77
            'duedateHigherThanOrEqual' => $this->duedateHigherThanOrEqual,
78
            'duedateLowerThanOrEqual' => $this->duedateLowerThanOrEqual,
79
            'createdBefore' => $this->createdBefore,
80
            'createdAfter' => $this->createdAfter,
81
            'priorityHigherThanOrEqual' => $this->priorityHigherThanOrEqual,
82
            'priorityLowerThanOrEqual' => $this->priorityLowerThanOrEqual,
83
            'withException' => $this->withException,
84
            'exceptionMessage' => $this->exceptionMessage,
85
            'failedActivityId' => $this->failedActivityId,
86
            'noRetriesLeft' => $this->noRetriesLeft,
87
            'suspensionState' => serialize($this->suspensionState),
88
            'isTenantIdSet' => $this->isTenantIdSet,
89
            'tenantIds' => $this->tenantIds,
90
            'includeJobsWithoutTenantId' => $this->includeJobsWithoutTenantId
91
        ]);
92
    }
93
94
    public function unserialize($data)
95
    {
96
        $json = json_decode($data);
97
        $this->id = $json->id;
98
        $this->activityId = $json->activityId;
99
        $this->ids = $json->ids;
100
        $this->jobDefinitionId = $json->jobDefinitionId;
101
        $this->processInstanceId = $json->processInstanceId;
102
        $this->processInstanceIds = $json->processInstanceIds;
103
        $this->executionId = $json->executionId;
104
        $this->processDefinitionId = $json->processDefinitionId;
105
        $this->processDefinitionKey = $json->processDefinitionKey;
106
        $this->retriesLeft = $json->retriesLeft;
107
        $this->executable = $json->executable;
108
        $this->onlyTimers = $json->onlyTimers;
109
        $this->onlyMessages = $json->onlyMessages;
110
        $this->duedateHigherThan = $json->duedateHigherThan;
111
        $this->duedateLowerThan = $json->duedateLowerThan;
112
        $this->duedateHigherThanOrEqual = $json->duedateHigherThanOrEqual;
113
        $this->duedateLowerThanOrEqual = $json->duedateLowerThanOrEqual;
114
        $this->createdBefore = $json->createdBefore;
115
        $this->createdAfter = $json->createdAfter;
116
        $this->priorityHigherThanOrEqual = $json->priorityHigherThanOrEqual;
117
        $this->priorityLowerThanOrEqual = $json->priorityLowerThanOrEqual;
118
        $this->withException = $json->withException;
119
        $this->exceptionMessage = $json->exceptionMessage;
120
        $this->failedActivityId = $json->failedActivityId;
121
        $this->noRetriesLeft = $json->noRetriesLeft;
122
        $this->suspensionState = unserialize($json->suspensionState);
123
        $this->isTenantIdSet = $json->isTenantIdSet;
124
        $this->tenantIds = $json->tenantIds;
125
        $this->includeJobsWithoutTenantId = $json->includeJobsWithoutTenantId;
126
    }
127
128
    public function jobId(string $jobId): JobQueryInterface
129
    {
130
        EnsureUtil::ensureNotNull("Provided job id", "jobId", $jobId);
131
        $this->id = $jobId;
132
        return $this;
133
    }
134
135
    public function jobIds(array $ids): JobQueryInterface
136
    {
137
        EnsureUtil::ensureNotEmpty("Set of job ids", "ids", $ids);
138
        $this->ids = $ids;
139
        return $this;
140
    }
141
142
    public function jobDefinitionId(string $jobDefinitionId): JobQueryInterface
143
    {
144
        EnsureUtil::ensureNotNull("Provided job definition id", "jobDefinitionId", $jobDefinitionId);
145
        $this->jobDefinitionId = $jobDefinitionId;
146
        return $this;
147
    }
148
149
    public function processInstanceId(string $processInstanceId): JobQueryImpl
150
    {
151
        EnsureUtil::ensureNotNull("Provided process instance id", "processInstanceId", $processInstanceId);
152
        $this->processInstanceId = $processInstanceId;
153
        return $this;
154
    }
155
156
    public function processInstanceIds(array $processInstanceIds): JobQueryInterface
157
    {
158
        EnsureUtil::ensureNotEmpty("Set of process instance ids", "processInstanceIds", $processInstanceIds);
159
        $this->processInstanceIds = $processInstanceIds;
160
        return $this;
161
    }
162
163
    public function executionId(string $executionId): JobQueryImpl
164
    {
165
        EnsureUtil::ensureNotNull("Provided execution id", "executionId", $executionId);
166
        $this->executionId = $executionId;
167
        return $this;
168
    }
169
170
    public function processDefinitionId(string $processDefinitionId): JobQueryInterface
171
    {
172
        EnsureUtil::ensureNotNull("Provided process definition id", "processDefinitionId", $processDefinitionId);
173
        $this->processDefinitionId = $processDefinitionId;
174
        return $this;
175
    }
176
177
    public function processDefinitionKey(string $processDefinitionKey): JobQueryInterface
178
    {
179
        EnsureUtil::ensureNotNull("Provided process instance key", "processDefinitionKey", $processDefinitionKey);
180
        $this->processDefinitionKey = $processDefinitionKey;
181
        return $this;
182
    }
183
184
    public function activityId(string $activityId): JobQueryInterface
185
    {
186
        EnsureUtil::ensureNotNull("Provided activity id", "activityId", $activityId);
187
        $this->activityId = $activityId;
188
        return $this;
189
    }
190
191
    public function withRetriesLeft(): JobQueryInterface
192
    {
193
        $this->retriesLeft = true;
194
        return $this;
195
    }
196
197
    public function executable(): JobQueryInterface
198
    {
199
        $this->executable = true;
200
        return $this;
201
    }
202
203
    public function timers(): JobQueryInterface
204
    {
205
        if ($this->onlyMessages) {
206
            throw new ProcessEngineException("Cannot combine onlyTimers() with onlyMessages() in the same query");
207
        }
208
        $this->onlyTimers = true;
209
        return $this;
210
    }
211
212
    public function messages(): JobQueryInterface
213
    {
214
        if ($this->onlyTimers) {
215
            throw new ProcessEngineException("Cannot combine onlyTimers() with onlyMessages() in the same query");
216
        }
217
        $this->onlyMessages = true;
218
        return $this;
219
    }
220
221
    public function duedateHigherThan(string $date): JobQueryInterface
222
    {
223
        EnsureUtil::ensureNotNull("Provided date", "date", $date);
224
        $this->duedateHigherThan = $date;
225
        return $this;
226
    }
227
228
    public function duedateLowerThan(string $date): JobQueryInterface
229
    {
230
        EnsureUtil::ensureNotNull("Provided date", "date", $date);
231
        $this->duedateLowerThan = $date;
232
        return $this;
233
    }
234
235
    public function duedateHigherThen(string $date): JobQueryInterface
236
    {
237
        return $this->duedateHigherThan($date);
238
    }
239
240
    public function duedateHigherThenOrEquals(string $date): JobQueryInterface
241
    {
242
        EnsureUtil::ensureNotNull("Provided date", "date", $date);
243
        $this->duedateHigherThanOrEqual = $date;
244
        return $this;
245
    }
246
247
    public function duedateLowerThen(string $date): JobQueryInterface
248
    {
249
        return $this->duedateLowerThan($date);
250
    }
251
252
    public function duedateLowerThenOrEquals(string $date): JobQueryInterface
253
    {
254
        EnsureUtil::ensureNotNull("Provided date", "date", $date);
255
        $this->duedateLowerThanOrEqual = $date;
256
        return $this;
257
    }
258
259
    public function createdBefore(string $date): JobQueryInterface
260
    {
261
        EnsureUtil::ensureNotNull("Provided date", "date", $date);
262
        $this->createdBefore = $date;
263
        return $this;
264
    }
265
266
    public function createdAfter(string $date): JobQueryInterface
267
    {
268
        EnsureUtil::ensureNotNull("Provided date", "date", $date);
269
        $this->createdAfter = $date;
270
        return $this;
271
    }
272
273
    public function priorityHigherThanOrEquals(int $priority): JobQueryInterface
274
    {
275
        $this->priorityHigherThanOrEqual = $priority;
276
        return $this;
277
    }
278
279
    public function priorityLowerThanOrEquals(int $priority): JobQueryInterface
280
    {
281
        $this->priorityLowerThanOrEqual = $priority;
282
        return $this;
283
    }
284
285
    public function withException(): JobQueryInterface
286
    {
287
        $this->withException = true;
288
        return $this;
289
    }
290
291
    public function exceptionMessage(string $exceptionMessage): JobQueryInterface
292
    {
293
        EnsureUtil::ensureNotNull("Provided exception message", "exceptionMessage", $exceptionMessage);
294
        $this->exceptionMessage = $exceptionMessage;
295
        return $this;
296
    }
297
298
    public function failedActivityId(string $activityId): JobQueryInterface
299
    {
300
        EnsureUtil::ensureNotNull("Provided activity id", "activityId", $activityId);
301
        $this->failedActivityId = $activityId;
302
        return $this;
303
    }
304
305
    public function noRetriesLeft(): JobQueryInterface
306
    {
307
        $this->noRetriesLeft = true;
308
        return $this;
309
    }
310
311
    public function active(): JobQueryInterface
312
    {
313
        $this->suspensionState = SuspensionState::active();
314
        return $this;
315
    }
316
317
    public function suspended(): JobQueryInterface
318
    {
319
        $this->suspensionState = SuspensionState::suspended();
320
        return $this;
321
    }
322
323
    protected function hasExcludingConditions(): bool
324
    {
325
        return parent::hasExcludingConditions()
326
            || CompareUtil::areNotInAscendingOrder($this->priorityHigherThanOrEqual, $this->priorityLowerThanOrEqual)
327
            || $this-> hasExcludingDueDateParameters()
328
            || CompareUtil::areNotInAscendingOrder($this->createdAfter, $this->createdBefore);
329
    }
330
331
    private function hasExcludingDueDateParameters(): bool
332
    {
333
        $dueDates = [];
334
        if ($this->duedateHigherThan != null && $this->duedateHigherThanOrEqual != null) {
335
            $dueDates[] = CompareUtil::min($this->duedateHigherThan, $this->duedateHigherThanOrEqual);
336
            $dueDates[] = CompareUtil::max($this->duedateHigherThan, $this->duedateHigherThanOrEqual);
337
        } elseif ($this->duedateHigherThan != null) {
338
            $dueDates[] = $duedateHigherThan;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $duedateHigherThan seems to be never defined.
Loading history...
339
        } elseif ($this->duedateHigherThanOrEqual != null) {
340
            $dueDates[] = $this->duedateHigherThanOrEqual;
341
        }
342
        if ($this->duedateLowerThan != null && $this->duedateLowerThanOrEqual != null) {
343
            $dueDates[] = CompareUtil::min($this->duedateLowerThan, $this->duedateLowerThanOrEqual);
344
            $dueDates[] = CompareUtil::max($this->duedateLowerThan, $this->duedateLowerThanOrEqual);
345
        } elseif ($this->duedateLowerThan != null) {
346
            $dueDates[] = $this->duedateLowerThan;
347
        } elseif ($this->duedateLowerThanOrEqual != null) {
348
            $dueDates[] = $this->duedateLowerThanOrEqual;
349
        }
350
        return CompareUtil::areNotInAscendingOrder($dueDates);
351
    }
352
353
    public function tenantIdIn(array $tenantIds): JobQueryInterface
354
    {
355
        EnsureUtil::ensureNotNull("tenantIds", "tenantIds", $tenantIds);
356
        $this->tenantIds = $tenantIds;
357
        $this->isTenantIdSet = true;
358
        return $this;
359
    }
360
361
    public function withoutTenantId(): JobQueryInterface
362
    {
363
        $this->isTenantIdSet = true;
364
        $this->tenantIds = null;
365
        return $this;
366
    }
367
368
    public function includeJobsWithoutTenantId(): JobQueryInterface
369
    {
370
        $this->includeJobsWithoutTenantId = true;
371
        return $this;
372
    }
373
374
    public function orderByJobDuedate(): JobQueryInterface
375
    {
376
        return $this->orderBy(JobQueryProperty::duedate());
377
    }
378
379
    public function orderByExecutionId(): JobQueryInterface
380
    {
381
        return $this->orderBy(JobQueryProperty::executionId());
382
    }
383
384
    public function orderByJobId(): JobQueryInterface
385
    {
386
        return $this->orderBy(JobQueryProperty::jobId());
387
    }
388
389
    public function orderByProcessInstanceId(): JobQueryInterface
390
    {
391
        return $this->orderBy(JobQueryProperty::processInstanceId());
392
    }
393
394
    public function orderByProcessDefinitionId(): JobQueryInterface
395
    {
396
        return $this->orderBy(JobQueryProperty::processDefinitionId());
397
    }
398
399
    public function orderByProcessDefinitionKey(): JobQueryInterface
400
    {
401
        return $this->orderBy(JobQueryProperty::processDefinitionKey());
402
    }
403
404
    public function orderByJobRetries(): JobQueryInterface
405
    {
406
        return $this->orderBy(JobQueryProperty::retries());
407
    }
408
409
    public function orderByJobPriority(): JobQueryInterface
410
    {
411
        return $this->orderBy(JobQueryProperty::priority());
412
    }
413
414
    public function orderByTenantId(): JobQueryInterface
415
    {
416
        return $this->orderBy(JobQueryProperty::tenantId());
417
    }
418
419
    public function executeCount(CommandContext $commandContext): int
420
    {
421
        $this->checkQueryOk();
422
        return $commandContext
423
            ->getJobManager()
424
            ->findJobCountByQueryCriteria($this);
425
    }
426
427
    public function executeList(CommandContext $commandContext, Page $page): array
428
    {
429
        $this->checkQueryOk();
430
        return $commandContext
431
            ->getJobManager()
432
            ->findJobsByQueryCriteria($this, $page);
433
    }
434
435
    public function executeDeploymentIdMappingsList(CommandContext $commandContext): array
436
    {
437
        $this->checkQueryOk();
438
        return $commandContext
439
            ->getJobManager()
440
            ->findDeploymentIdMappingsByQueryCriteria($this);
441
    }
442
443
    public function getIds(): array
444
    {
445
        return $this->ids;
446
    }
447
448
    public function getProcessInstanceId(): string
449
    {
450
        return $this->processInstanceId;
451
    }
452
453
    public function getProcessInstanceIds(): array
454
    {
455
        return $this->processInstanceIds;
456
    }
457
458
    public function getExecutionId(): string
459
    {
460
        return $this->executionId;
461
    }
462
463
    public function getRetriesLeft(): bool
464
    {
465
        return $this->retriesLeft;
466
    }
467
468
    public function getExecutable(): bool
469
    {
470
        return $this->executable;
471
    }
472
473
    public function getNow(): string
474
    {
475
        return ClockUtil::getCurrentTime()->format('c');
476
    }
477
478
    public function isWithException(): bool
479
    {
480
        return $this->withException;
481
    }
482
483
    public function getExceptionMessage(): string
484
    {
485
        return $this->exceptionMessage;
486
    }
487
}
488