Completed
Pull Request — master (#433)
by Jonas
02:53
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
        return (array) $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
60
    /**
61
     * @param int $shopId
62
     * @param string $stream
63
     * @param array $localCategories
64
     * @param array $children
65
     * @return array
66
     */
67
    private function buildLocalSubtrees($shopId, $stream, array $localCategories, array $children)
68
    {
69
        $mappedCategories = [];
70
        foreach ($localCategories as $localCategory) {
71
            $mappedCategories[$localCategory] = true;
72
            if (count($children) > 0) {
73
                foreach ($this->convertTreeToKeys($children, $localCategory, $shopId, $stream) as $local) {
74
                    $mappedCategories[$local['categoryKey']] = true;
75
                }
76
            }
77
        }
78
79
        return $mappedCategories;
80
    }
81
82
    /**
83
     * @param int $shopId
84
     * @param string $stream
85
     * @param array $children
86
     * @return array
87
     */
88
    private function findAssignedLocalCategoriesForChildCategories($shopId, $stream, $children)
89
    {
90
        $childCategories = [];
91
        if (count($children) > 0) {
92
            $childCategories = $this->findAssignedLocalCategories($children, $shopId, $stream);
93
        }
94
95
        return $childCategories;
96
    }
97
}
98