Issues (2551)

src/Impl/AbstractVariableQueryImpl.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Jabe\Impl;
4
5
use Jabe\Exception\NotValidException;
6
use Jabe\Impl\Cfg\ProcessEngineConfigurationImpl;
7
use Jabe\Impl\Context\Context;
8
use Jabe\Impl\Interceptor\{
9
    CommandContext,
10
    CommandExecutorInterface
11
};
12
use Jabe\Impl\Util\EnsureUtil;
13
use Jabe\Query\QueryInterface;
14
15
abstract class AbstractVariableQueryImpl extends AbstractQuery
16
{
17
    protected $queryVariableValues = [];
18
19
    protected $variableNamesIgnoreCase;
20
    protected $variableValuesIgnoreCase;
21
22
    public function __construct(CommandExecutorInterface $commandExecutor = null)
23
    {
24
        if ($commandExecutor !== null) {
25
            parent::__construct($commandExecutor);
26
        }
27
    }
28
29
    abstract public function executeCount(CommandContext $commandContext): int;
30
31
    abstract public function executeList(CommandContext $commandContext, Page $page): array;
32
33
    public function variableValueEquals(string $name, $value): QueryInterface
34
    {
35
        $this->addVariable($name, $value, QueryOperator::EQUALS, true);
36
        return $this;
37
    }
38
39
    public function variableValueNotEquals(string $name, $value): QueryInterface
40
    {
41
        $this->addVariable($name, $value, QueryOperator::NOT_EQUALS, true);
42
        return $this;
43
    }
44
45
    public function variableValueGreaterThan(string $name, $value): QueryInterface
46
    {
47
        $this->addVariable($name, $value, QueryOperator::GREATER_THAN, true);
48
        return $this;
49
    }
50
51
    public function variableValueGreaterThanOrEqual(string $name, $value): QueryInterface
52
    {
53
        $this->addVariable($name, $value, QueryOperator::GREATER_THAN_OR_EQUAL, true);
54
        return $this;
55
    }
56
57
    public function variableValueLessThan(string $name, $value): QueryInterface
58
    {
59
        $this->addVariable($name, $value, QueryOperator::LESS_THAN, true);
60
        return $this;
61
    }
62
63
    public function variableValueLessThanOrEqual(string $name, $value): QueryInterface
64
    {
65
        $this->addVariable($name, $value, QueryOperator::LESS_THAN_OR_EQUAL, true);
66
        return $this;
67
    }
68
69
    public function variableValueLike(string $name, string $value): QueryInterface
70
    {
71
        $this->addVariable($name, $value, QueryOperator::LIKE, true);
72
        return $this;
73
    }
74
75
    public function variableValueNotLike(string $name, string $value): QueryInterface
76
    {
77
        $this->addVariable($name, $value, QueryOperator::NOT_LIKE, true);
78
        return $this;
79
    }
80
81
    public function matchVariableNamesIgnoreCase(): QueryInterface
82
    {
83
        $this->variableNamesIgnoreCase = true;
84
        foreach ($this->getQueryVariableValues() as $variable) {
85
            $variable->setVariableNameIgnoreCase(true);
86
        }
87
        return $this;
88
    }
89
90
    public function matchVariableValuesIgnoreCase(): QUeryInterface
91
    {
92
        $this->variableValuesIgnoreCase = true;
93
        foreach ($this->getQueryVariableValues() as $variable) {
94
            $variable->setVariableValueIgnoreCase(true);
95
        }
96
        return $this;
97
    }
98
99
    protected function addVariable(string $name, $value, string $operator, bool $processInstanceScope): void
100
    {
101
        $queryVariableValue = $this->createQueryVariableValue($name, $value, $operator, $processInstanceScope);
102
        $this->queryVariableValues[] = $queryVariableValue;
103
    }
104
105
    protected function createQueryVariableValue(string $name, $value, string $operator, bool $processInstanceScope): QueryVariableValue
106
    {
107
        $this->validateVariable($name, $value, $operator);
108
109
        $shouldMatchVariableValuesIgnoreCase = $this->variableValuesIgnoreCase == true && $value !== null && is_string($value);
110
        $shouldMatchVariableNamesIgnoreCase = $this->variableNamesIgnoreCase == true;
111
112
        return new QueryVariableValue($name, $value, $operator, $processInstanceScope, $shouldMatchVariableNamesIgnoreCase, $shouldMatchVariableValuesIgnoreCase);
113
    }
114
115
    protected function validateVariable(string $name, $value, string $operator): void
116
    {
117
        EnsureUtil::ensureNotNull(NotValidException::class, "name", $name);
118
        if ($value === null || $this->isBoolean($value)) {
119
            // Null-values and booleans can only be used in EQUALS and NOT_EQUALS
120
            switch ($operator) {
121
                case QueryOperator::GREATER_THAN:
122
                    throw new NotValidException("Booleans and null cannot be used in 'greater than' condition");
123
                case QueryOperator::LESS_THAN:
124
                    throw new NotValidException("Booleans and null cannot be used in 'less than' condition");
125
                case QueryOperator::GREATER_THAN_OR_EQUAL:
126
                    throw new NotValidException("Booleans and null cannot be used in 'greater than or equal' condition");
127
                case QueryOperator::LESS_THAN_OR_EQUAL:
128
                    throw new NotValidException("Booleans and null cannot be used in 'less than or equal' condition");
129
                case QueryOperator::LIKE:
130
                    throw new NotValidException("Booleans and null cannot be used in 'like' condition");
131
                case QueryOperator::NOT_LIKE:
132
                    throw new NotValidException("Booleans and null cannot be used in 'not like' condition");
133
                default:
134
                    break;
135
            }
136
        }
137
    }
138
139
    private function isBoolean($value = null): bool
140
    {
141
        if ($value === null) {
142
            return false;
143
        }
144
        return is_bool($value);
145
    }
146
147
    protected function ensureVariablesInitialized(): void
148
    {
149
        if (!empty($this->getQueryVariableValues())) {
150
            $processEngineConfiguration = Context::getProcessEngineConfiguration();
151
            $variableSerializers = $processEngineConfiguration->getVariableSerializers();
0 ignored issues
show
The method getVariableSerializers() does not exist on Jabe\Impl\Cfg\ProcessEngineConfigurationImpl. ( Ignorable by Annotation )

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

151
            /** @scrutinizer ignore-call */ 
152
            $variableSerializers = $processEngineConfiguration->getVariableSerializers();

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...
152
            $dbType = $processEngineConfiguration->getDatabaseType();
153
            foreach ($this->getQueryVariableValues() as $queryVariableValue) {
154
                $queryVariableValue->initialize($variableSerializers, $dbType);
155
            }
156
        }
157
    }
158
159
    public function getQueryVariableValues(): array
160
    {
161
        return $this->queryVariableValues;
162
    }
163
164
    public function isVariableNamesIgnoreCase(): bool
165
    {
166
        return $this->variableNamesIgnoreCase;
167
    }
168
169
    public function isVariableValuesIgnoreCase(): bool
170
    {
171
        return $this->variableValuesIgnoreCase;
172
    }
173
}
174