| Total Complexity | 46 |
| Total Lines | 286 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like UserOperationLogContextEntryBuilder 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 UserOperationLogContextEntryBuilder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class UserOperationLogContextEntryBuilder |
||
| 23 | { |
||
| 24 | protected $entry; |
||
| 25 | |||
| 26 | public static function entry(string $operationType, string $entityType): UserOperationLogContextEntryBuilder |
||
| 27 | { |
||
| 28 | $builder = new UserOperationLogContextEntryBuilder(); |
||
| 29 | $builder->entry = new UserOperationLogContextEntry($operationType, $entityType); |
||
| 30 | return $builder; |
||
| 31 | } |
||
| 32 | |||
| 33 | public function inContextOf(/*JobEntity|JobDefinitionEntity*/$data, $arg2 = null, $arg3 = null): UserOperationLogContextEntryBuilder |
||
| 34 | { |
||
| 35 | if ($data instanceof JobEntity) { |
||
| 36 | $job = $data; |
||
| 37 | $this->entry->setJobDefinitionId($job->getJobDefinitionId()); |
||
| 38 | $this->entry->setProcessInstanceId($job->getProcessInstanceId()); |
||
| 39 | $this->entry->setProcessDefinitionId($job->getProcessDefinitionId()); |
||
| 40 | $this->entry->setProcessDefinitionKey($job->getProcessDefinitionKey()); |
||
| 41 | $this->entry->setDeploymentId($job->getDeploymentId()); |
||
| 42 | |||
| 43 | $execution = $job->getExecution(); |
||
| 44 | if ($execution !== null) { |
||
| 45 | $this->entry->setRootProcessInstanceId($execution->getRootProcessInstanceId()); |
||
| 46 | } |
||
| 47 | |||
| 48 | return $this; |
||
| 49 | } elseif ($data instanceof JobDefinitionEntity) { |
||
| 50 | $jobDefinition = $data; |
||
| 51 | $this->entry->setJobDefinitionId($jobDefinition->getId()); |
||
| 52 | $this->entry->setProcessDefinitionId($jobDefinition->getProcessDefinitionId()); |
||
| 53 | $this->entry->setProcessDefinitionKey($jobDefinition->getProcessDefinitionKey()); |
||
| 54 | |||
| 55 | if ($jobDefinition->getProcessDefinitionId() !== null) { |
||
|
|
|||
| 56 | $processDefinition = Context::getProcessEngineConfiguration() |
||
| 57 | ->getDeploymentCache() |
||
| 58 | ->findDeployedProcessDefinitionById($jobDefinition->getProcessDefinitionId()); |
||
| 59 | $this->entry->setDeploymentId($processDefinition->getDeploymentId()); |
||
| 60 | } |
||
| 61 | return $this; |
||
| 62 | } elseif ($data instanceof ExecutionEntity) { |
||
| 63 | if (is_array($arg2)) { |
||
| 64 | $processInstance = $data; |
||
| 65 | $propertyChanges = $arg2; |
||
| 66 | if (empty($propertyChanges)) { |
||
| 67 | if (UserOperationLogEntryInterface::OPERATION_TYPE_CREATE == $this->entry->getOperationType()) { |
||
| 68 | $propertyChanges = PropertyChange::emptyChange(); |
||
| 69 | } |
||
| 70 | } |
||
| 71 | $this->entry->setPropertyChanges($propertyChanges); |
||
| 72 | $this->entry->setRootProcessInstanceId($processInstance->getRootProcessInstanceId()); |
||
| 73 | $this->entry->setProcessInstanceId($processInstance->getProcessInstanceId()); |
||
| 74 | $this->entry->setProcessDefinitionId($processInstance->getProcessDefinitionId()); |
||
| 75 | $this->entry->setExecutionId($processInstance->getId()); |
||
| 76 | //$this->entry->setCaseInstanceId($processInstance->getCaseInstanceId()); |
||
| 77 | |||
| 78 | $definition = $processInstance->getProcessDefinition(); |
||
| 79 | if ($definition !== null) { |
||
| 80 | $this->entry->setProcessDefinitionKey($definition->getKey()); |
||
| 81 | $this->entry->setDeploymentId($definition->getDeploymentId()); |
||
| 82 | } |
||
| 83 | |||
| 84 | return $this; |
||
| 85 | } else { |
||
| 86 | $execution = $data; |
||
| 87 | $this->entry->setProcessInstanceId($execution->getProcessInstanceId()); |
||
| 88 | $this->entry->setRootProcessInstanceId($execution->getRootProcessInstanceId()); |
||
| 89 | $this->entry->setProcessDefinitionId($execution->getProcessDefinitionId()); |
||
| 90 | |||
| 91 | $processDefinition = $execution->getProcessDefinition(); |
||
| 92 | $this->entry->setProcessDefinitionKey($processDefinition->getKey()); |
||
| 93 | $this->entry->setDeploymentId($processDefinition->getDeploymentId()); |
||
| 94 | |||
| 95 | return $this; |
||
| 96 | } |
||
| 97 | } elseif ($data instanceof ProcessDefinitionEntity) { |
||
| 98 | $processDefinition = $data; |
||
| 99 | $this->entry->setProcessDefinitionId($processDefinition->getId()); |
||
| 100 | $this->entry->setProcessDefinitionKey($processDefinition->getKey()); |
||
| 101 | $this->entry->setDeploymentId($processDefinition->getDeploymentId()); |
||
| 102 | return $this; |
||
| 103 | } elseif ($data instanceof TaskEntity) { |
||
| 104 | $task = $data; |
||
| 105 | $propertyChanges = $arg2; |
||
| 106 | |||
| 107 | if (empty($propertyChanges)) { |
||
| 108 | if (UserOperationLogEntryInterface::OPERATION_TYPE_CREATE == $this->entry->getOperationType()) { |
||
| 109 | $propertyChanges = PropertyChange::emptyChange(); |
||
| 110 | } |
||
| 111 | } |
||
| 112 | $this->entry->setPropertyChanges($propertyChanges); |
||
| 113 | |||
| 114 | $definition = $task->getProcessDefinition(); |
||
| 115 | if ($definition !== null) { |
||
| 116 | $this->entry->setProcessDefinitionKey($definition->getKey()); |
||
| 117 | $this->entry->setDeploymentId($definition->getDeploymentId()); |
||
| 118 | }/* elseif (task.getCaseDefinitionId() !== null) { |
||
| 119 | $this->entry->setDeploymentId(task.getCaseDefinition().getDeploymentId()); |
||
| 120 | }*/ |
||
| 121 | |||
| 122 | $this->entry->setProcessDefinitionId($task->getProcessDefinitionId()); |
||
| 123 | $this->entry->setProcessInstanceId($task->getProcessInstanceId()); |
||
| 124 | $this->entry->setExecutionId($task->getExecutionId()); |
||
| 125 | /*$this->entry->setCaseDefinitionId(task.getCaseDefinitionId()); |
||
| 126 | $this->entry->setCaseInstanceId(task.getCaseInstanceId()); |
||
| 127 | $this->entry->setCaseExecutionId(task.getCaseExecutionId());*/ |
||
| 128 | $this->entry->setTaskId($task->getId()); |
||
| 129 | |||
| 130 | $execution = $task->getExecution(); |
||
| 131 | if ($execution !== null) { |
||
| 132 | $this->entry->setRootProcessInstanceId($execution->getRootProcessInstanceId()); |
||
| 133 | } |
||
| 134 | |||
| 135 | return $this; |
||
| 136 | } elseif ($data instanceof HistoricTaskInstanceInterface) { |
||
| 137 | $task = $data; |
||
| 138 | $propertyChanges = $arg2; |
||
| 139 | if (empty($propertyChanges)) { |
||
| 140 | if (UserOperationLogEntryInterface::OPERATION_TYPE_CREATE == $this->entry->getOperationType()) { |
||
| 141 | $propertyChanges = PropertyChange::emptyChange(); |
||
| 142 | } |
||
| 143 | } |
||
| 144 | $this->entry->setPropertyChanges($propertyChanges); |
||
| 145 | $this->entry->setProcessDefinitionKey($task->getProcessDefinitionKey()); |
||
| 146 | $this->entry->setProcessDefinitionId($task->getProcessDefinitionId()); |
||
| 147 | $this->entry->setProcessInstanceId($task->getProcessInstanceId()); |
||
| 148 | $this->entry->setExecutionId($task->getExecutionId()); |
||
| 149 | /*$this->entry->setCaseDefinitionId(task.getCaseDefinitionId()); |
||
| 150 | $this->entry->setCaseInstanceId(task.getCaseInstanceId()); |
||
| 151 | $this->entry->setCaseExecutionId(task.getCaseExecutionId());*/ |
||
| 152 | $this->entry->setTaskId($task->getId()); |
||
| 153 | $this->entry->setRootProcessInstanceId($task->getRootProcessInstanceId()); |
||
| 154 | |||
| 155 | return $this; |
||
| 156 | } elseif ($data instanceof HistoryEvent) { |
||
| 157 | $historyEvent = $data; |
||
| 158 | $definition = $arg2; |
||
| 159 | $propertyChanges = $arg3; |
||
| 160 | if (empty($propertyChanges)) { |
||
| 161 | if (UserOperationLogEntryInterface::OPERATION_TYPE_CREATE == $this->entry->getOperationType()) { |
||
| 162 | $propertyChanges = PropertyChange::emptyChange(); |
||
| 163 | } |
||
| 164 | } |
||
| 165 | $this->entry->setPropertyChanges($propertyChanges); |
||
| 166 | $this->entry->setRootProcessInstanceId($historyEvent->getRootProcessInstanceId()); |
||
| 167 | $this->entry->setProcessDefinitionId($historyEvent->getProcessDefinitionId()); |
||
| 168 | $this->entry->setProcessInstanceId($historyEvent->getProcessInstanceId()); |
||
| 169 | $this->entry->setExecutionId($historyEvent->getExecutionId()); |
||
| 170 | /*$this->entry->setCaseDefinitionId(historyEvent.getCaseDefinitionId()); |
||
| 171 | $this->entry->setCaseInstanceId(historyEvent.getCaseInstanceId()); |
||
| 172 | $this->entry->setCaseExecutionId(historyEvent.getCaseExecutionId());*/ |
||
| 173 | |||
| 174 | if ($definition !== null) { |
||
| 175 | if ($definition instanceof ProcessDefinitionEntity) { |
||
| 176 | $this->entry->setProcessDefinitionKey($definition->getKey()); |
||
| 177 | } |
||
| 178 | $this->entry->setDeploymentId($definition->getDeploymentId()); |
||
| 179 | } |
||
| 180 | |||
| 181 | return $this; |
||
| 182 | } elseif ($data instanceof HistoricVariableInstanceEntity) { |
||
| 183 | $variable = $data; |
||
| 184 | $definition = $arg2; |
||
| 185 | $propertyChanges = $arg3; |
||
| 186 | if (empty($propertyChanges)) { |
||
| 187 | if (UserOperationLogEntryInterface::OPERATION_TYPE_CREATE == $this->entry->getOperationType()) { |
||
| 188 | $propertyChanges = PropertyChange::emptyChange(); |
||
| 189 | } |
||
| 190 | } |
||
| 191 | $this->entry->setPropertyChanges($propertyChanges); |
||
| 192 | $this->entry->setRootProcessInstanceId($variable->getRootProcessInstanceId()); |
||
| 193 | $this->entry->setProcessDefinitionId($variable->getProcessDefinitionId()); |
||
| 194 | $this->entry->setProcessInstanceId($variable->getProcessInstanceId()); |
||
| 195 | $this->entry->setExecutionId($variable->getExecutionId()); |
||
| 196 | /*$this->entry->setCaseDefinitionId(variable.getCaseDefinitionId()); |
||
| 197 | $this->entry->setCaseInstanceId(variable.getCaseInstanceId()); |
||
| 198 | $this->entry->setCaseExecutionId(variable.getCaseExecutionId());*/ |
||
| 199 | $this->entry->setTaskId($variable->getTaskId()); |
||
| 200 | |||
| 201 | if ($definition !== null) { |
||
| 202 | if ($definition instanceof ProcessDefinitionEntity) { |
||
| 203 | $this->entry->setProcessDefinitionKey($definition->getKey()); |
||
| 204 | } |
||
| 205 | $this->entry->setDeploymentId($definition->getDeploymentId()); |
||
| 206 | } |
||
| 207 | |||
| 208 | return $this; |
||
| 209 | } elseif ($data instanceof ExternalTaskEntity) { |
||
| 210 | $task = $data; |
||
| 211 | $execution = $arg2; |
||
| 212 | $definition = $arg3; |
||
| 213 | if ($execution !== null) { |
||
| 214 | $this->inContextOf($execution); |
||
| 215 | } elseif ($definition !== null) { |
||
| 216 | $this->inContextOf($definition); |
||
| 217 | } |
||
| 218 | $this->entry->setExternalTaskId($task->getId()); |
||
| 219 | return $this; |
||
| 220 | } |
||
| 221 | } |
||
| 222 | |||
| 223 | public function propertyChanges($propertyChanges): UserOperationLogContextEntryBuilder |
||
| 224 | { |
||
| 225 | if (is_array($propertyChanges)) { |
||
| 226 | $this->entry->setPropertyChanges($propertyChanges); |
||
| 227 | return $this; |
||
| 228 | } else { |
||
| 229 | $propertyChanges = [ $propertyChanges ]; |
||
| 230 | $this->entry->setPropertyChanges($propertyChanges); |
||
| 231 | return $this; |
||
| 232 | } |
||
| 233 | } |
||
| 234 | |||
| 235 | public function create(): UserOperationLogContextEntry |
||
| 236 | { |
||
| 237 | return $this->entry; |
||
| 238 | } |
||
| 239 | |||
| 240 | public function jobId(string $jobId): UserOperationLogContextEntryBuilder |
||
| 241 | { |
||
| 242 | $this->entry->setJobId($jobId); |
||
| 243 | return $this; |
||
| 244 | } |
||
| 245 | |||
| 246 | public function jobDefinitionId(string $jobDefinitionId): UserOperationLogContextEntryBuilder |
||
| 247 | { |
||
| 248 | $this->entry->setJobDefinitionId($jobDefinitionId); |
||
| 249 | return $this; |
||
| 250 | } |
||
| 251 | |||
| 252 | public function processDefinitionId(string $processDefinitionId): UserOperationLogContextEntryBuilder |
||
| 253 | { |
||
| 254 | $this->entry->setProcessDefinitionId($processDefinitionId); |
||
| 255 | return $this; |
||
| 256 | } |
||
| 257 | |||
| 258 | public function processDefinitionKey(string $processDefinitionKey): UserOperationLogContextEntryBuilder |
||
| 259 | { |
||
| 260 | $this->entry->setProcessDefinitionKey($processDefinitionKey); |
||
| 261 | return $this; |
||
| 262 | } |
||
| 263 | |||
| 264 | public function processInstanceId(string $processInstanceId): UserOperationLogContextEntryBuilder |
||
| 265 | { |
||
| 266 | $this->entry->setProcessInstanceId($processInstanceId); |
||
| 267 | return $this; |
||
| 268 | } |
||
| 269 | |||
| 270 | /*public UserOperationLogContextEntryBuilder caseDefinitionId(string $caseDefinitionId) { |
||
| 271 | $this->entry->setCaseDefinitionId(caseDefinitionId); |
||
| 272 | return $this; |
||
| 273 | }*/ |
||
| 274 | |||
| 275 | public function deploymentId(string $deploymentId): UserOperationLogContextEntryBuilder |
||
| 276 | { |
||
| 277 | $this->entry->setDeploymentId($deploymentId); |
||
| 278 | return $this; |
||
| 279 | } |
||
| 280 | |||
| 281 | public function batchId(string $batchId): UserOperationLogContextEntryBuilder |
||
| 282 | { |
||
| 283 | $this->entry->setBatchId($batchId); |
||
| 284 | return $this; |
||
| 285 | } |
||
| 286 | |||
| 287 | public function taskId(string $taskId): UserOperationLogContextEntryBuilder |
||
| 288 | { |
||
| 289 | $this->entry->setTaskId($taskId); |
||
| 290 | return $this; |
||
| 291 | } |
||
| 292 | |||
| 293 | /*public UserOperationLogContextEntryBuilder caseInstanceId(string $caseInstanceId) { |
||
| 294 | $this->entry->setCaseInstanceId(caseInstanceId); |
||
| 295 | return $this; |
||
| 296 | }*/ |
||
| 297 | |||
| 298 | public function category(string $category): UserOperationLogContextEntryBuilder |
||
| 302 | } |
||
| 303 | |||
| 304 | public function annotation(string $annotation): UserOperationLogContextEntryBuilder |
||
| 305 | { |
||
| 306 | $this->entry->setAnnotation($annotation); |
||
| 307 | return $this; |
||
| 308 | } |
||
| 309 | } |
||
| 310 |