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

RestartProcessInstancesBatchCmd::execute()

Size

Total Lines 40
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 40
nc 1
nop 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A RestartProcessInstancesBatchCmd.php$0 ➔ write() 0 4 1
A RestartProcessInstancesBatchCmd.php$0 ➔ __construct() 0 4 1
1
<?php
2
3
namespace Jabe\Engine\Impl\Batch;
4
5
use Jabe\Engine\BadUserRequestException;
6
use Jabe\Engine\BatchPermissions;
7
use Jabe\Engine\Batch\BatchInterface;
8
use Jabe\Engine\Impl\{
9
    ProcessEngineLogger,
10
    RestartProcessInstanceBuilderImpl,
11
    RestartProcessInstancesBatchConfiguration
12
};
13
use Jabe\Engine\Impl\Batch\Builder\{
14
    BatchBuilder,
15
    OperationLogInstanceCountHandlerInterface
16
};
17
use Jabe\Engine\Impl\Cmd\{
18
    AbstractProcessInstanceModificationCommand,
19
    AbstractRestartProcessInstanceCmd,
20
    CommandLogger
21
};
22
use Jabe\Engine\Impl\Interceptor\{
23
    CommandContext,
24
    CommandExecutorInterface
25
};
26
use Jabe\Engine\Impl\Persistence\Entity\ProcessDefinitionEntity;
27
use Jabe\Engine\Impl\Util\EnsureUtil;
28
29
class RestartProcessInstancesBatchCmd extends AbstractRestartProcessInstanceCmd
30
{
31
    //private final CommandLogger LOG = ProcessEngineLogger.CMD_LOGGER;
32
33
    public function __construct(CommandExecutorInterface $commandExecutor, RestartProcessInstanceBuilderImpl $builder)
34
    {
35
        parent::__construct($commandExecutor, $builder);
36
    }
37
38
    public function execute(CommandContext $commandContext)
39
    {
40
        $collectedInstanceIds = $this->collectProcessInstanceIds();
41
42
        $instructions = $this->builder->getInstructions();
43
        EnsureUtil::ensureNotEmpty("Restart instructions cannot be empty", "instructions", $instructions);
44
        EnsureUtil::ensureNotEmpty("Process instance ids cannot be empty", "processInstanceIds", $collectedInstanceIds);
45
        EnsureUtil::ensureNotContainsNull("Process instance ids cannot be null", "processInstanceIds", $collectedInstanceIds);
46
47
        $processDefinitionId = $this->builder->getProcessDefinitionId();
48
        $processDefinition = $this->getProcessDefinition($commandContext, $processDefinitionId);
49
50
        EnsureUtil::ensureNotNull("Process definition cannot be null", "processDefinition", $processDefinition);
51
        EnsureUtil::ensureTenantAuthorized($commandContext, $processDefinition);
52
53
        $tenantId = $processDefinition->getTenantId();
54
55
        $scope = $this;
56
        return (new BatchBuilder($commandContext))
57
            ->type(BatchInterface::TYPE_PROCESS_INSTANCE_RESTART)
58
            ->config(getConfiguration($collectedInstanceIds, $processDefinition->getDeploymentId()))
0 ignored issues
show
Bug introduced by
The function getConfiguration was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

58
            ->config(/** @scrutinizer ignore-call */ getConfiguration($collectedInstanceIds, $processDefinition->getDeploymentId()))
Loading history...
59
            ->permission(BatchPermissions::createBatchRestartProcessInstances())
60
            ->tenantId($tenantId)
0 ignored issues
show
Bug introduced by
It seems like $tenantId can also be of type null; however, parameter $tenantId of Jabe\Engine\Impl\Batch\B...atchBuilder::tenantId() 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

60
            ->tenantId(/** @scrutinizer ignore-type */ $tenantId)
Loading history...
61
            ->operationLogHandler(new class ($scope, $processDefinition) implements OperationLogInstanceCountHandlerInterface {
62
                private $scope;
63
                private $processDefinition;
64
65
                public function __construct($scope, $processDefinition)
66
                {
67
                    $this->scope = $scope;
68
                    $this->processDefinition = $processDefinition;
69
                }
70
71
                public function write(CommandContext $commandContext, int $instanceCount): void
72
                {
73
                    $op = $this->scope->writeUserOperationLog;
74
                    $op($commandContext, $this->processDefinition, $instanceCount, true);
75
                }
76
            })
77
            ->build();
78
    }
79
80
    protected function ensureTenantAuthorized(CommandContext $commandContext, ProcessDefinitionEntity $processDefinition): void
81
    {
82
        if (!$commandContext->getTenantManager()->isAuthenticatedTenant($processDefinition->getTenantId())) {
83
            //throw LOG.exceptionCommandWithUnauthorizedTenant("restart process instances of process definition '" + processDefinition.getId() + "'");
84
            throw new \Exception("restart process instances of process definition '" . $processDefinition->getId() . "'");
85
        }
86
    }
87
88
    public function getConfiguration(array $instanceIds, string $deploymentId): BatchConfiguration
89
    {
90
        return new RestartProcessInstancesBatchConfiguration(
91
            $instanceIds,
92
            DeploymentMappings::of(new DeploymentMapping($deploymentId, count($instanceIds))),
93
            $this->builder->getInstructions(),
94
            $this->builder->getProcessDefinitionId(),
95
            $this->builder->isInitialVariables(),
96
            $this->builder->isSkipCustomListeners(),
97
            $this->builder->isSkipIoMappings(),
98
            $this->builder->isWithoutBusinessKey()
99
        );
100
    }
101
}
102