Passed
Push — master ( 02118d...11fb63 )
by Petr
03:00
created

TWriteDatabase::updateRecordByPk()   C

Complexity

Conditions 14
Paths 41

Size

Total Lines 52
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 14

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 14
eloc 32
c 1
b 0
f 0
nc 41
nop 1
dl 0
loc 52
ccs 31
cts 31
cp 1
crap 14
rs 6.2666

How to fix   Long Method    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 kalanis\kw_mapper\Mappers\Database;
4
5
6
use kalanis\kw_mapper\Interfaces\IQueryBuilder;
7
use kalanis\kw_mapper\MapperException;
8
use kalanis\kw_mapper\Mappers\Shared\TEntityChanged;
9
use kalanis\kw_mapper\Records\ARecord;
10
use kalanis\kw_mapper\Records\TFill;
11
use kalanis\kw_mapper\Storage\Database;
12
13
14
/**
15
 * Trait TWriteDatabase
16
 * @package kalanis\kw_mapper\Mappers\Database
17
 * Separated write DB entry without need to reload mapper
18
 *
19
 * Contains only operations for writing into DB
20
 */
21
trait TWriteDatabase
22
{
23
    use TEntityChanged;
24
    use TFill;
25
26
    /** @var string */
27
    protected $writeSource = '';
28
    /** @var Database\ASQL */
29
    protected $writeDatabase = null;
30
    /** @var Database\Dialects\ADialect */
31
    protected $writeDialect = null;
32
    /** @var Database\QueryBuilder */
33
    protected $writeQueryBuilder = null;
34
35
    /**
36
     * @throws MapperException
37
     */
38 16
    public function initTWriteDatabase(): void
39
    {
40 16
        $this->writeDatabase = $this->getWriteDatabase();
41 16
        $this->writeDialect = $this->getWriteDialect($this->writeDatabase);
42 16
        $this->writeQueryBuilder = $this->getWriteQueryBuilder($this->writeDialect);
43 16
    }
44
45 4
    protected function setWriteSource(string $writeSource): void
46
    {
47 4
        $this->writeSource = $writeSource;
48 4
    }
49
50 4
    protected function getWriteSource(): string
51
    {
52 4
        return $this->writeSource;
53
    }
54
55
    /**
56
     * @throws MapperException
57
     * @return Database\ADatabase
58
     */
59 16
    protected function getWriteDatabase(): Database\ADatabase
60
    {
61 16
        return Database\DatabaseSingleton::getInstance()->getDatabase(
62 16
            Database\ConfigStorage::getInstance()->getConfig($this->getWriteSource())
63
        );
64
    }
65
66
    /**
67
     * @param Database\ADatabase $database
68
     * @throws MapperException
69
     * @return Database\Dialects\ADialect
70
     */
71 16
    protected function getWriteDialect(Database\ADatabase $database): Database\Dialects\ADialect
72
    {
73 16
        return Database\Dialects\Factory::getInstance()->getDialectClass($database->languageDialect());
74
    }
75
76 16
    protected function getWriteQueryBuilder(Database\Dialects\ADialect $dialect): Database\QueryBuilder
77
    {
78 16
        return new Database\QueryBuilder($dialect);
79
    }
80
81
    /**
82
     * @param ARecord $record
83
     * @throws MapperException
84
     * @return bool
85
     */
86 8
    protected function insertRecord(ARecord $record): bool
87
    {
88 8
        $this->writeQueryBuilder->clear();
89 8
        $this->writeQueryBuilder->setBaseTable($record->getMapper()->getAlias());
90 8
        $relations = $record->getMapper()->getRelations();
91
92 8
        foreach ($record as $key => $item) {
93 8
            if (isset($relations[$key]) && $this->ifEntryChanged($record->getEntry($key))) {
94 6
                $this->writeQueryBuilder->addProperty(
95 6
                    $record->getMapper()->getAlias(),
96 6
                    $relations[$key],
97
                    $item
98
                );
99
            }
100
        }
101
102 8
        if (empty($this->writeQueryBuilder->getProperties())) {
103 2
            return false;
104
        }
105
106 6
        return $this->writeDatabase->exec(
107 6
            strval($this->writeDialect->insert($this->writeQueryBuilder)),
108 6
            $this->writeQueryBuilder->getParams()
109
        );
110
    }
111
112
    /**
113
     * @param ARecord $record
114
     * @throws MapperException
115
     * @return bool
116
     */
117 8
    protected function updateRecord(ARecord $record): bool
118
    {
119 8
        if ($this->updateRecordByPk($record)) {
120 3
            return true;
121
        }
122 7
        $this->writeQueryBuilder->clear();
123 7
        $this->writeQueryBuilder->setBaseTable($record->getMapper()->getAlias());
124 7
        $relations = $record->getMapper()->getRelations();
125
126 7
        foreach ($record as $key => $item) {
127 7
            if (isset($relations[$key]) && $this->ifEntryChanged($record->getEntry($key))) {
128 7
                if ($record->getEntry($key)->isFromStorage()) {
129 5
                    $this->writeQueryBuilder->addCondition(
130 5
                        $record->getMapper()->getAlias(),
131 5
                        $relations[$key],
132 5
                        IQueryBuilder::OPERATION_EQ,
133
                        $item
134
                    );
135
                } else {
136 5
                    $this->writeQueryBuilder->addProperty(
137 5
                        $record->getMapper()->getAlias(),
138 5
                        $relations[$key],
139
                        $item
140
                    );
141
                }
142
            }
143
        }
144
145 7
        if (empty($this->writeQueryBuilder->getConditions())) { /// this one is questionable - I really want to update everything?
146 2
            return false;
147
        }
148 5
        if (empty($this->writeQueryBuilder->getProperties())) {
149 2
            return false;
150
        }
151
152 3
        return $this->writeDatabase->exec(
153 3
            strval($this->writeDialect->update($this->writeQueryBuilder)),
154 3
            $this->writeQueryBuilder->getParams()
155
        );
156
    }
157
158
    /**
159
     * @param ARecord $record
160
     * @throws MapperException
161
     * @return bool
162
     */
163 12
    protected function updateRecordByPk(ARecord $record): bool
164
    {
165 12
        if (empty($record->getMapper()->getPrimaryKeys())) {
166 3
            return false;
167
        }
168
169 9
        $this->writeQueryBuilder->clear();
170 9
        $this->writeQueryBuilder->setBaseTable($record->getMapper()->getAlias());
171 9
        $relations = $record->getMapper()->getRelations();
172
173 9
        foreach ($record->getMapper()->getPrimaryKeys() as $key) {
174
            try {
175 9
                if (isset($relations[$key])) {
176 9
                    $entry = $record->getEntry($key);
177 7
                    if ($entry->isFromStorage() && $this->ifEntryChanged($record->getEntry($key))) {
178 5
                        $this->writeQueryBuilder->addCondition(
179 5
                            $record->getMapper()->getAlias(),
180 5
                            $relations[$key],
181 5
                            IQueryBuilder::OPERATION_EQ,
182 7
                            $entry->getData()
183
                        );
184
                    }
185
                }
186 2
            } catch (MapperException $ex) {
187 2
                return false;
188
            }
189
        }
190
191 7
        if (empty($this->writeQueryBuilder->getConditions())) { // no conditions, nothing in PKs - back to normal system
192 4
            return false;
193
        }
194
195 5
        foreach ($record as $key => $item) {
196 5
            if (isset($relations[$key])) {
197 5
                $entry = $record->getEntry($key);
198
                if (
199 5
                    !in_array($key, $record->getMapper()->getPrimaryKeys())
200 5
                    && !$entry->isFromStorage()
201 5
                    && $this->ifEntryChanged($record->getEntry($key))
202
                ) {
203 3
                    $this->writeQueryBuilder->addProperty($record->getMapper()->getAlias(), $relations[$key], $item);
204
                }
205
            }
206
        }
207
208 5
        if (empty($this->writeQueryBuilder->getProperties())) {
209 2
            return false;
210
        }
211
212 3
        return $this->writeDatabase->exec(
213 3
            strval($this->writeDialect->update($this->writeQueryBuilder)),
214 3
            $this->writeQueryBuilder->getParams()
215
        );
216
    }
217
218
    /**
219
     * @param ARecord $record
220
     * @throws MapperException
221
     * @return bool
222
     */
223 8
    protected function deleteRecord(ARecord $record): bool
224
    {
225 8
        if ($this->deleteRecordByPk($record)) {
226 3
            return true;
227
        }
228
229 5
        $this->writeQueryBuilder->clear();
230 5
        $this->writeQueryBuilder->setBaseTable($record->getMapper()->getAlias());
231 5
        $relations = $record->getMapper()->getRelations();
232
233 5
        foreach ($record as $key => $item) {
234 5
            if (isset($relations[$key]) && $this->ifEntryChanged($record->getEntry($key))) {
235 3
                $this->writeQueryBuilder->addCondition(
236 3
                    $record->getMapper()->getAlias(),
237 3
                    $relations[$key],
238 3
                    IQueryBuilder::OPERATION_EQ,
239
                    $item
240
                );
241
            }
242
        }
243
244 5
        if (empty($this->writeQueryBuilder->getConditions())) { /// this one is necessary - delete everything? do it yourself - and manually!
245 2
            return false;
246
        }
247
248 3
        return $this->writeDatabase->exec(
249 3
            strval($this->writeDialect->delete($this->writeQueryBuilder)),
250 3
            $this->writeQueryBuilder->getParams()
251
        );
252
    }
253
254
    /**
255
     * @param ARecord $record
256
     * @throws MapperException
257
     * @return bool
258
     */
259 12
    protected function deleteRecordByPk(ARecord $record): bool
260
    {
261 12
        if (empty($record->getMapper()->getPrimaryKeys())) {
262 4
            return false;
263
        }
264
265 9
        $this->writeQueryBuilder->clear();
266 9
        $this->writeQueryBuilder->setBaseTable($record->getMapper()->getAlias());
267 9
        $relations = $record->getMapper()->getRelations();
268
269 9
        foreach ($record->getMapper()->getPrimaryKeys() as $key) {
270
            try {
271 9
                if (isset($relations[$key]) && $this->ifEntryChanged($record->getEntry($key))) {
272 3
                    $this->writeQueryBuilder->addCondition(
273 3
                        $record->getMapper()->getAlias(),
274 3
                        $relations[$key],
275 3
                        IQueryBuilder::OPERATION_EQ,
276 7
                        $record->offsetGet($key)
277
                    );
278
                }
279 2
            } catch (MapperException $ex) {
280 2
                return false;
281
            }
282
        }
283
284 7
        if (empty($this->writeQueryBuilder->getConditions())) { // no conditions, nothing in PKs - back to normal system
285 4
            return false;
286
        }
287
288 3
        return $this->writeDatabase->exec(
289 3
            strval($this->writeDialect->delete($this->writeQueryBuilder)),
290 3
            $this->writeQueryBuilder->getParams()
291
        );
292
    }
293
}
294