PDO   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
eloc 24
c 1
b 0
f 0
dl 0
loc 52
ccs 25
cts 25
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A fromDbRows() 0 9 2
A __construct() 0 5 1
B fillDataFromRow() 0 22 10
1
<?php
2
3
namespace kalanis\nested_tree\Sources\PDO;
4
5
use kalanis\nested_tree\Sources\SourceInterface;
6
use kalanis\nested_tree\Support;
7
use PDO as base;
8
9
abstract class PDO implements SourceInterface
10
{
11
    use Support\ColumnsTrait;
0 ignored issues
show
introduced by
The trait kalanis\nested_tree\Support\ColumnsTrait requires some properties which are not provided by kalanis\nested_tree\Sources\PDO\PDO: $parentIdColumnName, $positionColumnName, $levelColumnName, $rightColumnName, $leftColumnName, $idColumnName
Loading history...
12
13 65
    public function __construct(
14
        protected readonly base $pdo,
15
        protected readonly Support\Node $nodeBase,
16
        protected readonly Support\TableSettings $settings,
17
    ) {
18 65
    }
19
20
    /**
21
     * @param array<array<mixed>> $rows
22
     * @return array<int, Support\Node>
23
     */
24 62
    protected function fromDbRows(array $rows) : array
25
    {
26 62
        $result = [];
27 62
        foreach ($rows as &$row) {
28 62
            $data = $this->fillDataFromRow($row);
29 62
            $result[$data->id] = $data;
30
        }
31
32 62
        return $result;
33
    }
34
35
    /**
36
     * @param array<mixed> $row
37
     * @return Support\Node
38
     */
39 62
    protected function fillDataFromRow(array $row) : Support\Node
40
    {
41 62
        $data = clone $this->nodeBase;
42 62
        foreach ($row as $k => $v) {
43 62
            if ($this->settings->idColumnName === $k) {
44 62
                $data->id = max(0, intval($v));
45 62
            } elseif ($this->settings->parentIdColumnName === $k) {
46 62
                $data->parentId = is_null($v) && $this->settings->rootIsNull ? null : max(0, intval($v));
47 62
            } elseif ($this->settings->levelColumnName === $k) {
48 62
                $data->level = max(0, intval($v));
49 62
            } elseif ($this->settings->leftColumnName === $k) {
50 62
                $data->left = max(0, intval($v));
51 62
            } elseif ($this->settings->rightColumnName === $k) {
52 62
                $data->right = max(0, intval($v));
53 62
            } elseif ($this->settings->positionColumnName === $k) {
54 62
                $data->position = max(0, intval($v));
55
            } else {
56 62
                $data->{$k} = strval($v);
57
            }
58
        }
59
60 62
        return $data;
61
    }
62
}
63