Test Failed
Push — main ( 16fa93...92cbf8 )
by Bingo
06:44
created

UserOperationLogContextEntryBuilder::entry()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Jabe\Engine\Impl\OpLog;
4
5
use Jabe\Engine\History\{
6
    HistoricTaskInstanceInterface,
7
    UserOperationLogEntryInterface
8
};
9
use Jabe\Engine\Impl\Context\Context;
10
use Jabe\Engine\Impl\History\Event\HistoryEvent;
11
use Jabe\Engine\Impl\Persistence\Entity\{
12
    ExecutionEntity,
13
    ExternalTaskEntity,
14
    HistoricVariableInstanceEntity,
15
    JobDefinitionEntity,
16
    JobEntity,
17
    ProcessDefinitionEntity,
18
    PropertyChange,
19
    TaskEntity
20
};
21
use Jabe\Engine\Impl\Repository\ResourceDefinitionEntity;
22
23
class UserOperationLogContextEntryBuilder
24
{
25
    protected $entry;
26
27
    public static function entry(string $operationType, string $entityType): UserOperationLogContextEntryBuilder
28
    {
29
        $builder = new UserOperationLogContextEntryBuilder();
30
        $builder->entry = new UserOperationLogContextEntry($operationType, $entityType);
31
        return $builder;
32
    }
33
34
    public function inContextOf(/*JobEntity|JobDefinitionEntity*/$data, $arg2 = null, $arg3 = null): UserOperationLogContextEntryBuilder
35
    {
36
        if ($data instanceof JobEntity) {
37
            $job = $data;
38
            $this->entry->setJobDefinitionId($job->getJobDefinitionId());
39
            $this->entry->setProcessInstanceId($job->getProcessInstanceId());
40
            $this->entry->setProcessDefinitionId($job->getProcessDefinitionId());
41
            $this->entry->setProcessDefinitionKey($job->getProcessDefinitionKey());
42
            $this->entry->setDeploymentId($job->getDeploymentId());
43
44
            $execution = $job->getExecution();
45
            if ($execution !== null) {
46
                $this->entry->setRootProcessInstanceId($execution->getRootProcessInstanceId());
47
            }
48
49
            return $this;
50
        } elseif ($data instanceof JobDefinitionEntity) {
51
            $jobDefinition = $data;
52
            $this->entry->setJobDefinitionId($jobDefinition->getId());
53
            $this->entry->setProcessDefinitionId($jobDefinition->getProcessDefinitionId());
54
            $this->entry->setProcessDefinitionKey($jobDefinition->getProcessDefinitionKey());
55
56
            if ($jobDefinition->getProcessDefinitionId() !== null) {
0 ignored issues
show
introduced by
The condition $jobDefinition->getProcessDefinitionId() !== null is always true.
Loading history...
57
                $processDefinition = Context::getProcessEngineConfiguration()
58
                    ->getDeploymentCache()
0 ignored issues
show
Bug introduced by
The method getDeploymentCache() 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

58
                    ->/** @scrutinizer ignore-call */ getDeploymentCache()

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...
59
                    ->findDeployedProcessDefinitionById($jobDefinition->getProcessDefinitionId());
60
                $this->entry->setDeploymentId($processDefinition->getDeploymentId());
61
            }
62
            return $this;
63
        } elseif ($data instanceof ExecutionEntity) {
64
            if (is_array($arg2)) {
65
                $processInstance = $data;
66
                $propertyChanges = $arg2;
67
                if (empty($propertyChanges)) {
68
                    if (UserOperationLogEntryInterface::OPERATION_TYPE_CREATE == $this->entry->getOperationType()) {
69
                        $propertyChanges = PropertyChange::emptyChange();
70
                    }
71
                }
72
                $this->entry->setPropertyChanges($propertyChanges);
73
                $this->entry->setRootProcessInstanceId($processInstance->getRootProcessInstanceId());
74
                $this->entry->setProcessInstanceId($processInstance->getProcessInstanceId());
75
                $this->entry->setProcessDefinitionId($processInstance->getProcessDefinitionId());
76
                $this->entry->setExecutionId($processInstance->getId());
77
                //$this->entry->setCaseInstanceId($processInstance->getCaseInstanceId());
78
79
                $definition = $processInstance->getProcessDefinition();
80
                if ($definition != null) {
81
                    $this->entry->setProcessDefinitionKey($definition->getKey());
82
                    $this->entry->setDeploymentId($definition->getDeploymentId());
83
                }
84
85
                return $this;
86
            } else {
87
                $execution = $data;
88
                $this->entry->setProcessInstanceId($execution->getProcessInstanceId());
89
                $this->entry->setRootProcessInstanceId($execution->getRootProcessInstanceId());
90
                $this->entry->setProcessDefinitionId($execution->getProcessDefinitionId());
91
92
                $processDefinition = $execution->getProcessDefinition();
93
                $this->entry->setProcessDefinitionKey($processDefinition->getKey());
94
                $this->entry->setDeploymentId($processDefinition->getDeploymentId());
95
96
                return $this;
97
            }
98
        } elseif ($data instanceof ProcessDefinitionEntity) {
99
            $processDefinition = $data;
100
            $this->entry->setProcessDefinitionId($processDefinition->getId());
101
            $this->entry->setProcessDefinitionKey($processDefinition->getKey());
102
            $this->entry->setDeploymentId($processDefinition->getDeploymentId());
103
            return $this;
104
        } elseif ($data instanceof TaskEntity) {
105
            $task = $data;
106
            $propertyChanges = $arg2;
107
108
            if (empty($propertyChanges)) {
109
                if (UserOperationLogEntryInterface::OPERATION_TYPE_CREATE == $this->entry->getOperationType()) {
110
                    $propertyChanges = PropertyChange::emptyChange();
111
                }
112
            }
113
            $this->entry->setPropertyChanges($propertyChanges);
114
115
            $definition = $task->getProcessDefinition();
116
            if ($definition != null) {
117
                $this->entry->setProcessDefinitionKey($definition->getKey());
118
                $this->entry->setDeploymentId($definition->getDeploymentId());
119
            }/* elseif (task.getCaseDefinitionId() != null) {
120
                $this->entry->setDeploymentId(task.getCaseDefinition().getDeploymentId());
121
            }*/
122
123
            $this->entry->setProcessDefinitionId($task->getProcessDefinitionId());
124
            $this->entry->setProcessInstanceId($task->getProcessInstanceId());
125
            $this->entry->setExecutionId($task->getExecutionId());
126
            /*$this->entry->setCaseDefinitionId(task.getCaseDefinitionId());
127
            $this->entry->setCaseInstanceId(task.getCaseInstanceId());
128
            $this->entry->setCaseExecutionId(task.getCaseExecutionId());*/
129
            $this->entry->setTaskId($task->getId());
130
131
            $execution = $task->getExecution();
132
            if ($execution != null) {
133
                $this->entry->setRootProcessInstanceId($execution->getRootProcessInstanceId());
134
            }
135
136
            return $this;
137
        } elseif ($data instanceof HistoricTaskInstanceInterface) {
138
            $task = $data;
139
            $propertyChanges = $arg2;
140
            if (empty($propertyChanges)) {
141
                if (UserOperationLogEntryInterface::OPERATION_TYPE_CREATE == $this->entry->getOperationType()) {
142
                    $propertyChanges = PropertyChange::emptyChange();
143
                }
144
            }
145
            $this->entry->setPropertyChanges($propertyChanges);
146
            $this->entry->setProcessDefinitionKey($task->getProcessDefinitionKey());
147
            $this->entry->setProcessDefinitionId($task->getProcessDefinitionId());
148
            $this->entry->setProcessInstanceId($task->getProcessInstanceId());
149
            $this->entry->setExecutionId($task->getExecutionId());
150
            /*$this->entry->setCaseDefinitionId(task.getCaseDefinitionId());
151
            $this->entry->setCaseInstanceId(task.getCaseInstanceId());
152
            $this->entry->setCaseExecutionId(task.getCaseExecutionId());*/
153
            $this->entry->setTaskId($task->getId());
154
            $this->entry->setRootProcessInstanceId($task->getRootProcessInstanceId());
155
156
            return $this;
157
        } elseif ($data instanceof HistoryEvent) {
158
            $historyEvent = $data;
159
            $definition = $arg2;
160
            $propertyChanges = $arg3;
161
            if (empty($propertyChanges)) {
162
                if (UserOperationLogEntryInterface::OPERATION_TYPE_CREATE == $this->entry->getOperationType()) {
163
                    $propertyChanges = PropertyChange::emptyChange();
164
                }
165
            }
166
            $this->entry->setPropertyChanges($propertyChanges);
167
            $this->entry->setRootProcessInstanceId($historyEvent->getRootProcessInstanceId());
168
            $this->entry->setProcessDefinitionId($historyEvent->getProcessDefinitionId());
169
            $this->entry->setProcessInstanceId($historyEvent->getProcessInstanceId());
170
            $this->entry->setExecutionId($historyEvent->getExecutionId());
171
            /*$this->entry->setCaseDefinitionId(historyEvent.getCaseDefinitionId());
172
            $this->entry->setCaseInstanceId(historyEvent.getCaseInstanceId());
173
            $this->entry->setCaseExecutionId(historyEvent.getCaseExecutionId());*/
174
175
            if ($definition != null) {
176
                if ($definition instanceof ProcessDefinitionEntity) {
177
                    $this->entry->setProcessDefinitionKey($definition->getKey());
178
                }
179
                $this->entry->setDeploymentId($definition->getDeploymentId());
180
            }
181
182
            return $this;
183
        } elseif ($data instanceof HistoricVariableInstanceEntity) {
184
            $variable = $data;
185
            $definition = $arg2;
186
            $propertyChanges = $arg3;
187
            if (empty($propertyChanges)) {
188
                if (UserOperationLogEntryInterface::OPERATION_TYPE_CREATE == $this->entry->getOperationType()) {
189
                    $propertyChanges = PropertyChange::emptyChange();
190
                }
191
            }
192
            $this->entry->setPropertyChanges($propertyChanges);
193
            $this->entry->setRootProcessInstanceId($variable->getRootProcessInstanceId());
194
            $this->entry->setProcessDefinitionId($variable->getProcessDefinitionId());
195
            $this->entry->setProcessInstanceId($variable->getProcessInstanceId());
196
            $this->entry->setExecutionId($variable->getExecutionId());
197
            /*$this->entry->setCaseDefinitionId(variable.getCaseDefinitionId());
198
            $this->entry->setCaseInstanceId(variable.getCaseInstanceId());
199
            $this->entry->setCaseExecutionId(variable.getCaseExecutionId());*/
200
            $this->entry->setTaskId($variable->getTaskId());
201
202
            if ($definition != null) {
203
                if ($definition instanceof ProcessDefinitionEntity) {
204
                    $this->entry->setProcessDefinitionKey($definition->getKey());
205
                }
206
                $this->entry->setDeploymentId($definition->getDeploymentId());
207
            }
208
209
            return $this;
210
        } elseif ($data instanceof ExternalTaskEntity) {
211
            $task = $data;
212
            $execution = $arg2;
213
            $definition = $arg3;
214
            if ($execution !== null) {
215
                $this->inContextOf($execution);
216
            } elseif ($definition !== null) {
217
                $this->inContextOf($definition);
218
            }
219
            $this->entry->setExternalTaskId($task->getId());
220
            return $this;
221
        }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return Jabe\Engine\Impl\OpLog\U...nLogContextEntryBuilder. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
222
    }
223
224
    public function propertyChanges($propertyChanges): UserOperationLogContextEntryBuilder
225
    {
226
        if (is_array($propertyChanges)) {
227
            $this->entry->setPropertyChanges($propertyChanges);
228
            return $this;
229
        } else {
230
            $propertyChanges = [ $propertyChanges ];
231
            $this->entry->setPropertyChanges($propertyChanges);
232
            return $this;
233
        }
234
    }
235
236
    public function create(): UserOperationLogContextEntry
237
    {
238
        return $this->entry;
239
    }
240
241
    public function jobId(string $jobId): UserOperationLogContextEntryBuilder
242
    {
243
        $this->entry->setJobId($jobId);
244
        return $this;
245
    }
246
247
    public function jobDefinitionId(string $jobDefinitionId): UserOperationLogContextEntryBuilder
248
    {
249
        $this->entry->setJobDefinitionId($jobDefinitionId);
250
        return $this;
251
    }
252
253
    public function processDefinitionId(string $processDefinitionId): UserOperationLogContextEntryBuilder
254
    {
255
        $this->entry->setProcessDefinitionId($processDefinitionId);
256
        return $this;
257
    }
258
259
    public function processDefinitionKey(string $processDefinitionKey): UserOperationLogContextEntryBuilder
260
    {
261
        $this->entry->setProcessDefinitionKey($processDefinitionKey);
262
        return $this;
263
    }
264
265
    public function processInstanceId(string $processInstanceId): UserOperationLogContextEntryBuilder
266
    {
267
        $this->entry->setProcessInstanceId($processInstanceId);
268
        return $this;
269
    }
270
271
    /*public UserOperationLogContextEntryBuilder caseDefinitionId(string $caseDefinitionId) {
272
        $this->entry->setCaseDefinitionId(caseDefinitionId);
273
        return $this;
274
    }*/
275
276
    public function deploymentId(string $deploymentId): UserOperationLogContextEntryBuilder
277
    {
278
        $this->entry->setDeploymentId($deploymentId);
279
        return $this;
280
    }
281
282
    public function batchId(string $batchId): UserOperationLogContextEntryBuilder
283
    {
284
        $this->entry->setBatchId($batchId);
285
        return $this;
286
    }
287
288
    public function taskId(string $taskId): UserOperationLogContextEntryBuilder
289
    {
290
        $this->entry->setTaskId($taskId);
291
        return $this;
292
    }
293
294
    /*public UserOperationLogContextEntryBuilder caseInstanceId(string $caseInstanceId) {
295
        $this->entry->setCaseInstanceId(caseInstanceId);
296
        return $this;
297
    }*/
298
299
    public function category(string $category): UserOperationLogContextEntryBuilder
300
    {
301
        $this->entry->setCategory($category);
302
        return $this;
303
    }
304
305
    public function annotation(string $annotation): UserOperationLogContextEntryBuilder
306
    {
307
        $this->entry->setAnnotation($annotation);
308
        return $this;
309
    }
310
}
311