Test Failed
Push — main ( a2096a...c7561b )
by Bingo
15:21
created

AbstractPersistenceSession::dbSchemaCreate()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 29
rs 8.8333
c 0
b 0
f 0
cc 7
nc 10
nop 0
1
<?php
2
3
namespace Jabe\Engine\Impl\Db;
4
5
use Jabe\Engine\ProcessEngineInterface;
6
use Jabe\Engine\Impl\ProcessEngineLogger;
7
use Jabe\Engine\Impl\Cfg\ProcessEngineConfigurationImpl;
8
use Jabe\Engine\Impl\Context\Context;
9
use Jabe\Engine\Impl\Db\EntityManager\Operation\{
10
    DbBulkOperation,
11
    DbEntityOperation,
12
    DbOperation,
13
    DbOperationType
14
};
15
use Jabe\Engine\Impl\History\HistoryLevel;
16
17
abstract class AbstractPersistenceSession implements PersistenceSessionInterface
18
{
19
    //protected static final EnginePersistenceLogger LOG = ProcessEngineLogger.PERSISTENCE_LOGGER;
20
    protected $listeners = [];
21
22
    public function executeDbOperation(DbOperation $operation): void
23
    {
24
        switch ($operation->getOperationType()) {
25
            case DbOperationType::INSERT:
26
                $this->insertEntity($operation);
27
                break;
28
            case DbOperationType::DELETE:
29
                $this->deleteEntity($operation);
30
                break;
31
            case DbOperationType::DELETE_BULK:
32
                $this->deleteBulk($operation);
33
                break;
34
            case DbOperationType::UPDATE:
35
                $this->updateEntity($operation);
36
                break;
37
            case DbOperationType::UPDATE_BULK:
38
                $this->updateBulk($operation);
39
                break;
40
        }
41
    }
42
43
    abstract protected function insertEntity(DbEntityOperation $operation): void;
44
45
    abstract protected function deleteEntity(DbEntityOperation $operation): void;
46
47
    abstract protected function deleteBulk(DbBulkOperation $operation): void;
48
49
    abstract protected function updateEntity(DbEntityOperation $operation): void;
50
51
    abstract protected function updateBulk(DbBulkOperation $operation): void;
52
53
    abstract protected function getDbVersion(): string;
54
55
    public function dbSchemaCreate(): void
56
    {
57
        $processEngineConfiguration = Context::getProcessEngineConfiguration();
58
59
        $configuredHistoryLevel = $processEngineConfiguration->getHistoryLevel();
0 ignored issues
show
Bug introduced by
The method getHistoryLevel() does not exist on Jabe\Engine\Impl\Cfg\Pro...EngineConfigurationImpl. Did you maybe mean getHistoryLevelCommand()? ( Ignorable by Annotation )

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

59
        /** @scrutinizer ignore-call */ 
60
        $configuredHistoryLevel = $processEngineConfiguration->getHistoryLevel();

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...
60
        if (
61
            (!$processEngineConfiguration->isDbHistoryUsed())
0 ignored issues
show
Bug introduced by
The method isDbHistoryUsed() 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

61
            (!$processEngineConfiguration->/** @scrutinizer ignore-call */ isDbHistoryUsed())

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...
62
            && ($configuredHistoryLevel != HistoryLevel::historyLevelNone())
63
        ) {
64
            //throw LOG.databaseHistoryLevelException(configuredHistoryLevel.getName());
65
            throw new \Exception("databaseHistoryLevelException");
66
        }
67
68
        if ($this->isEngineTablePresent()) {
69
            $dbVersion = $this->getDbVersion();
70
            if (ProcessEngineInterface::VERSION != $dbVersion) {
71
                //throw LOG.wrongDbVersionException(ProcessEngine.VERSION, dbVersion);
72
                throw new \Exception("wrongDbVersionException");
73
            }
74
        } else {
75
            $this->dbSchemaCreateEngine();
76
        }
77
78
        if ($processEngineConfiguration->isDbHistoryUsed()) {
79
            $this->dbSchemaCreateHistory();
80
        }
81
82
        if ($processEngineConfiguration->isDbIdentityUsed()) {
0 ignored issues
show
Bug introduced by
The method isDbIdentityUsed() 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

82
        if ($processEngineConfiguration->/** @scrutinizer ignore-call */ isDbIdentityUsed()) {

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...
83
            $this->dbSchemaCreateIdentity();
84
        }
85
86
        /*if (processEngineConfiguration.isCmmnEnabled()) {
87
            dbSchemaCreateCmmn();
88
        }
89
90
        if (processEngineConfiguration.isCmmnEnabled() && processEngineConfiguration.isDbHistoryUsed()) {
91
            dbSchemaCreateCmmnHistory();
92
        }
93
94
        if (processEngineConfiguration.isDmnEnabled()) {
95
            dbSchemaCreateDmn();
96
            if (processEngineConfiguration.isDbHistoryUsed()) {
97
                dbSchemaCreateDmnHistory();
98
            }
99
        }*/
100
    }
101
102
    abstract protected function dbSchemaCreateIdentity(): void;
103
104
    abstract protected function dbSchemaCreateHistory(): void;
105
106
    abstract protected function dbSchemaCreateEngine(): void;
107
108
    /*abstract protected function dbSchemaCreateCmmn(): void;
109
110
    abstract protected function dbSchemaCreateCmmnHistory(): void;
111
112
    abstract protected function void dbSchemaCreateDmn();
113
114
    abstract protected function void dbSchemaCreateDmnHistory();*/
115
116
    public function dbSchemaDrop(): void
117
    {
118
        $processEngineConfiguration = Context::getProcessEngineConfiguration();
119
120
        /*if ($processEngineConfiguration->isDmnEnabled()) {
121
            dbSchemaDropDmn();
122
            if (processEngineConfiguration.isDbHistoryUsed()) {
123
                dbSchemaDropDmnHistory();
124
            }
125
        }
126
127
        if (processEngineConfiguration.isCmmnEnabled()) {
128
            dbSchemaDropCmmn();
129
        }
130
131
        dbSchemaDropEngine();
132
133
        if (processEngineConfiguration.isCmmnEnabled() && processEngineConfiguration.isDbHistoryUsed()) {
134
            dbSchemaDropCmmnHistory();
135
        }*/
136
137
        if ($processEngineConfiguration->isDbHistoryUsed()) {
138
            $this->dbSchemaDropHistory();
139
        }
140
141
        if ($processEngineConfiguration->isDbIdentityUsed()) {
142
            $this->dbSchemaDropIdentity();
143
        }
144
    }
145
146
    abstract protected function dbSchemaDropIdentity(): void;
147
148
    abstract protected function dbSchemaDropHistory(): void;
149
150
    abstract protected function dbSchemaDropEngine(): void;
151
152
    /*abstract protected function void dbSchemaDropCmmn();
153
154
    abstract protected function void dbSchemaDropCmmnHistory();
155
156
    abstract protected function void dbSchemaDropDmn();
157
158
    abstract protected function void dbSchemaDropDmnHistory();*/
159
160
    public function dbSchemaPrune(): void
161
    {
162
        $processEngineConfiguration = Context::getProcessEngineConfiguration();
163
        if ($this->isHistoryTablePresent() && !$processEngineConfiguration->isDbHistoryUsed()) {
164
            $this->dbSchemaDropHistory();
165
        }
166
        if ($this->isIdentityTablePresent() && !$processEngineConfiguration->isDbIdentityUsed()) {
167
            $this->dbSchemaDropIdentity();
168
        }
169
        /*if (isCmmnTablePresent() && !processEngineConfiguration.isCmmnEnabled()) {
170
            dbSchemaDropCmmn();
171
        }
172
        if (isCmmnHistoryTablePresent() && (!processEngineConfiguration.isCmmnEnabled() || !processEngineConfiguration.isDbHistoryUsed())) {
173
            dbSchemaDropCmmnHistory();
174
        }
175
        if (isDmnTablePresent() && !processEngineConfiguration.isDmnEnabled()) {
176
            dbSchemaDropDmn();
177
        }
178
        if (isDmnHistoryTablePresent() && (!processEngineConfiguration.isDmnEnabled() || !processEngineConfiguration.isDbHistoryUsed())) {
179
            dbSchemaDropDmnHistory();
180
        }*/
181
    }
182
183
    abstract public function isEngineTablePresent(): bool;
184
185
    abstract public function isHistoryTablePresent(): bool;
186
187
    abstract public function isIdentityTablePresent(): bool;
188
189
    /*abstract public function boolean isCmmnTablePresent();
190
191
    abstract public function boolean isCmmnHistoryTablePresent();
192
193
    abstract public function boolean isDmnTablePresent();
194
195
    abstract public function boolean isDmnHistoryTablePresent();*/
196
197
    public function dbSchemaUpdate(): void
198
    {
199
        $processEngineConfiguration = Context::getProcessEngineConfiguration();
200
        if (!$this->isEngineTablePresent()) {
201
            $this->dbSchemaCreateEngine();
202
        }
203
204
        if (!$this->isHistoryTablePresent() && $processEngineConfiguration->isDbHistoryUsed()) {
205
            $this->dbSchemaCreateHistory();
206
        }
207
208
        if (!$this->isIdentityTablePresent() && $processEngineConfiguration->isDbIdentityUsed()) {
209
            $this->dbSchemaCreateIdentity();
210
        }
211
212
        /*if (!isCmmnTablePresent() && processEngineConfiguration.isCmmnEnabled()) {
213
            dbSchemaCreateCmmn();
214
        }
215
216
        if (!isCmmnHistoryTablePresent() && processEngineConfiguration.isCmmnEnabled() && processEngineConfiguration.isDbHistoryUsed()) {
217
            dbSchemaCreateCmmnHistory();
218
        }
219
220
        if (!isDmnTablePresent() && processEngineConfiguration.isDmnEnabled()) {
221
            dbSchemaCreateDmn();
222
        }
223
224
        if (!isDmnHistoryTablePresent() && processEngineConfiguration.isDmnEnabled() && processEngineConfiguration.isDbHistoryUsed()) {
225
            dbSchemaCreateDmnHistory();
226
        }*/
227
    }
228
229
    public function getTableNamesPresent(): array
230
    {
231
        return [];
232
    }
233
234
    public function addEntityLoadListener(EntityLoadListenerInterface $listener): void
235
    {
236
        $this->listeners[] = $listener;
237
    }
238
239
    protected function fireEntityLoaded($result = null): void
240
    {
241
        if ($result != null && $result instanceof DbEntityInterface) {
242
            $entity = $result;
243
            foreach ($this->listeners as $entityLoadListener) {
244
                $entityLoadListener->onEntityLoaded($entity);
245
            }
246
        }
247
    }
248
}
249