Test Failed
Push — main ( 5af89f...c76fcf )
by Bingo
05:51
created

DefaultPriorityProvider::evaluateValueProvider()   A

Complexity

Conditions 6
Paths 7

Size

Total Lines 25
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 25
rs 9.0111
cc 6
nc 7
nop 3
1
<?php
2
3
namespace Jabe\Engine\Impl;
4
5
use Jabe\Engine\ProcessEngineException;
6
use Jabe\Engine\Impl\Context\{
7
    Context,
8
    ProcessApplicationContextUtil
9
};
10
use Jabe\Engine\Impl\Core\Variable\Mapping\Value\ParameterValueProviderInterface;
11
use Jabe\Engine\Impl\Persistence\Entity\ExecutionEntity;
12
use Jabe\Engine\Impl\Pvm\Process\ProcessDefinitionImpl;
13
14
abstract class DefaultPriorityProvider implements PriorityProviderInterface
0 ignored issues
show
Bug introduced by
The type Jabe\Engine\Impl\PriorityProviderInterface 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...
15
{
16
    /**
17
     * The default priority.
18
     */
19
    public const DEFAULT_PRIORITY = 0;
20
21
    /**
22
     * The default priority in case of resolution failure.
23
     */
24
    public const DEFAULT_PRIORITY_ON_RESOLUTION_FAILURE = 0;
25
26
    /**
27
     * Returns the default priority.
28
     *
29
     * @return the default priority
0 ignored issues
show
Bug introduced by
The type Jabe\Engine\Impl\the 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...
30
     */
31
    public function getDefaultPriority(): int
32
    {
33
        return self::DEFAULT_PRIORITY;
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::DEFAULT_PRIORITY returns the type integer which is incompatible with the documented return type Jabe\Engine\Impl\the.
Loading history...
34
    }
35
36
    /**
37
     * Returns the default priority in case of resolution failure.
38
     *
39
     * @return the default priority
40
     */
41
    public function getDefaultPriorityOnResolutionFailure(): int
42
    {
43
        return self::DEFAULT_PRIORITY_ON_RESOLUTION_FAILURE;
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::DEFAULT_PRI...Y_ON_RESOLUTION_FAILURE returns the type integer which is incompatible with the documented return type Jabe\Engine\Impl\the.
Loading history...
44
    }
45
46
    /**
47
     * Evaluates a given value provider with the given execution entity to determine
48
     * the correct value. The error message heading is used for the error message
49
     * if the validation fails because the value is no valid priority.
50
     *
51
     * @param valueProvider the provider which contains the value
52
     * @param execution the execution entity
53
     * @param errorMessageHeading the heading which is used for the error message
54
     * @return the valid priority value
55
     */
56
    protected function evaluateValueProvider(ParameterValueProviderInterface $valueProvider, ExecutionEntity $execution, string $errorMessageHeading): int
57
    {
58
        $value = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $value is dead and can be removed.
Loading history...
59
        try {
60
            $value = $valueProvider->getValue($execution);
61
        } catch (ProcessEngineException $e) {
62
            if (
63
                Context::getProcessEngineConfiguration()->isEnableGracefulDegradationOnContextSwitchFailure()
0 ignored issues
show
Bug introduced by
The method isEnableGracefulDegradat...nContextSwitchFailure() 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

63
                Context::getProcessEngineConfiguration()->/** @scrutinizer ignore-call */ isEnableGracefulDegradationOnContextSwitchFailure()

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...
64
                && $this->isSymptomOfContextSwitchFailure($e, $execution)
65
            ) {
66
                $value = $this->getDefaultPriorityOnResolutionFailure();
67
                $this->logNotDeterminingPriority($execution, $value, $e);
0 ignored issues
show
Bug introduced by
$value of type integer is incompatible with the type Jabe\Engine\Impl\the expected by parameter $value of Jabe\Engine\Impl\Default...otDeterminingPriority(). ( Ignorable by Annotation )

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

67
                $this->logNotDeterminingPriority($execution, /** @scrutinizer ignore-type */ $value, $e);
Loading history...
68
            } else {
69
                throw e;
0 ignored issues
show
Bug introduced by
The constant Jabe\Engine\Impl\e was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
70
            }
71
        }
72
73
        if (!is_int($value)) {
74
            throw new ProcessEngineException($errorMessageHeading . ": Priority value is not an Integer");
75
        } else {
76
            $numberValue = $value;
77
            if ($this->isValidLongValue($numberValue)) {
0 ignored issues
show
Bug introduced by
$numberValue of type integer is incompatible with the type Jabe\Engine\Impl\the expected by parameter $value of Jabe\Engine\Impl\Default...der::isValidLongValue(). ( Ignorable by Annotation )

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

77
            if ($this->isValidLongValue(/** @scrutinizer ignore-type */ $numberValue)) {
Loading history...
78
                return $numberValue;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $numberValue returns the type integer which is incompatible with the documented return type Jabe\Engine\Impl\the.
Loading history...
79
            } else {
80
                throw new ProcessEngineException($errorMessageHeading . ": Priority value must be either Short, Integer, or Long");
81
            }
82
        }
83
    }
84
85
    public function determinePriority(ExecutionEntity $execution, $param, string $jobDefinitionId): int
86
    {
87
        if ($param != null || $execution != null) {
88
            $specificPriority = $this->getSpecificPriority($execution, $param, $jobDefinitionId);
89
            if ($specificPriority != null) {
90
                return $specificPriority;
91
            }
92
93
            $processDefinitionPriority = $this->getProcessDefinitionPriority($execution, $param);
94
            if ($processDefinitionPriority != null) {
95
                return $processDefinitionPriority;
96
            }
97
        }
98
        return $this->getDefaultPriority();
99
    }
100
101
    /**
102
     * Returns the priority defined in the specific entity. Like a job definition priority or
103
     * an activity priority. The result can also be null in that case the process
104
     * priority will be used.
105
     *
106
     * @param execution the current execution
107
     * @param param the generic param
108
     * @param jobDefinitionId the job definition id if related to a job
109
     * @return the specific priority
110
     */
111
    abstract protected function getSpecificPriority(ExecutionEntity $execution, $param, string $jobDefinitionId): int;
112
113
    /**
114
     * Returns the priority defined in the process definition. Can also be null
115
     * in that case the fallback is the default priority.
116
     *
117
     * @param execution the current execution
118
     * @param param the generic param
119
     * @return the priority defined in the process definition
120
     */
121
    abstract protected function getProcessDefinitionPriority(ExecutionEntity $execution, $param): int;
122
123
    /**
124
     * Returns the priority which is defined in the given process definition.
125
     * The priority value is identified with the given propertyKey.
126
     * Returns null if the process definition is null or no priority was defined.
127
     *
128
     * @param processDefinition the process definition that should contains the priority
129
     * @param propertyKey the key which identifies the property
130
     * @param execution the current execution
131
     * @param errorMsgHead the error message header which is used if the evaluation fails
132
     * @return the priority defined in the given process
133
     */
134
    protected function getProcessDefinedPriority(ProcessDefinitionImpl $processDefinition, string $propertyKey, ExecutionEntity $execution, string $errorMsgHead): ?int
135
    {
136
        if ($processDefinition != null) {
137
            $priorityProvider = $processDefinition->getProperty($propertyKey);
138
            if ($priorityProvider != null) {
139
                return $this->evaluateValueProvider($priorityProvider, $execution, $errorMsgHead);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->evaluateVa...ecution, $errorMsgHead) returns the type integer which is incompatible with the documented return type Jabe\Engine\Impl\the.
Loading history...
140
            }
141
        }
142
        return null;
143
    }
144
    /**
145
     * Logs the exception which was thrown if the priority can not be determined.
146
     *
147
     * @param execution the current execution entity
148
     * @param value the current value
149
     * @param e the exception which was catched
150
     */
151
    abstract protected function logNotDeterminingPriority(ExecutionEntity $execution, $value, ProcessEngineException $e): void;
152
153
    protected function isSymptomOfContextSwitchFailure(\Throwable $t, ExecutionEntity $contextExecution): bool
154
    {
155
        // a context switch failure can occur, if the current engine has no PA registration for the deployment
156
        // subclasses may assert the actual throwable to narrow down the diagnose
157
        return ProcessApplicationContextUtil::getTargetProcessApplication($contextExecution) == null;
158
    }
159
160
    /**
161
     * Checks if the given number is a valid long value.
162
     * @param value the number which should be checked
163
     * @return true if is a valid long value, false otherwise
164
     */
165
    protected function isValidLongValue($value): bool
166
    {
167
        return is_int($value);
0 ignored issues
show
Bug Best Practice introduced by
The expression return is_int($value) returns the type boolean which is incompatible with the documented return type true.
Loading history...
168
    }
169
}
170