EcondaCategoryCollector   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A collectResourceType() 0 3 1
A collectItem() 0 3 1
A formatCategoryNode() 0 6 1
A getParents() 0 12 2
A collectData() 0 12 2
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
    protected const ROOT_CATEGORY = 'ROOT';
16
17
    // CSV File Columns
18
    protected const ID_COLUMN = 'ID';
19
    protected const PARENT_COLUMN = 'ParentID';
20
    protected const NAME_COLUMN = 'Name';
21
22
    // Internal Query Fields
23
    protected const ID_CATEGORY_NODE_QUERY_FIELD = 'id_category_node';
24
    protected const PARENTS_QUERY_FIELD = 'parents';
25
    protected const NAME_QUERY_FIELD = 'name';
26
    protected const CHILDREN_QUERY_FIELD = 'children';
27
    protected const FK_PARENT_CATEGORY_NODE = 'fk_parent_category_node';
28
29
    protected const RESOURCE_TYPE = 'categories';
30
31
    /**
32
     * @param array $collectedSet
33
     * @param \Generated\Shared\Transfer\LocaleTransfer $localeTransfer
34
     *
35
     * @return array
36
     */
37
    protected function collectData(array $collectedSet, LocaleTransfer $localeTransfer): array
38
    {
39
        $setToExport = [];
40
41
        foreach ($collectedSet as $collectedItemData) {
42
            $collectedItemData[static::CHILDREN_QUERY_FIELD] = $this->getChildren($collectedItemData, $collectedSet);
43
            $collectedItemData[static::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): array
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): array
71
    {
72
        $children = array_filter($data, function ($item) use ($node) {
73
            return ((int)$item[static::FK_PARENT_CATEGORY_NODE] === (int)$node[static::ID_CATEGORY_NODE_QUERY_FIELD]);
74
        });
75
76
        foreach ($children as $index => $child) {
77
            if ($nested) {
78
                $children[$index][static::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(): string
91
    {
92
        return static::RESOURCE_TYPE;
93
    }
94
95
    /**
96
     * @param array $collectItemData
97
     *
98
     * @return array
99
     */
100
    protected function formatCategoryNode(array $collectItemData): array
101
    {
102
        return [
103
            static::ID_COLUMN => $collectItemData[static::ID_CATEGORY_NODE_QUERY_FIELD],
104
            static::PARENT_COLUMN => $collectItemData[static::PARENTS_QUERY_FIELD],
105
            static::NAME_COLUMN => $collectItemData[static::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): string
116
    {
117
        $parents = array_filter($data, function ($item) use ($node) {
118
            return ((int)$item[static::ID_CATEGORY_NODE_QUERY_FIELD] === (int)$node[static::FK_PARENT_CATEGORY_NODE]);
119
        });
120
121
        $lastParent = end($parents);
122
        if ($lastParent === false) {
123
            return static::ROOT_CATEGORY; // 'ROOT'
124
        }
125
126
        return $lastParent[static::ID_CATEGORY_NODE_QUERY_FIELD];
127
    }
128
}
129