Test Failed
Push — main ( fc8ba5...9083af )
by Bingo
05:27
created

ProcessSetRemovalTimeJobHandler::addRemovalTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 3
1
<?php
2
3
namespace Jabe\Engine\Impl\Batch\RemovalTime;
4
5
use Jabe\Engine\ProcessEngineConfiguration;
6
use Jabe\Engine\Batch\BatchInterface;
7
use Jabe\Engine\Impl\Batch\{
8
    AbstractBatchJobHandler,
9
    BatchJobConfiguration,
10
    BatchJobContext,
11
    BatchJobDeclaration
12
};
13
use Jabe\Engine\Impl\Interceptor\CommandContext;
14
use Jabe\Engine\Impl\JobExecutor\JobDeclaration;
15
use Jabe\Engine\Impl\Json\JsonObjectConverter;
16
use Jabe\Engine\Impl\Persistence\Entity\{
17
    ByteArrayEntity,
18
    ExecutionEntity,
19
    HistoricProcessInstanceEntity,
20
    MessageEntity
21
};
22
use Jabe\Engine\Repository\ProcessDefinitionInterface;
23
24
class ProcessSetRemovalTimeJobHandler extends AbstractBatchJobHandler
25
{
26
    public static $JOB_DECLARATION;
27
28
    public function execute(BatchJobConfiguration $configuration, ExecutionEntity $execution, CommandContext $commandContext, string $tenantId = null)
29
    {
30
        $byteArrayId = $configuration->getConfigurationByteArrayId();
31
        $configurationByteArray = $this->findByteArrayById($byteArrayId, $commandContext);
32
33
        $batchConfiguration = $this->readConfiguration($configurationByteArray);
34
35
        foreach ($batchConfiguration->getIds() as $instanceId) {
36
            $instance = $this->findProcessInstanceById($instanceId, $commandContext);
37
38
            if ($instance != null) {
39
                if ($batchConfiguration->isHierarchical() && $this->hasHierarchy($instance)) {
0 ignored issues
show
Bug introduced by
The method isHierarchical() does not exist on Jabe\Engine\Impl\Batch\BatchConfiguration. It seems like you code against a sub-type of Jabe\Engine\Impl\Batch\BatchConfiguration such as Jabe\Engine\Impl\Batch\R...lTimeBatchConfiguration. ( Ignorable by Annotation )

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

39
                if ($batchConfiguration->/** @scrutinizer ignore-call */ isHierarchical() && $this->hasHierarchy($instance)) {
Loading history...
40
                    $rootProcessInstanceId = $instance->getRootProcessInstanceId();
41
                    $rootInstance = $this->findProcessInstanceById($rootProcessInstanceId, $commandContext);
42
                    $removalTime = $this->getOrCalculateRemovalTime($batchConfiguration, $rootInstance, $commandContext);
0 ignored issues
show
Bug introduced by
It seems like $rootInstance can also be of type null; however, parameter $instance of Jabe\Engine\Impl\Batch\R...rCalculateRemovalTime() does only seem to accept Jabe\Engine\Impl\Persist...icProcessInstanceEntity, maybe add an additional type check? ( Ignorable by Annotation )

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

42
                    $removalTime = $this->getOrCalculateRemovalTime($batchConfiguration, /** @scrutinizer ignore-type */ $rootInstance, $commandContext);
Loading history...
43
                    $this->addRemovalTimeToHierarchy($rootProcessInstanceId, $removalTime, $commandContext);
0 ignored issues
show
Bug introduced by
It seems like $removalTime can also be of type null; however, parameter $removalTime of Jabe\Engine\Impl\Batch\R...emovalTimeToHierarchy() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

43
                    $this->addRemovalTimeToHierarchy($rootProcessInstanceId, /** @scrutinizer ignore-type */ $removalTime, $commandContext);
Loading history...
44
                } else {
45
                    $removalTime = $this->getOrCalculateRemovalTime($batchConfiguration, $instance, $commandContext);
46
                    if ($removalTime != $instance->getRemovalTime()) {
47
                        $this->addRemovalTime($instanceId, $removalTime, $commandContext);
0 ignored issues
show
Bug introduced by
It seems like $removalTime can also be of type null; however, parameter $removalTime of Jabe\Engine\Impl\Batch\R...ndler::addRemovalTime() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

47
                        $this->addRemovalTime($instanceId, /** @scrutinizer ignore-type */ $removalTime, $commandContext);
Loading history...
48
                    }
49
                }
50
            }
51
        }
52
    }
53
54
    protected function getOrCalculateRemovalTime(SetRemovalTimeBatchConfiguration $batchConfiguration, HistoricProcessInstanceEntity $instance, CommandContext $commandContext): ?string
55
    {
56
        if ($batchConfiguration->hasRemovalTime()) {
57
            return $batchConfiguration->getRemovalTime();
58
        } elseif ($this->hasBaseTime($instance, $commandContext)) {
59
            return $this->calculateRemovalTime($instance, $commandContext);
60
        } else {
61
            return null;
62
        }
63
    }
64
65
    protected function addRemovalTimeToHierarchy(string $rootProcessInstanceId, string $removalTime, CommandContext $commandContext): void
66
    {
67
        $commandContext->getHistoricProcessInstanceManager()
68
            ->addRemovalTimeToProcessInstancesByRootProcessInstanceId($rootProcessInstanceId, $removalTime);
69
70
        /*if (isDmnEnabled(commandContext)) {
71
            $commandContext->getHistoricDecisionInstanceManager()
72
            .addRemovalTimeToDecisionsByRootProcessInstanceId(rootProcessInstanceId, removalTime);
73
        }*/
74
    }
75
76
    protected function addRemovalTime(string $instanceId, string $removalTime, CommandContext $commandContext): void
77
    {
78
        $commandContext->getHistoricProcessInstanceManager()
79
            ->addRemovalTimeById($instanceId, $removalTime);
80
81
        /*if (isDmnEnabled(commandContext)) {
82
            $commandContext->getHistoricDecisionInstanceManager()
83
            .addRemovalTimeToDecisionsByProcessInstanceId(instanceId, removalTime);
84
        }*/
85
    }
86
87
    protected function hasBaseTime(HistoricProcessInstanceEntity $instance, CommandContext $commandContext): bool
88
    {
89
        return $this->isStrategyStart($commandContext) || ($this->isStrategyEnd($commandContext) && $this->isEnded($instance));
90
    }
91
92
    protected function isEnded(HistoricProcessInstanceEntity $instance): bool
93
    {
94
        return $instance->getEndTime() != null;
95
    }
96
97
    protected function isStrategyStart(CommandContext $commandContext): bool
98
    {
99
        return ProcessEngineConfiguration::HISTORY_REMOVAL_TIME_STRATEGY_START == $this->getHistoryRemovalTimeStrategy($commandContext);
100
    }
101
102
    protected function isStrategyEnd(CommandContext $commandContext): bool
103
    {
104
        return ProcessEngineConfiguration::HISTORY_REMOVAL_TIME_STRATEGY_END == $this->getHistoryRemovalTimeStrategy($commandContext);
105
    }
106
107
    protected function hasHierarchy(HistoricProcessInstanceEntity $instance): bool
108
    {
109
        return $instance->getRootProcessInstanceId() != null;
110
    }
111
112
    protected function getHistoryRemovalTimeStrategy(CommandContext $commandContext): string
113
    {
114
        return $commandContext->getProcessEngineConfiguration()
115
            ->getHistoryRemovalTimeStrategy();
0 ignored issues
show
Bug introduced by
The method getHistoryRemovalTimeStrategy() does not exist on Jabe\Engine\Impl\Cfg\Pro...EngineConfigurationImpl. Did you maybe mean getHistory()? ( Ignorable by Annotation )

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

115
            ->/** @scrutinizer ignore-call */ getHistoryRemovalTimeStrategy();

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...
116
    }
117
118
    protected function findProcessDefinitionById(string $processDefinitionId, CommandContext $commandContext): ?ProcessDefinitionInterface
119
    {
120
        return $commandContext->getProcessEngineConfiguration()
121
            ->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

121
            ->/** @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...
122
            ->findDeployedProcessDefinitionById($processDefinitionId);
123
    }
124
125
    /*protected boolean isDmnEnabled(CommandContext commandContext) {
126
        return $commandContext->getProcessEngineConfiguration().isDmnEnabled();
127
    }*/
128
129
    protected function calculateRemovalTime(HistoricProcessInstanceEntity $processInstance, CommandContext $commandContext): string
130
    {
131
        $processDefinition = $this->findProcessDefinitionById($processInstance->getProcessDefinitionId(), $commandContext);
132
133
        return $commandContext->getProcessEngineConfiguration()
134
            ->getHistoryRemovalTimeProvider()
0 ignored issues
show
Bug introduced by
The method getHistoryRemovalTimeProvider() does not exist on Jabe\Engine\Impl\Cfg\Pro...EngineConfigurationImpl. Did you maybe mean getHistory()? ( Ignorable by Annotation )

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

134
            ->/** @scrutinizer ignore-call */ getHistoryRemovalTimeProvider()

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...
135
            ->calculateRemovalTime($processInstance, $processDefinition);
136
    }
137
138
    protected function findByteArrayById(string $byteArrayId, CommandContext $commandContext): ByteArrayEntity
139
    {
140
        return $commandContext->getDbEntityManager()
141
            ->selectById(ByteArrayEntity::class, $byteArrayId);
142
    }
143
144
    protected function findProcessInstanceById(string $instanceId, CommandContext $commandContext): ?HistoricProcessInstanceEntity
145
    {
146
        return $commandContext->getHistoricProcessInstanceManager()
147
            ->findHistoricProcessInstance($instanceId);
148
    }
149
150
    public function getJobDeclaration(): JobDeclaration
151
    {
152
        if (self::$JOB_DECLARATION === null) {
153
            self::$JOB_DECLARATION = new BatchJobDeclaration(BatchInterface::TYPE_PROCESS_SET_REMOVAL_TIME);
154
        }
155
        return self::$JOB_DECLARATION;
156
    }
157
158
    protected function createJobConfiguration(BatchConfiguration $configuration, array $processInstanceIds): BatchConfiguration
159
    {
160
        return (new SetRemovalTimeBatchConfiguration($processInstanceIds))
0 ignored issues
show
Bug Best Practice introduced by
The expression return new Jabe\Engine\I...tion->isHierarchical()) returns the type Jabe\Engine\Impl\Batch\R...lTimeBatchConfiguration which is incompatible with the type-hinted return Jabe\Engine\Impl\Batch\R...Time\BatchConfiguration.
Loading history...
161
            ->setRemovalTime($configuration->getRemovalTime())
162
            ->setHasRemovalTime($configuration->hasRemovalTime())
163
            ->setHierarchical($configuration->isHierarchical());
164
    }
165
166
    protected function getJsonConverterInstance(): JsonObjectConverter
167
    {
168
        return SetRemovalTimeJsonConverter::instance();
0 ignored issues
show
Bug Best Practice introduced by
The method Jabe\Engine\Impl\Batch\R...onConverter::instance() is not static, but was called statically. ( Ignorable by Annotation )

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

168
        return SetRemovalTimeJsonConverter::/** @scrutinizer ignore-call */ instance();
Loading history...
169
    }
170
171
    public function getType(): string
172
    {
173
        return BatchInteface::TYPE_PROCESS_SET_REMOVAL_TIME;
174
    }
175
}
176