Passed
Push — main ( 6fd589...d01f4c )
by Bingo
05:51
created

getTargetProcessApplication()   B

Complexity

Conditions 11
Paths 9

Size

Total Lines 40
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 40
rs 7.3166
cc 11
nc 9
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Jabe\Engine\Impl\Context;
4
5
use Jabe\Engine\Application\ProcessApplicationReferenceInterface;
6
use Jabe\Engine\Application\ProcessApplicationLogger;
0 ignored issues
show
Bug introduced by
The type Jabe\Engine\Application\ProcessApplicationLogger was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Jabe\Engine\Impl\Cfg\ProcessEngineConfigurationImpl;
8
use Jabe\Engine\Impl\Core\Instance\CoreExecution;
9
use Jabe\Engine\Impl\JobExecutor\RunnableInterface;
10
use Jabe\Engine\Impl\Persistence\Entity\{
11
    ExecutionEntity,
12
    ProcessDefinitionEntity,
13
    TaskEntity
14
};
15
use Jabe\Engine\Impl\Repository\ResourceDefinitionEntity;
16
17
class ProcessApplicationContextUtil
18
{
19
    //private final static ProcessApplicationLogger LOG = ProcessApplicationLogger.PROCESS_APPLICATION_LOGGER;
20
21
    public static function getTargetProcessApplication($execution = null): ?ProcessApplicationReferenceInterface
22
    {
23
        if ($execution == null) {
24
            return null;
25
        }
26
27
        if ($execution instanceof ExecutionEntity) {
28
            $processApplicationForDeployment = self::getTargetProcessApplication($execution->getProcessDefinition());
29
            return $processApplicationForDeployment;
30
        } elseif ($execution instanceof TaskEntity) {
31
            if ($execution->getProcessDefinition() != null) {
32
                return self::getTargetProcessApplication($execution->getProcessDefinition());
33
            }
34
            return null;
35
        } elseif ($execution instanceof ResourceDefinitionEntity) {
36
            $reference = self::getTargetProcessApplication($execution->getDeploymentId());
37
38
            if ($reference == null && self::areProcessApplicationsRegistered()) {
39
                $previous = $execution->getPreviousDefinition();
40
41
                // do it in a iterative way instead of recursive to avoid
42
                // a possible StackOverflowException in cases with a lot
43
                // of versions of a definition
44
                while ($previous != null) {
45
                    $reference = self::getTargetProcessApplication($previous->getDeploymentId());
46
                    if ($reference == null) {
47
                        $previous = $previous->getPreviousDefinition();
48
                    } else {
49
                        return $reference;
50
                    }
51
                }
52
            }
53
            return $reference;
54
        } elseif (is_string($execution)) {
55
            $processEngineConfiguration = Context::getProcessEngineConfiguration();
56
            $processApplicationManager = $processEngineConfiguration->getProcessApplicationManager();
0 ignored issues
show
Bug introduced by
The method getProcessApplicationManager() 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

56
            /** @scrutinizer ignore-call */ 
57
            $processApplicationManager = $processEngineConfiguration->getProcessApplicationManager();

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...
57
            $processApplicationForDeployment = $processApplicationManager->getProcessApplicationForDeployment($execution);
58
            return $processApplicationForDeployment;
59
        }
60
        return null;
61
    }
62
63
    public static function areProcessApplicationsRegistered(): bool
64
    {
65
        $processEngineConfiguration = Context::getProcessEngineConfiguration();
66
        $processApplicationManager = $processEngineConfiguration->getProcessApplicationManager();
67
        return $processApplicationManager->hasRegistrations();
68
    }
69
70
    public static function requiresContextSwitch(ProcessApplicationReferenceInterface $processApplicationReference): bool
71
    {
72
        $currentProcessApplication = Context::getCurrentProcessApplication();
73
74
        if ($processApplicationReference == null) {
75
            return false;
76
        }
77
78
        if ($currentProcessApplication == null) {
79
            return true;
80
        }
81
        //@TODO
82
        return false;
83
    }
84
85
    public static function doContextSwitch(RunnableInterface $runnable, ProcessDefinitionEntity $contextDefinition): void
86
    {
87
        $processApplication = $this->getTargetProcessApplication($contextDefinition);
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using $this inside a static method is generally not recommended and can lead to errors in newer PHP versions.
Loading history...
88
        if ($this->requiresContextSwitch($processApplication)) {
0 ignored issues
show
Bug introduced by
It seems like $processApplication can also be of type null; however, parameter $processApplicationReference of Jabe\Engine\Impl\Context...requiresContextSwitch() does only seem to accept Jabe\Engine\Application\...ationReferenceInterface, 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

88
        if ($this->requiresContextSwitch(/** @scrutinizer ignore-type */ $processApplication)) {
Loading history...
89
            Context::executeWithinProcessApplication(function () use ($runnable) {
90
                $runnable->run();
91
                return null;
92
            }, $processApplication);
0 ignored issues
show
Bug introduced by
It seems like $processApplication can also be of type null; however, parameter $processApplicationReference of Jabe\Engine\Impl\Context...hinProcessApplication() does only seem to accept Jabe\Engine\Application\...ationReferenceInterface, 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

92
            }, /** @scrutinizer ignore-type */ $processApplication);
Loading history...
93
        } else {
94
            $runnable->run();
95
        }
96
    }
97
}
98