Passed
Branch development (0519a2)
by Theodoros
02:02
created

EcondaCategoryCollector   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 114
rs 10
c 0
b 0
f 0
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A formatCategoryNode() 0 6 1
A getParents() 0 12 2
A collectData() 0 13 2
A collectResourceType() 0 3 1
A collectItem() 0 3 1
A getChildren() 0 15 3
1
<?php
2
3
/**
4
 * MIT License
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerEco\Zed\Econda\Business\Collector\File;
9
10
use Generated\Shared\Transfer\LocaleTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\LocaleTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use SprykerEco\Zed\Econda\Business\Collector\AbstractDatabaseCollector;
12
13
class EcondaCategoryCollector extends AbstractDatabaseCollector
14
{
15
16
    //CSV file columns
17
    const ID_COLUMN = 'ID';
18
    const PARENT_COLUMN = 'ParentID';
19
    const NAME_COLUMN = self::NAME_QUERY_FIELD;
20
21
    //internal query fields
22
    const ID_CATEGORY_NODE_QUERY_FIELD = 'id_category_node';
23
    const PARENTS_QUERY_FIELD = 'parents';
24
    const NAME_QUERY_FIELD = 'name';
25
    const CHILDREN_QUERY_FIELD = 'children';
26
    const FK_PARENT_CATEGORY_NODE = 'fk_parent_category_node';
27
28
    const CATEGORIES = 'categories';
29
30
    /**
31
     * @param array $collectedSet
32
     * @param \Generated\Shared\Transfer\LocaleTransfer $locale
33
     *
34
     * @return array
35
     */
36
    protected function collectData(array $collectedSet, LocaleTransfer $locale)
37
    {
38
        $setToExport = [];
39
40
        foreach ($collectedSet as $collectedItemData) {
41
42
            $collectedItemData[self::CHILDREN_QUERY_FIELD] = $this->getChildren($collectedItemData, $collectedSet);
43
            $collectedItemData[self::PARENTS_QUERY_FIELD] = $this->getParents($collectedItemData, $collectedSet);
44
45
            $setToExport[] = $this->collectItem($collectedItemData);
46
        }
47
48
        return $setToExport;
49
    }
50
51
    /**
52
     * @param array $collectItemData
53
     *
54
     * @return array
55
     */
56
    protected function collectItem(array $collectItemData)
57
    {
58
        return $this->formatCategoryNode($collectItemData);
59
    }
60
61
    /**
62
     * @SuppressWarnings(PHPMD.UnusedLocalVariable)
63
     *
64
     * @param array $node
65
     * @param array $data
66
     * @param bool $nested
67
     *
68
     * @return array
69
     */
70
    protected function getChildren(array $node, array $data, $nested = true)
71
    {
72
        $children = array_filter($data, function ($item) use ($node) {
73
            return ((int)$item[self::FK_PARENT_CATEGORY_NODE] === (int)$node[self::ID_CATEGORY_NODE_QUERY_FIELD]);
74
        });
75
76
        foreach ($children as $index => $child) {
77
            if ($nested) {
78
                $children[$index][self::CHILDREN_QUERY_FIELD] = $this->getChildren($children[$index], $data);
79
            }
80
81
            $children[$index] = $this->formatCategoryNode($children[$index]);
82
        }
83
84
        return $children;
85
    }
86
87
    /**
88
     * @return string
89
     */
90
    protected function collectResourceType()
91
    {
92
        return self::CATEGORIES;
93
    }
94
95
    /**
96
     * @param array $collectItemData
97
     *
98
     * @return array
99
     */
100
    protected function formatCategoryNode(array $collectItemData)
101
    {
102
        return [
103
            static::ID_COLUMN => $collectItemData[self::ID_CATEGORY_NODE_QUERY_FIELD],
104
            static::PARENT_COLUMN => $collectItemData[self::PARENTS_QUERY_FIELD],
105
            static::NAME_COLUMN => $collectItemData[self::NAME_QUERY_FIELD],
106
        ];
107
    }
108
109
    /**
110
     * @param array $node
111
     * @param array $data
112
     *
113
     * @return string
114
     */
115
    protected function getParents(array $node, array $data)
116
    {
117
        $parents = array_filter($data, function ($item) use ($node) {
118
            return ((int)$item[self::ID_CATEGORY_NODE_QUERY_FIELD] === (int)$node[self::FK_PARENT_CATEGORY_NODE]);
119
        });
120
121
        $result = 'ROOT';
122
        foreach ($parents as $parent) {
123
            $result = $parent[self::ID_CATEGORY_NODE_QUERY_FIELD];
124
        }
125
126
        return $result;
127
    }
128
129
}
130