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

HistoricVariableInstanceQueryImpl   B

Complexity

Total Complexity 52

Size/Duplication

Total Lines 357
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 52
eloc 130
dl 0
loc 357
rs 7.44
c 0
b 0
f 0

43 Methods

Rating   Name   Duplication   Size   Complexity  
A getProcessInstanceId() 0 3 1
A isTenantIdSet() 0 3 1
A variableName() 0 5 1
A orderByTenantId() 0 4 1
A variableNameLike() 0 5 1
A getVariableValuesIgnoreCase() 0 3 1
A matchVariableValuesIgnoreCase() 0 7 2
A withoutTenantId() 0 5 1
A processDefinitionKey() 0 4 1
A lowerCase() 0 6 2
A __construct() 0 3 1
A orderByProcessInstanceId() 0 4 1
A getQueryVariableValue() 0 3 1
A variableNameIn() 0 5 1
A getProcessInstanceIds() 0 3 1
A disableBinaryFetching() 0 4 1
A processDefinitionId() 0 5 1
A getVariableName() 0 3 1
A processInstanceId() 0 5 1
A getProcessDefinitionKey() 0 3 1
A getActivityInstanceIds() 0 3 1
A disableCustomObjectDeserialization() 0 4 1
A getVariableNameLike() 0 3 1
A getVariableNamesIgnoreCase() 0 3 1
A variableValueEquals() 0 7 1
A tenantIdIn() 0 6 1
A activityInstanceIdIn() 0 5 1
A getTaskIds() 0 3 1
A variableId() 0 5 1
A shouldFetchValue() 0 5 2
A getVariableNameIn() 0 3 1
A includeDeleted() 0 4 1
A getProcessDefinitionId() 0 3 1
A getExecutionIds() 0 3 1
A processInstanceIdIn() 0 5 1
A executionIdIn() 0 5 1
A variableTypeIn() 0 5 1
A executeList() 0 22 5
A orderByVariableName() 0 4 1
A ensureVariablesInitialized() 0 7 2
A executeCount() 0 5 1
A taskIdIn() 0 5 1
A matchVariableNamesIgnoreCase() 0 7 2

How to fix   Complexity   

Complex Class

Complex classes like HistoricVariableInstanceQueryImpl often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use HistoricVariableInstanceQueryImpl, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Jabe\Engine\Impl;
4
5
use Jabe\Engine\Exception\NotValidException;
6
use Jabe\Engine\History\{
7
    HistoricVariableInstanceInterface,
8
    HistoricVariableInstanceQueryInterface
9
};
10
use Jabe\Engine\Impl\Cfg\ProcessEngineConfigurationImpl;
11
use Jabe\Engine\Impl\Context\Context;
12
use Jabe\Engine\Impl\Interceptor\{
13
    CommandContext,
14
    CommandExecutorInterface
15
};
16
use Jabe\Engine\Impl\Persistence\Entity\HistoricVariableInstanceEntity;
17
use Jabe\Engine\Impl\Util\EnsureUtil;
18
use Jabe\Engine\Impl\Variable\Serializer\{
19
    AbstractTypedValueSerializer,
20
    VariableSerializersInterface
21
};
22
23
class HistoricVariableInstanceQueryImpl extends AbstractQuery implements HistoricVariableInstanceQuery
24
{
25
    //private final static CommandLogger LOG = ProcessEngineLogger.CMD_LOGGER;
26
27
    protected $variableNameIn = [];
28
    protected $variableId;
29
    protected $processInstanceId;
30
    protected $processDefinitionId;
31
    protected $processDefinitionKey;
32
    protected $caseInstanceId;
33
    protected $variableName;
34
    protected $variableNameLike;
35
    protected $queryVariableValue;
36
    protected $variableNamesIgnoreCase;
37
    protected $variableValuesIgnoreCase;
38
    protected $variableTypes = [];
39
    protected $taskIds = [];
40
    protected $executionIds = [];
41
    //protected String[] caseExecutionIds;
42
    //protected String[] caseActivityIds;
43
    protected $activityInstanceIds = [];
44
45
    protected $tenantIds = [];
46
    protected $isTenantIdSet;
47
48
    protected $processInstanceIds = [];
49
    protected $includeDeleted = false;
50
51
    protected $isByteArrayFetchingEnabled = true;
52
    protected $isCustomObjectDeserializationEnabled = true;
53
54
    public function __construct(CommandExecutorInterface $commandExecutor = null)
55
    {
56
        parent::__construct($commandExecutor);
57
    }
58
59
    public function variableNameIn(array $names): HistoricVariableInstanceQueryInterface
60
    {
61
        EnsureUtil::ensureNotNull("Variable names", "names", $names);
62
        $this->variableNameIn = $names;
63
        return $this;
64
    }
65
66
    public function variableId(string $id): HistoricVariableInstanceQueryInterface
67
    {
68
        EnsureUtil::ensureNotNull("variableId", "variableId", $id);
69
        $this->variableId = $id;
70
        return $this;
71
    }
72
73
    public function processInstanceId(string $processInstanceId): HistoricVariableInstanceQueryImpl
74
    {
75
        EnsureUtil::ensureNotNull("processInstanceId", "processInstanceId", $processInstanceId);
76
        $this->processInstanceId = $processInstanceId;
77
        return $this;
78
    }
79
80
    public function processDefinitionId(string $processDefinitionId): HistoricVariableInstanceQueryInterface
81
    {
82
        EnsureUtil::ensureNotNull("processDefinitionId", "processDefinitionId", $processDefinitionId);
83
        $this->processDefinitionId = $processDefinitionId;
84
        return $this;
85
    }
86
87
    public function processDefinitionKey(string $processDefinitionKey): HistoricVariableInstanceQueryInterface
88
    {
89
        $this->processDefinitionKey = $processDefinitionKey;
90
        return $this;
91
    }
92
93
    /*public HistoricVariableInstanceQueryInterface caseInstanceId(string $caseInstanceId) {
94
        EnsureUtil::ensureNotNull("caseInstanceId", caseInstanceId);
95
        $this->caseInstanceId = caseInstanceId;
96
        return $this;
97
    }*/
98
99
    public function variableTypeIn(array $variableTypes): HistoricVariableInstanceQueryInterface
100
    {
101
        EnsureUtil::ensureNotNull("Variable types", "variableTypes", $variableTypes);
102
        $this->variableTypes = $this->lowerCase($variableTypes);
103
        return $this;
104
    }
105
106
    public function matchVariableNamesIgnoreCase(): HistoricVariableInstanceQueryInterface
107
    {
108
        $this->variableNamesIgnoreCase = true;
109
        if ($this->queryVariableValue != null) {
110
            $this->queryVariableValue->variableNameIgnoreCase = true;
111
        }
112
        return $this;
113
    }
114
115
    public function matchVariableValuesIgnoreCase(): HistoricVariableInstanceQueryInterface
116
    {
117
        $this->variableValuesIgnoreCase = true;
118
        if ($this->queryVariableValue != null) {
119
            $this->queryVariableValue->variableValueIgnoreCase = true;
120
        }
121
        return $this;
122
    }
123
124
    private function lowerCase(array $variableTypes): array
125
    {
126
        for ($i = 0; $i < count($variableTypes); $i += 1) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
127
            $variableTypes[$i] = strtolower($variableTypes[$i]);
128
        }
129
        return $variableTypes;
130
    }
131
132
    /** Only select historic process variables with the given process instance ids. */
133
    public function processInstanceIdIn(array $processInstanceIds): HistoricVariableInstanceQueryInterface
134
    {
135
        EnsureUtil::ensureNotNull("Process Instance Ids", "processInstanceIds", $processInstanceIds);
136
        $this->processInstanceIds = $processInstanceIds;
137
        return $this;
138
    }
139
140
    public function taskIdIn(array $taskIds): HistoricVariableInstanceQueryInterface
141
    {
142
        EnsureUtil::ensureNotNull("Task Ids", "taskIds", $taskIds);
143
        $this->taskIds = $taskIds;
144
        return $this;
145
    }
146
147
    public function executionIdIn(array $executionIds): HistoricVariableInstanceQueryInterface
148
    {
149
        EnsureUtil::ensureNotNull("Execution Ids", "executionIds", $executionIds);
150
        $this->executionIds = $executionIds;
151
        return $this;
152
    }
153
154
    /*public function caseExecutionIdIn(array $caseExecutionIds) {
155
        EnsureUtil::ensureNotNull("Case execution ids", (Object[]) caseExecutionIds);
156
        $this->caseExecutionIds = caseExecutionIds;
157
        return $this;
158
    }
159
160
    public HistoricVariableInstanceQueryInterface caseActivityIdIn(array $caseActivityIds) {
161
        EnsureUtil::ensureNotNull("Case activity ids", (Object[]) caseActivityIds);
162
        $this->caseActivityIds = caseActivityIds;
163
        return $this;
164
    }*/
165
166
    public function activityInstanceIdIn(array $activityInstanceIds): HistoricVariableInstanceQueryInterface
167
    {
168
        EnsureUtil::ensureNotNull("Activity Instance Ids", "activityInstanceIds", $activityInstanceIds);
169
        $this->activityInstanceIds = $activityInstanceIds;
170
        return $this;
171
    }
172
173
    public function variableName(string $variableName): HistoricVariableInstanceQueryInterface
174
    {
175
        EnsureUtil::ensureNotNull("variableName", "variableName", $variableName);
176
        $this->variableName = $variableName;
177
        return $this;
178
    }
179
180
    public function variableValueEquals(string $variableName, $variableValue): HistoricVariableInstanceQueryInterface
181
    {
182
        EnsureUtil::ensureNotNull("variableName", "variableName", $variableName);
183
        EnsureUtil::ensureNotNull("variableValue", "variableValue", $variableValue);
184
        $this->variableName = $variableName;
185
        $this->queryVariableValue = new QueryVariableValue($variableName, $variableValue, QueryOperator::EQUALS, true, $this->variableNamesIgnoreCase, $this->variableValuesIgnoreCase);
186
        return $this;
187
    }
188
189
    public function variableNameLike(string $variableNameLike): HistoricVariableInstanceQueryInterface
190
    {
191
        EnsureUtil::ensureNotNull("variableNameLike", "variableNameLike", $variableNameLike);
192
        $this->variableNameLike = $variableNameLike;
193
        return $this;
194
    }
195
196
    protected function ensureVariablesInitialized(): void
197
    {
198
        if ($this->queryVariableValue != null) {
199
            $processEngineConfiguration = Context::getProcessEngineConfiguration();
200
            $variableSerializers = $processEngineConfiguration->getVariableSerializers();
0 ignored issues
show
Bug introduced by
The method getVariableSerializers() does not exist on Jabe\Engine\Impl\Cfg\Pro...EngineConfigurationImpl. ( Ignorable by Annotation )

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

200
            /** @scrutinizer ignore-call */ 
201
            $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...
201
            $dbType = $processEngineConfiguration->getDatabaseType();
202
            $this->queryVariableValue->initialize($variableSerializers, $dbType);
203
        }
204
    }
205
206
    public function disableBinaryFetching(): HistoricVariableInstanceQueryInterface
207
    {
208
        $this->isByteArrayFetchingEnabled = false;
209
        return $this;
210
    }
211
212
    public function disableCustomObjectDeserialization(): HistoricVariableInstanceQueryInterface
213
    {
214
        $this->isCustomObjectDeserializationEnabled = false;
215
        return $this;
216
    }
217
218
    public function tenantIdIn(array $tenantIds): HistoricVariableInstanceQueryInterface
219
    {
220
        EnsureUtil::ensureNotNull("tenantIds", "tenantIds", $tenantIds);
221
        $this->tenantIds = $tenantIds;
222
        $this->isTenantIdSet = true;
223
        return $this;
224
    }
225
226
    public function withoutTenantId(): HistoricVariableInstanceQueryInterface
227
    {
228
        $this->tenantIds = null;
229
        $this->isTenantIdSet = true;
230
        return $this;
231
    }
232
233
    public function executeCount(CommandContext $commandContext): int
234
    {
235
        $this->checkQueryOk();
236
        $this->ensureVariablesInitialized();
237
        return $commandContext->getHistoricVariableInstanceManager()->findHistoricVariableInstanceCountByQueryCriteria($this);
238
    }
239
240
    public function executeList(CommandContext $commandContext, Page $page): array
241
    {
242
        $this->checkQueryOk();
243
        $this->ensureVariablesInitialized();
244
        $historicVariableInstances = $commandContext
245
                ->getHistoricVariableInstanceManager()
246
                ->findHistoricVariableInstancesByQueryCriteria($this, $page);
247
248
        if (!empty($historicVariableInstances)) {
249
            foreach ($historicVariableInstances as $historicVariableInstance) {
250
                $variableInstanceEntity = $historicVariableInstance;
251
                if ($this->shouldFetchValue($variableInstanceEntity)) {
252
                    try {
253
                        $variableInstanceEntity->getTypedValue($this->isCustomObjectDeserializationEnabled);
254
                    } catch (\Exception $t) {
255
                        // do not fail if one of the variables fails to load
256
                        //LOG.exceptionWhileGettingValueForVariable(t);
257
                    }
258
                }
259
            }
260
        }
261
        return $historicVariableInstances;
262
    }
263
264
    protected function shouldFetchValue(HistoricVariableInstanceEntity $entity): bool
265
    {
266
        // do not fetch values for byte arrays eagerly (unless requested by the user)
267
        return $this->isByteArrayFetchingEnabled
268
            || !in_array($entity->getSerializer()->getType()->getName(), AbstractTypedValueSerializer::BINARY_VALUE_TYPES);
269
    }
270
271
    // order by /////////////////////////////////////////////////////////////////
272
273
    public function orderByProcessInstanceId(): HistoricVariableInstanceQueryInterface
274
    {
275
        $this->orderBy(HistoricVariableInstanceQueryProperty::processInstanceId());
276
        return $this;
277
    }
278
279
    public function orderByVariableName(): HistoricVariableInstanceQueryInterface
280
    {
281
        $this->orderBy(HistoricVariableInstanceQueryProperty::variableName());
282
        return $this;
283
    }
284
285
    public function orderByTenantId(): HistoricVariableInstanceQueryInterface
286
    {
287
        $this->orderBy(HistoricVariableInstanceQueryProperty::tenantId());
288
        return $this;
289
    }
290
291
    // getters and setters //////////////////////////////////////////////////////
292
293
    public function getProcessInstanceId(): string
294
    {
295
        return $this->processInstanceId;
296
    }
297
298
    /*public String getCaseInstanceId() {
299
        return caseInstanceId;
300
    }*/
301
302
    public function getActivityInstanceIds(): array
303
    {
304
        return $this->activityInstanceIds;
305
    }
306
307
    public function getProcessInstanceIds(): array
308
    {
309
        return $this->processInstanceIds;
310
    }
311
312
    public function getTaskIds(): array
313
    {
314
        return $this->taskIds;
315
    }
316
317
    public function getExecutionIds(): array
318
    {
319
        return $this->executionIds;
320
    }
321
322
    /*public String[] getCaseExecutionIds() {
323
        return caseExecutionIds;
324
    }
325
326
    public function getCaseActivityIds(): array
327
    {
328
        return caseActivityIds;
329
    }*/
330
331
    public function isTenantIdSet(): bool
332
    {
333
        return $this->isTenantIdSet;
334
    }
335
336
    public function getVariableName(): string
337
    {
338
        return $this->variableName;
339
    }
340
341
    public function getVariableNameLike(): string
342
    {
343
        return $this->variableNameLike;
344
    }
345
346
    public function getQueryVariableValue(): QueryVariableValue
347
    {
348
        return $this->queryVariableValue;
349
    }
350
351
    public function getVariableNamesIgnoreCase(): bool
352
    {
353
        return $this->variableNamesIgnoreCase;
354
    }
355
356
    public function getVariableValuesIgnoreCase(): bool
357
    {
358
        return $this->variableValuesIgnoreCase;
359
    }
360
361
    public function includeDeleted(): HistoricVariableInstanceQueryInterface
362
    {
363
        $this->includeDeleted = true;
364
        return $this;
365
    }
366
367
    public function getProcessDefinitionId(): string
368
    {
369
        return $this->processDefinitionId;
370
    }
371
372
    public function getProcessDefinitionKey(): string
373
    {
374
        return $this->processDefinitionKey;
375
    }
376
377
    public function getVariableNameIn(): array
378
    {
379
        return $this->variableNameIn;
380
    }
381
}
382