Completed
Push — master ( 512122...851336 )
by Tobias
18s
created

DefaultCategoryResolver::buildLocalSubtrees()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 4
dl 0
loc 14
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * (c) shopware AG <[email protected]>
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
8
namespace ShopwarePlugins\Connect\Components\CategoryResolver;
9
10
use ShopwarePlugins\Connect\Components\CategoryResolver;
11
12
class DefaultCategoryResolver extends CategoryResolver
13
{
14
    /**
15
     * {@inheritdoc}
16
     */
17
    public function resolve(array $categories, $shopId, $stream)
18
    {
19
        $tree = $this->generateTree($categories);
20
21
        return array_keys($this->findAssignedLocalCategories($tree, $shopId, $stream));
22
    }
23
24
    /**
25
     * @param array $children
26
     * @param int $shopId
27
     * @param string $stream
28
     * @return array
29
     */
30
    private function findAssignedLocalCategories(array $children, $shopId, $stream)
31
    {
32
        $mappedCategories = [];
33
        foreach ($children as $category) {
34
            $localCategories = $this->getDirectlyAssignedCategories($category['categoryId'], $shopId, $stream);
35
            //use + not array_merge because we want to preserve the numeric keys
36
            $mappedCategories = $mappedCategories + $this->buildLocalSubtrees($shopId, $stream, $localCategories, $category['children']);
37
            $mappedCategories = $mappedCategories + $this->findAssignedLocalCategoriesForChildCategories($shopId, $stream, $category['children']);
38
        }
39
40
        return $mappedCategories;
41
    }
42
43
    /**
44
     * @param int $remoteCategoryId
45
     * @param int $shopId
46
     * @param string $stream
47
     * @return array
48
     */
49
    private function getDirectlyAssignedCategories($remoteCategoryId, $shopId, $stream)
50
    {
51
        $localCategories = $this->manager->getConnection()->executeQuery('
52
              SELECT pclc.local_category_id
53
              FROM s_plugin_connect_categories_to_local_categories AS pclc
54
              INNER JOIN s_plugin_connect_categories AS pcc ON pcc.id = pclc.remote_category_id
55
              WHERE pcc.category_key = ? AND pcc.shop_id = ? AND (pclc.stream = ? OR pclc.stream IS NULL) ',
56
            [$remoteCategoryId, $shopId, $stream]
57
        )->fetchAll(\PDO::FETCH_COLUMN);
58
59
        if ($localCategories === false) {
60
            return [];
61
        } else {
62
            return $localCategories;
63
        }
64
    }
65
66
    /**
67
     * @param int $shopId
68
     * @param string $stream
69
     * @param array $localCategories
70
     * @param array $children
71
     * @return array
72
     */
73
    private function buildLocalSubtrees($shopId, $stream, array $localCategories, array $children)
74
    {
75
        $mappedCategories = [];
76
        foreach ($localCategories as $localCategory) {
77
            $mappedCategories[$localCategory] = true;
78
            if (count($children) > 0) {
79
                foreach ($this->convertTreeToKeys($children, $localCategory, $shopId, $stream) as $local) {
80
                    $mappedCategories[$local['categoryKey']] = true;
81
                }
82
            }
83
        }
84
85
        return $mappedCategories;
86
    }
87
88
    /**
89
     * @param int $shopId
90
     * @param string $stream
91
     * @param array $children
92
     * @return array
93
     */
94
    private function findAssignedLocalCategoriesForChildCategories($shopId, $stream, $children)
95
    {
96
        $childCategories = [];
97
        if (count($children) > 0) {
98
            $childCategories = $this->findAssignedLocalCategories($children, $shopId, $stream);
99
        }
100
101
        return $childCategories;
102
    }
103
}
104