Completed
Push — develop ( c08e7b...416a58 )
by Bartko
05:25
created

AdapterAbstract   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 70
rs 10
c 0
b 0
f 0
ccs 26
cts 26
cp 1
wmc 6
lcom 1
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setOptions() 0 4 1
A getOptions() 0 4 1
A cleanData() 0 18 2
A _buildNodeInfoObject() 0 18 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace StefanoTree\NestedSet\Adapter;
6
7
use StefanoTree\NestedSet\NodeInfo;
8
use StefanoTree\NestedSet\Options;
9
10
abstract class AdapterAbstract implements AdapterInterface
11
{
12
    private $options;
13
14
    /**
15
     * @param Options $options
16
     */
17 205
    protected function setOptions(Options $options): void
18
    {
19 205
        $this->options = $options;
20 205
    }
21
22
    /**
23
     * @return Options
24
     */
25 190
    protected function getOptions(): Options
26
    {
27 190
        return $this->options;
28
    }
29
30
    /**
31
     * Data cannot contain keys like idColumnName, levelColumnName, ...
32
     *
33
     * @param array $data
34
     *
35
     * @return array
36
     */
37 11
    protected function cleanData(array $data): array
38
    {
39 11
        $options = $this->getOptions();
40
41
        $disallowedDataKeys = array(
42 11
            $options->getIdColumnName(),
43 11
            $options->getLeftColumnName(),
44 11
            $options->getRightColumnName(),
45 11
            $options->getLevelColumnName(),
46 11
            $options->getParentIdColumnName(),
47
        );
48
49 11
        if (null !== $options->getScopeColumnName()) {
50 4
            $disallowedDataKeys[] = $options->getScopeColumnName();
51
        }
52
53 11
        return array_diff_key($data, array_flip($disallowedDataKeys));
54
    }
55
56
    /**
57
     * @param array $data
58
     *
59
     * @return NodeInfo
60
     */
61 76
    protected function _buildNodeInfoObject(array $data)
62
    {
63 76
        $options = $this->getOptions();
64
65 76
        $id = $data[$options->getIdColumnName()];
66 76
        $parentId = $data[$options->getParentIdColumnName()];
67 76
        $level = (int) $data[$options->getLevelColumnName()];
68 76
        $left = (int) $data[$options->getLeftColumnName()];
69 76
        $right = (int) $data[$options->getRightColumnName()];
70
71 76
        if (isset($data[$options->getScopeColumnName()])) {
72 23
            $scope = $data[$options->getScopeColumnName()];
73
        } else {
74 53
            $scope = null;
75
        }
76
77 76
        return new NodeInfo($id, $parentId, $level, $left, $right, $scope);
78
    }
79
}
80