ProcessInstanceQueryImpl::processInstanceId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Jabe\Impl;
4
5
use Jabe\ProcessEngineException;
6
use Jabe\Impl\Cfg\ProcessEngineConfigurationImpl;
7
use Jabe\Impl\Context\Context;
8
use Jabe\Impl\Interceptor\{
9
    CommandContext,
10
    CommandExecutorInterface
11
};
12
use Jabe\Impl\Persistence\Entity\SuspensionState;
13
use Jabe\Impl\Util\{
14
    EnsureUtil//,
15
    //ImmutablePair
16
};
17
use Jabe\Runtime\{
18
    ProcessInstanceInterface,
19
    ProcessInstanceQueryInterface
20
};
21
22
class ProcessInstanceQueryImpl extends AbstractVariableQueryImpl implements ProcessInstanceQueryInterface, \Serializable
23
{
24
    protected $processInstanceId;
25
    protected $businessKey;
26
    protected $businessKeyLike;
27
    protected $processDefinitionId;
28
    protected $processInstanceIds = [];
29
    protected $processDefinitionKey;
30
    protected $processDefinitionKeys = [];
31
    protected $processDefinitionKeyNotIn = [];
32
    protected $deploymentId;
33
    protected $superProcessInstanceId;
34
    protected $subProcessInstanceId;
35
    protected $suspensionState;
36
    protected $withIncident;
37
    protected $incidentType;
38
    protected $incidentId;
39
    protected $incidentMessage;
40
    protected $incidentMessageLike;
41
    /*protected $caseInstanceId;
42
    protected $superCaseInstanceId;
43
    protected $subCaseInstanceId;*/
44
    protected $activityIds = [];
45
    protected $isRootProcessInstances;
46
    protected $isLeafProcessInstances;
47
48
    protected $isTenantIdSet = false;
49
    protected $tenantIds = [];
50
    protected $isProcessDefinitionWithoutTenantId = false;
51
52
    // or query /////////////////////////////
53
    protected $queries = [];//new ArrayList<>(Arrays.asList($this));
54
    protected $isOrQueryActive = false;
55
56
    public function __construct(CommandExecutorInterface $commandExecutor = null)
57
    {
58
        parent::__construct($commandExecutor);
59
        $this->queries[] = $this;
60
    }
61
62
    public function serialize()
63
    {
64
        $queries = [];
65
        foreach ($queries as $query) {
66
            $queries[] = serialize($query);
67
        }
68
        return json_encode([
69
            'processInstanceId' => $this->processInstanceId,
70
            'businessKey' => $this->businessKey,
71
            'businessKeyLike' => $this->businessKeyLike,
72
            'processDefinitionId' => $this->processDefinitionId,
73
            'processInstanceIds' => $this->processInstanceIds,
74
            'processDefinitionKey' => $this->processDefinitionKey,
75
            'processDefinitionKeys' => $this->processDefinitionKeys,
76
            'processDefinitionKeyNotIn' => $this->processDefinitionKeyNotIn,
77
            'deploymentId' => $this->deploymentId,
78
            'superProcessInstanceId' => $this->superProcessInstanceId,
79
            'subProcessInstanceId' => $this->subProcessInstanceId,
80
            'suspensionState' => serialize($this->suspensionState),
81
            'withIncident' => $this->withIncident,
82
            'incidentType' => $this->incidentType,
83
            'incidentId' => $this->incidentId,
84
            'incidentMessage' => $this->incidentMessage,
85
            'incidentMessageLike' => $this->incidentMessageLike,
86
            'activityIds' => $this->activityIds,
87
            'isRootProcessInstances' => $this->isRootProcessInstances,
88
            'isLeafProcessInstances' => $this->isLeafProcessInstances,
89
            'isTenantIdSet' => $this->isTenantIdSet,
90
            'tenantIds' => $this->tenantIds,
91
            'isProcessDefinitionWithoutTenantId' => $this->isProcessDefinitionWithoutTenantId,
92
            'queries' => $queries,
93
            'isOrQueryActive' => $this->isOrQueryActive,
94
        ]);
95
    }
96
97
    public function unserialize($data)
98
    {
99
        $json = json_decode($data);
0 ignored issues
show
Unused Code introduced by
The assignment to $json is dead and can be removed.
Loading history...
100
        $queries = [];
101
        foreach ($data->queries as $query) {
102
            $queries[] = unserialize($query);
103
        }
104
105
        $this->processInstanceId = $data->processInstanceId;
106
        $this->businessKey = $data->businessKey;
107
        $this->businessKeyLike = $data->businessKeyLike;
108
        $this->processDefinitionId = $data->processDefinitionId;
109
        $this->processInstanceIds = $data->processInstanceIds;
110
        $this->processDefinitionKey = $data->processDefinitionKey;
111
        $this->processDefinitionKeys = $data->processDefinitionKeys;
112
        $this->processDefinitionKeyNotIn = $data->processDefinitionKeyNotIn;
113
        $this->deploymentId = $data->deploymentId;
114
        $this->superProcessInstanceId = $data->superProcessInstanceId;
115
        $this->suspensionState = unserialize($data->suspensionState);
116
        $this->withIncident = $data->withIncident;
117
        $this->incidentType = $data->incidentType;
118
        $this->incidentId = $data->incidentId;
119
        $this->incidentMessage = $data->incidentMessage;
120
        $this->incidentMessageLike = $data->incidentMessageLike;
121
        $this->activityIds = $data->activityIds;
122
        $this->isRootProcessInstances = $data->isRootProcessInstances;
123
        $this->isLeafProcessInstances = $data->isLeafProcessInstances;
124
        $this->isTenantIdSet = $data->isTenantIdSet;
125
        $this->tenantIds = $data->tenantIds;
126
        $this->isProcessDefinitionWithoutTenantId = $data->isProcessDefinitionWithoutTenantId;
127
        $this->queries = $queries;
128
        $this->isOrQueryActive = $data->isOrQueryActive;
129
    }
130
131
    public function processInstanceId(string $processInstanceId): ProcessInstanceQueryImpl
132
    {
133
        EnsureUtil::ensureNotNull("Process instance id", "processInstanceId", $processInstanceId);
134
        $this->processInstanceId = $processInstanceId;
135
        return $this;
136
    }
137
138
    public function processInstanceIds(array $processInstanceIds): ProcessInstanceQueryInterface
139
    {
140
        EnsureUtil::ensureNotEmpty("Set of process instance ids", "processInstanceIds", $processInstanceIds);
141
        $this->processInstanceIds = $processInstanceIds;
142
        return $this;
143
    }
144
145
    public function processInstanceBusinessKey(string $businessKey, string $processDefinitionKey = null): ProcessInstanceQueryInterface
146
    {
147
        EnsureUtil::ensureNotNull("Business key", "businessKey", $businessKey);
148
        $this->businessKey = $businessKey;
149
        $this->processDefinitionKey = $processDefinitionKey;
150
        return $this;
151
    }
152
153
    public function processInstanceBusinessKeyLike(string $businessKeyLike): ProcessInstanceQueryInterface
154
    {
155
        $this->businessKeyLike = $businessKeyLike;
156
        return $this;
157
    }
158
159
    public function processDefinitionId(string $processDefinitionId): ProcessInstanceQueryImpl
160
    {
161
        EnsureUtil::ensureNotNull("Process definition id", "processDefinitionId", $processDefinitionId);
162
        $this->processDefinitionId = $processDefinitionId;
163
        return $this;
164
    }
165
166
    public function processDefinitionKey(string $processDefinitionKey): ProcessInstanceQueryImpl
167
    {
168
        EnsureUtil::ensureNotNull("Process definition key", "processDefinitionKey", $processDefinitionKey);
169
        $this->processDefinitionKey = $processDefinitionKey;
170
        return $this;
171
    }
172
173
    public function processDefinitionKeyIn(array $processDefinitionKeys): ProcessInstanceQueryInterface
174
    {
175
        EnsureUtil::ensureNotNull("processDefinitionKeys", "processDefinitionKeys", $processDefinitionKeys);
176
        $this->processDefinitionKeys = $processDefinitionKeys;
177
        return $this;
178
    }
179
180
    public function processDefinitionKeyNotIn(array $processDefinitionKeys): ProcessInstanceQueryInterface
181
    {
182
        EnsureUtil::ensureNotNull("processDefinitionKeyNotIn", "processDefinitionKeys", $processDefinitionKeys);
183
        $this->processDefinitionKeyNotIn = $processDefinitionKeys;
184
        return $this;
185
    }
186
187
    public function deploymentId(string $deploymentId): ProcessInstanceQueryInterface
188
    {
189
        EnsureUtil::ensureNotNull("Deployment id", "deploymentId", $deploymentId);
190
        $this->deploymentId = $deploymentId;
191
        return $this;
192
    }
193
194
    public function superProcessInstanceId(string $superProcessInstanceId): ProcessInstanceQueryInterface
195
    {
196
        if ($this->isRootProcessInstances) {
197
            throw new ProcessEngineException("Invalid query usage: cannot set both rootProcessInstances and superProcessInstanceId");
198
        }
199
        $this->superProcessInstanceId = $superProcessInstanceId;
200
        return $this;
201
    }
202
203
    public function subProcessInstanceId(string $subProcessInstanceId): ProcessInstanceQueryInterface
204
    {
205
        $this->subProcessInstanceId = $subProcessInstanceId;
206
        return $this;
207
    }
208
209
    /*public function caseInstanceId(string $caseInstanceId): ProcessInstanceQueryInterface
210
    {
211
        EnsureUtil::ensureNotNull("caseInstanceId", caseInstanceId);
212
        $this->caseInstanceId = caseInstanceId;
213
        return $this;
214
    }
215
216
    public ProcessInstanceQueryInterface superCaseInstanceId(string $superCaseInstanceId) {
217
        EnsureUtil::ensureNotNull("superCaseInstanceId", superCaseInstanceId);
218
        $this->superCaseInstanceId = superCaseInstanceId;
219
        return $this;
220
    }
221
222
    public ProcessInstanceQueryInterface subCaseInstanceId(string $subCaseInstanceId) {
223
        EnsureUtil::ensureNotNull("subCaseInstanceId", subCaseInstanceId);
224
        $this->subCaseInstanceId = subCaseInstanceId;
225
        return $this;
226
    }*/
227
228
    public function orderByProcessInstanceId(): ProcessInstanceQueryInterface
229
    {
230
        if ($this->isOrQueryActive) {
231
            throw new ProcessEngineException("Invalid query usage: cannot set orderByProcessInstanceId() within 'or' query");
232
        }
233
234
        $this->orderBy(ProcessInstanceQueryProperty::processInstanceId());
235
        return $this;
236
    }
237
238
    public function orderByProcessDefinitionId(): ProcessInstanceQueryInterface
239
    {
240
        if ($this->isOrQueryActive) {
241
            throw new ProcessEngineException("Invalid query usage: cannot set orderByProcessDefinitionId() within 'or' query");
242
        }
243
244
        $this->orderBy(
245
            new QueryOrderingProperty(
246
                QueryOrderingProperty::relationProcessDefinition(),
0 ignored issues
show
Bug introduced by
The method relationProcessDefinition() does not exist on Jabe\Impl\QueryOrderingProperty. ( Ignorable by Annotation )

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

246
                QueryOrderingProperty::/** @scrutinizer ignore-call */ 
247
                                       relationProcessDefinition(),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
247
                ProcessInstanceQueryProperty::processDefinitionId()
248
            )
249
        );
250
        return $this;
251
    }
252
253
    public function orderByProcessDefinitionKey(): ProcessInstanceQueryInterface
254
    {
255
        if ($this->isOrQueryActive) {
256
            throw new ProcessEngineException("Invalid query usage: cannot set orderByProcessDefinitionKey() within 'or' query");
257
        }
258
259
        $this->orderBy(
260
            new QueryOrderingProperty(
261
                QueryOrderingProperty::relationProcessDefinition(),
262
                ProcessInstanceQueryProperty::processDefinitionKey()
263
            )
264
        );
265
        return $this;
266
    }
267
268
    public function orderByTenantId(): ProcessInstanceQueryInterface
269
    {
270
        if ($this->isOrQueryActive) {
271
            throw new ProcessEngineException("Invalid query usage: cannot set orderByTenantId() within 'or' query");
272
        }
273
274
        $this->orderBy(ProcessInstanceQueryProperty::tenantId());
275
        return $this;
276
    }
277
278
    public function orderByBusinessKey(): ProcessInstanceQueryInterface
279
    {
280
        if ($this->isOrQueryActive) {
281
            throw new ProcessEngineException("Invalid query usage: cannot set orderByBusinessKey() within 'or' query");
282
        }
283
284
        $this->orderBy(ProcessInstanceQueryProperty::businessKey());
285
        return $this;
286
    }
287
288
    public function active(): ProcessInstanceQueryInterface
289
    {
290
        $this->suspensionState = SuspensionState::active();
291
        return $this;
292
    }
293
294
    public function suspended(): ProcessInstanceQueryInterface
295
    {
296
        $this->suspensionState = SuspensionState::suspended();
297
        return $this;
298
    }
299
300
    public function withIncident(): ProcessInstanceQueryInterface
301
    {
302
        $this->withIncident = true;
303
        return $this;
304
    }
305
306
    public function incidentType(string $incidentType): ProcessInstanceQueryInterface
307
    {
308
        EnsureUtil::ensureNotNull("incident type", "incidentType", $incidentType);
309
        $this->incidentType = $incidentType;
310
        return $this;
311
    }
312
313
    public function incidentId(string $incidentId): ProcessInstanceQueryInterface
314
    {
315
        EnsureUtil::ensureNotNull("incident id", "incidentId", $incidentId);
316
        $this->incidentId = $incidentId;
317
        return $this;
318
    }
319
320
    public function incidentMessage(string $incidentMessage): ProcessInstanceQueryInterface
321
    {
322
        EnsureUtil::ensureNotNull("incident message", "incidentMessage", $incidentMessage);
323
        $this->incidentMessage = $incidentMessage;
324
        return $this;
325
    }
326
327
    public function incidentMessageLike(string $incidentMessageLike): ProcessInstanceQueryInterface
328
    {
329
        EnsureUtil::ensureNotNull("incident messageLike", "incidentMessageLike", $incidentMessageLike);
330
        $this->incidentMessageLike = $incidentMessageLike;
331
        return $this;
332
    }
333
334
    public function tenantIdIn(array $tenantIds): ProcessInstanceQueryInterface
335
    {
336
        EnsureUtil::ensureNotNull("tenantIds", "tenantIds", $tenantIds);
337
        $this->tenantIds = $tenantIds;
338
        $this->isTenantIdSet = true;
339
        return $this;
340
    }
341
342
    public function withoutTenantId(): ProcessInstanceQueryInterface
343
    {
344
        $this->tenantIds = null;
345
        $this->isTenantIdSet = true;
346
        return $this;
347
    }
348
349
    public function activityIdIn(array $activityIds): ProcessInstanceQueryInterface
350
    {
351
        EnsureUtil::ensureNotNull("activity ids", "activityIds", $activityIds);
352
        $this->activityIds = $activityIds;
353
        return $this;
354
    }
355
356
    public function rootProcessInstances(): ProcessInstanceQueryInterface
357
    {
358
        if ($this->superProcessInstanceId !== null) {
359
            throw new ProcessEngineException("Invalid query usage: cannot set both rootProcessInstances and superProcessInstanceId");
360
        }
361
        $this->isRootProcessInstances = true;
362
        return $this;
363
    }
364
365
    public function leafProcessInstances(): ProcessInstanceQueryInterface
366
    {
367
        if ($this->subProcessInstanceId !== null) {
368
            throw new ProcessEngineException("Invalid query usage: cannot set both leafProcessInstances and subProcessInstanceId");
369
        }
370
        $this->isLeafProcessInstances = true;
371
        return $this;
372
    }
373
374
    public function processDefinitionWithoutTenantId(): ProcessInstanceQueryInterface
375
    {
376
        $this->isProcessDefinitionWithoutTenantId = true;
377
        return $this;
378
    }
379
380
    //results /////////////////////////////////////////////////////////////////
381
382
    protected function checkQueryOk(): void
383
    {
384
        $this->ensureVariablesInitialized();
385
386
        parent::checkQueryOk();
387
    }
388
389
    public function executeCount(CommandContext $commandContext): int
390
    {
391
        $this->checkQueryOk();
392
393
        return $commandContext
394
            ->getExecutionManager()
395
            ->findProcessInstanceCountByQueryCriteria($this);
396
    }
397
398
    public function executeList(CommandContext $commandContext, Page $page): array
399
    {
400
        $this->checkQueryOk();
401
402
        return $commandContext
403
            ->getExecutionManager()
404
            ->findProcessInstancesByQueryCriteria($this, $page);
405
    }
406
407
    public function executeIdsList(CommandContext $commandContext): array
408
    {
409
        $this->checkQueryOk();
410
411
        return $commandContext
412
            ->getExecutionManager()
413
            ->findProcessInstancesIdsByQueryCriteria($this);
414
    }
415
416
    public function executeDeploymentIdMappingsList(CommandContext $commandContext): array
417
    {
418
        $this->checkQueryOk();
419
420
        return $commandContext
421
            ->getExecutionManager()
422
            ->findDeploymentIdMappingsByQueryCriteria($this);
423
    }
424
425
    protected function ensureVariablesInitialized(): void
426
    {
427
        parent::ensureVariablesInitialized();
428
429
        if (!empty($this->queries)) {
430
            $processEngineConfiguration = Context::getProcessEngineConfiguration();
431
            $variableSerializers = $processEngineConfiguration->getVariableSerializers();
0 ignored issues
show
Bug introduced by
The method getVariableSerializers() does not exist on Jabe\Impl\Cfg\ProcessEngineConfigurationImpl. ( Ignorable by Annotation )

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

431
            /** @scrutinizer ignore-call */ 
432
            $variableSerializers = $processEngineConfiguration->getVariableSerializers();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
432
            $dbType = $processEngineConfiguration->getDatabaseType();
433
434
            foreach ($this->queries as $orQuery) {
435
                foreach ($orQuery->getQueryVariableValues() as $var) {
436
                    $var->initialize($variableSerializers, $dbType);
437
                }
438
            }
439
        }
440
    }
441
442
    //getters /////////////////////////////////////////////////////////////////
443
444
    public function getProcessInstanceId(): string
445
    {
446
        return $this->processInstanceId;
447
    }
448
449
    public function getProcessInstanceIds(): array
450
    {
451
        return $this->processInstanceIds;
452
    }
453
454
    public function getQueries(): array
455
    {
456
        return $this->queries;
457
    }
458
459
    public function addOrQuery(ProcessInstanceQueryImpl $orQuery): void
460
    {
461
        $orQuery->isOrQueryActive = true;
462
        $this->queries[] = $orQuery;
463
    }
464
465
    public function setOrQueryActive(): void
466
    {
467
        $this->isOrQueryActive = true;
468
    }
469
470
    public function isOrQueryActive(): bool
471
    {
472
        return $this->isOrQueryActive;
473
    }
474
475
    public function getActivityIds(): array
476
    {
477
        return $this->activityIds;
478
    }
479
480
    public function getBusinessKey(): string
481
    {
482
        return $this->businessKey;
483
    }
484
485
    public function getBusinessKeyLike(): string
486
    {
487
        return $this->businessKeyLike;
488
    }
489
490
    public function getProcessDefinitionId(): string
491
    {
492
        return $this->processDefinitionId;
493
    }
494
495
    public function getProcessDefinitionKey(): string
496
    {
497
        return $this->processDefinitionKey;
498
    }
499
500
    public function getProcessDefinitionKeys(): array
501
    {
502
        return $this->processDefinitionKeys;
503
    }
504
505
    public function getProcessDefinitionKeyNotIn(): array
506
    {
507
        return $this->processDefinitionKeyNotIn;
508
    }
509
510
    public function getDeploymentId(): string
511
    {
512
        return $this->deploymentId;
513
    }
514
515
    public function getSuperProcessInstanceId(): string
516
    {
517
        return $this->superProcessInstanceId;
518
    }
519
520
    public function getSubProcessInstanceId(): string
521
    {
522
        return $this->subProcessInstanceId;
523
    }
524
525
    public function getSuspensionState(): SuspensionState
526
    {
527
        return $this->suspensionState;
528
    }
529
530
    public function setSuspensionState(SuspensionState $suspensionState): void
531
    {
532
        $this->suspensionState = $suspensionState;
533
    }
534
535
    public function isWithIncident(): bool
536
    {
537
        return $this->withIncident;
538
    }
539
540
    public function getIncidentId(): string
541
    {
542
        return $this->incidentId;
543
    }
544
545
    public function getIncidentType(): string
546
    {
547
        return $this->incidentType;
548
    }
549
550
    public function getIncidentMessage(): string
551
    {
552
        return $this->incidentMessage;
553
    }
554
555
    public function getIncidentMessageLike(): string
556
    {
557
        return $this->incidentMessageLike;
558
    }
559
560
    /*public String getCaseInstanceId() {
561
        return caseInstanceId;
562
    }
563
564
    public String getSuperCaseInstanceId() {
565
        return superCaseInstanceId;
566
    }
567
568
    public String getSubCaseInstanceId() {
569
        return subCaseInstanceId;
570
    }*/
571
572
    public function isTenantIdSet(): bool
573
    {
574
        return $this->isTenantIdSet;
575
    }
576
577
    public function isRootProcessInstances(): bool
578
    {
579
        return $this->isRootProcessInstances;
580
    }
581
582
    public function isProcessDefinitionWithoutTenantId(): bool
583
    {
584
        return $this->isProcessDefinitionWithoutTenantId;
585
    }
586
587
    public function isLeafProcessInstances(): bool
588
    {
589
        return $this->isLeafProcessInstances;
590
    }
591
592
    public function getTenantIds(): array
593
    {
594
        return $this->tenantIds;
595
    }
596
597
    public function or(): ProcessInstanceQueryInterface
598
    {
599
        if ($this != $this->queries[0]) {
600
            throw new ProcessEngineException("Invalid query usage: cannot set or() within 'or' query");
601
        }
602
603
        $orQuery = new ProcessInstanceQueryImpl();
604
        $orQuery->isOrQueryActive = true;
605
        $orQuery->queries = $this->queries;
606
        $this->queries[] = $orQuery;
607
        return $orQuery;
608
    }
609
610
    public function endOr(): ProcessInstanceQueryInterface
611
    {
612
        if (!empty($this->queries) && $this != $this->queries[count($this->queries) - 1]) {
613
            throw new ProcessEngineException("Invalid query usage: cannot set endOr() before or()");
614
        }
615
616
        return $this->queries[0];
617
    }
618
}
619