| Total Complexity | 76 |
| Total Lines | 463 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like BatchEntity 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 BatchEntity, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class BatchEntity implements BatchInterface, DbEntityInterface, HasDbReferencesInterface, NameableInterface, HasDbRevisionInterface |
||
| 29 | { |
||
| 30 | public static $BATCH_SEED_JOB_DECLARATION; |
||
| 31 | public static $BATCH_MONITOR_JOB_DECLARATION; |
||
| 32 | |||
| 33 | // persistent |
||
| 34 | protected $id; |
||
| 35 | protected $type; |
||
| 36 | |||
| 37 | protected $totalJobs; |
||
| 38 | protected $jobsCreated; |
||
| 39 | protected $batchJobsPerSeed; |
||
| 40 | protected $invocationsPerBatchJob; |
||
| 41 | |||
| 42 | protected $seedJobDefinitionId; |
||
| 43 | protected $monitorJobDefinitionId; |
||
| 44 | protected $batchJobDefinitionId; |
||
| 45 | |||
| 46 | protected $configuration; |
||
| 47 | |||
| 48 | protected $tenantId; |
||
| 49 | protected $createUserId; |
||
| 50 | |||
| 51 | protected $suspensionState; |
||
| 52 | |||
| 53 | protected $revision; |
||
| 54 | |||
| 55 | // transient |
||
| 56 | protected $seedJobDefinition; |
||
| 57 | protected $monitorJobDefinition; |
||
| 58 | protected $batchJobDefinition; |
||
| 59 | |||
| 60 | protected $batchJobHandler; |
||
| 61 | |||
| 62 | public function __construct() |
||
| 63 | { |
||
| 64 | if (self::$BATCH_SEED_JOB_DECLARATION == null) { |
||
| 65 | self::$BATCH_SEED_JOB_DECLARATION = new BatchSeedJobDeclaration(); |
||
| 66 | } |
||
| 67 | if (self::$BATCH_MONITOR_JOB_DECLARATION == null) { |
||
| 68 | self::$BATCH_MONITOR_JOB_DECLARATION = new BatchMonitorJobDeclaration(); |
||
| 69 | } |
||
| 70 | $this->configuration = new ByteArrayField($this, ResourceTypes::runtime()); |
||
| 71 | $this->suspensionState = SuspensionState::active()->getStateCode(); |
||
| 72 | } |
||
| 73 | |||
| 74 | public function getId(): string |
||
| 75 | { |
||
| 76 | return $this->id; |
||
| 77 | } |
||
| 78 | |||
| 79 | public function setId(string $id): void |
||
| 80 | { |
||
| 81 | $this->id = $id; |
||
| 82 | } |
||
| 83 | |||
| 84 | public function getName(): string |
||
| 85 | { |
||
| 86 | return $this->id; |
||
| 87 | } |
||
| 88 | |||
| 89 | public function getType(): string |
||
| 90 | { |
||
| 91 | return $this->type; |
||
| 92 | } |
||
| 93 | |||
| 94 | public function setType(string $type): void |
||
| 95 | { |
||
| 96 | $this->type = $type; |
||
| 97 | } |
||
| 98 | |||
| 99 | public function getTotalJobs(): int |
||
| 100 | { |
||
| 101 | return $this->totalJobs; |
||
| 102 | } |
||
| 103 | |||
| 104 | public function setTotalJobs(int $totalJobs): void |
||
| 105 | { |
||
| 106 | $this->totalJobs = $totalJobs; |
||
| 107 | } |
||
| 108 | |||
| 109 | public function getJobsCreated(): int |
||
| 110 | { |
||
| 111 | return $this->jobsCreated; |
||
| 112 | } |
||
| 113 | |||
| 114 | public function setJobsCreated(int $jobsCreated): void |
||
| 115 | { |
||
| 116 | $this->jobsCreated = $jobsCreated; |
||
| 117 | } |
||
| 118 | |||
| 119 | public function getBatchJobsPerSeed(): int |
||
| 120 | { |
||
| 121 | return $this->batchJobsPerSeed; |
||
| 122 | } |
||
| 123 | |||
| 124 | public function setBatchJobsPerSeed(int $batchJobsPerSeed): void |
||
| 125 | { |
||
| 126 | $this->batchJobsPerSeed = $batchJobsPerSeed; |
||
| 127 | } |
||
| 128 | |||
| 129 | public function getInvocationsPerBatchJob(): int |
||
| 130 | { |
||
| 131 | return $this->invocationsPerBatchJob; |
||
| 132 | } |
||
| 133 | |||
| 134 | public function setInvocationsPerBatchJob(int $invocationsPerBatchJob): void |
||
| 135 | { |
||
| 136 | $this->invocationsPerBatchJob = $invocationsPerBatchJob; |
||
| 137 | } |
||
| 138 | |||
| 139 | public function getSeedJobDefinitionId(): string |
||
| 140 | { |
||
| 141 | return $this->seedJobDefinitionId; |
||
| 142 | } |
||
| 143 | |||
| 144 | public function setSeedJobDefinitionId(string $seedJobDefinitionId): void |
||
| 145 | { |
||
| 146 | $this->seedJobDefinitionId = $seedJobDefinitionId; |
||
| 147 | } |
||
| 148 | |||
| 149 | public function getMonitorJobDefinitionId(): string |
||
| 150 | { |
||
| 151 | return $this->monitorJobDefinitionId; |
||
| 152 | } |
||
| 153 | |||
| 154 | public function setMonitorJobDefinitionId(string $monitorJobDefinitionId): void |
||
| 155 | { |
||
| 156 | $this->monitorJobDefinitionId = $monitorJobDefinitionId; |
||
| 157 | } |
||
| 158 | |||
| 159 | public function getBatchJobDefinitionId(): string |
||
| 160 | { |
||
| 161 | return $this->batchJobDefinitionId; |
||
| 162 | } |
||
| 163 | |||
| 164 | public function setBatchJobDefinitionId(string $batchJobDefinitionId): void |
||
| 165 | { |
||
| 166 | $this->batchJobDefinitionId = $batchJobDefinitionId; |
||
| 167 | } |
||
| 168 | |||
| 169 | public function getTenantId(): ?string |
||
| 170 | { |
||
| 171 | return $this->tenantId; |
||
| 172 | } |
||
| 173 | |||
| 174 | public function setTenantId(?string $tenantId): void |
||
| 175 | { |
||
| 176 | $this->tenantId = $tenantId; |
||
| 177 | } |
||
| 178 | |||
| 179 | public function getCreateUserId(): string |
||
| 180 | { |
||
| 181 | return $this->createUserId; |
||
| 182 | } |
||
| 183 | |||
| 184 | public function setCreateUserId(string $createUserId): void |
||
| 185 | { |
||
| 186 | $this->createUserId = $createUserId; |
||
| 187 | } |
||
| 188 | |||
| 189 | public function getConfiguration(): string |
||
| 190 | { |
||
| 191 | return $this->configuration->getByteArrayId(); |
||
| 192 | } |
||
| 193 | |||
| 194 | public function setConfiguration(string $configuration): void |
||
| 195 | { |
||
| 196 | $this->configuration->setByteArrayId($configuration); |
||
| 197 | } |
||
| 198 | |||
| 199 | public function setSuspensionState(int $state): void |
||
| 200 | { |
||
| 201 | $this->suspensionState = $state; |
||
| 202 | } |
||
| 203 | |||
| 204 | public function getSuspensionState(): int |
||
| 205 | { |
||
| 206 | return $this->suspensionState; |
||
| 207 | } |
||
| 208 | |||
| 209 | public function isSuspended(): bool |
||
| 210 | { |
||
| 211 | return $this->suspensionState == SuspensionState::suspended()->getStateCode(); |
||
| 212 | } |
||
| 213 | |||
| 214 | public function setRevision(int $revision): void |
||
| 215 | { |
||
| 216 | $this->revision = $revision; |
||
| 217 | } |
||
| 218 | |||
| 219 | public function getRevision(): int |
||
| 220 | { |
||
| 221 | return $this->revision; |
||
| 222 | } |
||
| 223 | |||
| 224 | public function getRevisionNext(): int |
||
| 227 | } |
||
| 228 | |||
| 229 | // transient |
||
| 230 | |||
| 231 | public function getSeedJobDefinition(): JobDefinitionEntity |
||
| 232 | { |
||
| 233 | if ($this->seedJobDefinition == null && $this->seedJobDefinitionId !== null) { |
||
| 234 | $this->seedJobDefinition = Context::getCommandContext()->getJobDefinitionManager()->findById($this->seedJobDefinitionId); |
||
| 235 | } |
||
| 236 | return $this->seedJobDefinition; |
||
| 237 | } |
||
| 238 | |||
| 239 | public function getMonitorJobDefinition(): JobDefinitionEntity |
||
| 240 | { |
||
| 241 | if ($this->monitorJobDefinition == null && $this->monitorJobDefinitionId !== null) { |
||
| 242 | $this->monitorJobDefinition = Context::getCommandContext()->getJobDefinitionManager()->findById($this->monitorJobDefinitionId); |
||
| 243 | } |
||
| 244 | return $this->monitorJobDefinition; |
||
| 245 | } |
||
| 246 | |||
| 247 | public function getBatchJobDefinition(): JobDefinitionEntity |
||
| 248 | { |
||
| 249 | if ($this->batchJobDefinition == null && $this->batchJobDefinitionId !== null) { |
||
| 250 | $this->batchJobDefinition = Context::getCommandContext()->getJobDefinitionManager()->findById($this->batchJobDefinitionId); |
||
| 251 | } |
||
| 252 | |||
| 253 | return $this->batchJobDefinition; |
||
| 254 | } |
||
| 255 | |||
| 256 | public function getConfigurationBytes(): string |
||
| 257 | { |
||
| 258 | return $this->configuration->getByteArrayValue(); |
||
|
|
|||
| 259 | } |
||
| 260 | |||
| 261 | public function setConfigurationBytes(string $configuration): void |
||
| 262 | { |
||
| 263 | $this->configuration->setByteArrayValue($configuration); |
||
| 264 | } |
||
| 265 | |||
| 266 | public function getBatchJobHandler(): BatchJobHandlerInterface |
||
| 267 | { |
||
| 268 | if ($this->batchJobHandler == null) { |
||
| 269 | $this->batchJobHandler = Context::getCommandContext()->getProcessEngineConfiguration()->getBatchHandlers()->get($this->type); |
||
| 270 | } |
||
| 271 | return $this->batchJobHandler; |
||
| 272 | } |
||
| 273 | |||
| 274 | public function getPersistentState() |
||
| 275 | { |
||
| 276 | $persistentState = []; |
||
| 277 | $persistentState["jobsCreated"] = $this->jobsCreated; |
||
| 278 | return $persistentState; |
||
| 279 | } |
||
| 280 | |||
| 281 | public function createSeedJobDefinition(string $deploymentId): JobDefinitionEntity |
||
| 282 | { |
||
| 283 | $this->seedJobDefinition = new JobDefinitionEntity(self::$BATCH_SEED_JOB_DECLARATION); |
||
| 284 | $this->seedJobDefinition->setJobConfiguration($this->id); |
||
| 285 | $this->seedJobDefinition->setTenantId($this->tenantId); |
||
| 286 | $this->seedJobDefinition->setDeploymentId($this->deploymentId); |
||
| 287 | |||
| 288 | Context::getCommandContext()->getJobDefinitionManager()->insert($this->seedJobDefinition); |
||
| 289 | |||
| 290 | $this->seedJobDefinitionId = $this->seedJobDefinition->getId(); |
||
| 291 | |||
| 292 | return $this->seedJobDefinition; |
||
| 293 | } |
||
| 294 | |||
| 295 | public function createMonitorJobDefinition(): JobDefinitionEntity |
||
| 296 | { |
||
| 297 | $this->monitorJobDefinition = new JobDefinitionEntity(self::$BATCH_MONITOR_JOB_DECLARATION); |
||
| 298 | $this->monitorJobDefinition->setJobConfiguration($this->id); |
||
| 299 | $this->monitorJobDefinition->setTenantId($this->tenantId); |
||
| 300 | |||
| 301 | Context::getCommandContext()->getJobDefinitionManager()->insert($this->monitorJobDefinition); |
||
| 302 | |||
| 303 | $this->monitorJobDefinitionId = $this->monitorJobDefinition->getId(); |
||
| 304 | |||
| 305 | return $this->monitorJobDefinition; |
||
| 306 | } |
||
| 307 | |||
| 308 | public function createBatchJobDefinition(): JobDefinitionEntity |
||
| 319 | } |
||
| 320 | |||
| 321 | public function createSeedJob(): JobEntity |
||
| 322 | { |
||
| 323 | $seedJob = self::$BATCH_SEED_JOB_DECLARATION->createJobInstance($this); |
||
| 324 | |||
| 325 | Context::getCommandContext()->getJobManager()->insertAndHintJobExecutor($seedJob); |
||
| 326 | |||
| 327 | return $seedJob; |
||
| 328 | } |
||
| 329 | |||
| 330 | public function deleteSeedJob(): void |
||
| 331 | { |
||
| 332 | $seedJobs = Context::getCommandContext() |
||
| 333 | ->getJobManager() |
||
| 334 | ->findJobsByJobDefinitionId($this->seedJobDefinitionId); |
||
| 335 | |||
| 336 | foreach ($seedJobs as $job) { |
||
| 337 | $job->delete(); |
||
| 338 | } |
||
| 339 | } |
||
| 340 | |||
| 341 | public function createMonitorJob(bool $setDueDate): JobEntity |
||
| 342 | { |
||
| 343 | // Maybe use an other job declaration |
||
| 344 | $monitorJob = self::$BATCH_MONITOR_JOB_DECLARATION->createJobInstance($this); |
||
| 345 | if ($setDueDate) { |
||
| 346 | $monitorJob->setDuedate($this->calculateMonitorJobDueDate()); |
||
| 347 | } |
||
| 348 | |||
| 349 | Context::getCommandContext() |
||
| 350 | ->getJobManager()->insertAndHintJobExecutor($monitorJob); |
||
| 351 | |||
| 352 | return $monitorJob; |
||
| 353 | } |
||
| 354 | |||
| 355 | protected function calculateMonitorJobDueDate(): string |
||
| 356 | { |
||
| 357 | $pollTime = Context::getCommandContext() |
||
| 358 | ->getProcessEngineConfiguration() |
||
| 359 | ->getBatchPollTime(); |
||
| 360 | $dueTime = ClockUtil::getCurrentTime()->getTimestamp() + $pollTime; |
||
| 361 | return (new \DateTime())->setTimestamp($dueTime)->format('c'); |
||
| 362 | } |
||
| 363 | |||
| 364 | public function deleteMonitorJob(): void |
||
| 365 | { |
||
| 366 | $monitorJobs = Context::getCommandContext() |
||
| 367 | ->getJobManager() |
||
| 368 | ->findJobsByJobDefinitionId($this->monitorJobDefinitionId); |
||
| 369 | |||
| 370 | foreach ($monitorJobs as $monitorJob) { |
||
| 371 | $monitorJob->delete(); |
||
| 372 | } |
||
| 373 | } |
||
| 374 | |||
| 375 | public function delete(bool $cascadeToHistory, bool $deleteJobs): void |
||
| 376 | { |
||
| 377 | $commandContext = Context::getCommandContext(); |
||
| 378 | |||
| 379 | if ( |
||
| 380 | BatchInterface::TYPE_SET_VARIABLES == $this->type || |
||
| 381 | BatchInterface::TYPE_PROCESS_INSTANCE_MIGRATION == $this->type || |
||
| 382 | BatchInterface::TYPE_CORRELATE_MESSAGE == $this->type |
||
| 383 | ) { |
||
| 384 | $this->deleteVariables($commandContext); |
||
| 385 | } |
||
| 386 | |||
| 387 | $this->deleteSeedJob(); |
||
| 388 | $this->deleteMonitorJob(); |
||
| 389 | if ($deleteJobs) { |
||
| 390 | $this->getBatchJobHandler()->deleteJobs($this); |
||
| 391 | } |
||
| 392 | |||
| 393 | $jobDefinitionManager = $commandContext->getJobDefinitionManager(); |
||
| 394 | $jobDefinitionManager->delete($this->getSeedJobDefinition()); |
||
| 395 | $jobDefinitionManager->delete($this->getMonitorJobDefinition()); |
||
| 396 | $jobDefinitionManager->delete($this->getBatchJobDefinition()); |
||
| 397 | |||
| 398 | $commandContext->getBatchManager()->delete($this); |
||
| 399 | $this->configuration->deleteByteArrayValue(); |
||
| 400 | |||
| 401 | $this->fireHistoricEndEvent(); |
||
| 402 | |||
| 403 | if ($cascadeToHistory) { |
||
| 404 | $historicIncidentManager = $commandContext->getHistoricIncidentManager(); |
||
| 405 | $historicIncidentManager->deleteHistoricIncidentsByJobDefinitionId($this->seedJobDefinitionId); |
||
| 406 | $historicIncidentManager->deleteHistoricIncidentsByJobDefinitionId($this->monitorJobDefinitionId); |
||
| 407 | $historicIncidentManager->deleteHistoricIncidentsByJobDefinitionId($this->batchJobDefinitionId); |
||
| 408 | |||
| 409 | $historicJobLogManager = $commandContext->getHistoricJobLogManager(); |
||
| 410 | $historicJobLogManager->deleteHistoricJobLogsByJobDefinitionId($this->seedJobDefinitionId); |
||
| 411 | $historicJobLogManager->deleteHistoricJobLogsByJobDefinitionId($this->monitorJobDefinitionId); |
||
| 412 | $historicJobLogManager->deleteHistoricJobLogsByJobDefinitionId($this->batchJobDefinitionId); |
||
| 413 | |||
| 414 | $commandContext->getHistoricBatchManager()->deleteHistoricBatchById($this->id); |
||
| 415 | } |
||
| 416 | } |
||
| 417 | |||
| 418 | protected function deleteVariables(CommandContext $commandContext): void |
||
| 419 | { |
||
| 420 | $variableInstanceManager = $commandContext->getVariableInstanceManager(); |
||
| 421 | |||
| 422 | $variableInstances = |
||
| 423 | $variableInstanceManager->findVariableInstancesByBatchId($this->id); |
||
| 424 | |||
| 425 | foreach ($variableInstances as $variable) { |
||
| 426 | $variable->delete(); |
||
| 427 | } |
||
| 428 | } |
||
| 429 | |||
| 430 | public function fireHistoricStartEvent(): void |
||
| 431 | { |
||
| 432 | Context::getCommandContext() |
||
| 433 | ->getHistoricBatchManager() |
||
| 434 | ->createHistoricBatch($this); |
||
| 435 | } |
||
| 436 | |||
| 437 | public function fireHistoricEndEvent(): void |
||
| 438 | { |
||
| 439 | Context::getCommandContext() |
||
| 440 | ->getHistoricBatchManager() |
||
| 441 | ->completeHistoricBatch($this); |
||
| 442 | } |
||
| 443 | |||
| 444 | public function isCompleted(): bool |
||
| 451 | } |
||
| 452 | |||
| 453 | public function __toString() |
||
| 454 | { |
||
| 455 | return "BatchEntity{" . |
||
| 456 | "batchHandler=" . $this->batchJobHandler . |
||
| 457 | ", id='" . $this->id . '\'' . |
||
| 458 | ", type='" . $this->type . '\'' . |
||
| 459 | ", size=" . $this->totalJobs . |
||
| 468 | } |
||
| 469 | |||
| 470 | public function getReferencedEntityIds(): array |
||
| 471 | { |
||
| 472 | $referencedEntityIds = []; |
||
| 473 | return $referencedEntityIds; |
||
| 474 | } |
||
| 475 | |||
| 476 | public function getReferencedEntitiesIdAndClass(): array |
||
| 477 | { |
||
| 478 | $referenceIdAndClass = []; |
||
| 479 | |||
| 480 | if ($this->seedJobDefinitionId !== null) { |
||
| 481 | $referenceIdAndClass[$this->seedJobDefinitionId] = JobDefinitionEntity::class; |
||
| 482 | } |
||
| 483 | if ($this->batchJobDefinitionId !== null) { |
||
| 484 | $referenceIdAndClass[$this->batchJobDefinitionId] = JobDefinitionEntity::class; |
||
| 485 | } |
||
| 486 | if ($this->monitorJobDefinitionId !== null) { |
||
| 487 | $referenceIdAndClass[$this->monitorJobDefinitionId] = JobDefinitionEntity::class; |
||
| 488 | } |
||
| 489 | |||
| 490 | return $referenceIdAndClass; |
||
| 491 | } |
||
| 492 | } |
||
| 493 |