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

AutoCategoryResolver::generateTree()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 18
nc 4
nop 2
dl 0
loc 30
rs 8.5806
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
use Shopware\CustomModels\Connect\RemoteCategoryRepository;
12
use Shopware\Models\Category\Category;
13
use Shopware\Models\Category\Repository as CategoryRepository;
14
use Shopware\Components\Model\ModelManager;
15
use ShopwarePlugins\Connect\Components\Config;
16
use Shopware\CustomModels\Connect\ProductToRemoteCategoryRepository;
17
use Shopware\CustomModels\Connect\ProductToRemoteCategory;
18
use Shopware\Components\Model\CategoryDenormalization;
19
20
class AutoCategoryResolver extends CategoryResolver
21
{
22
    /**
23
     * @var Config
24
     */
25
    private $config;
26
27
    /**
28
     * AutoCategoryResolver constructor.
29
     * @param ModelManager $manager
30
     * @param CategoryRepository $categoryRepository
31
     * @param RemoteCategoryRepository $remoteCategoryRepository
32
     * @param Config $config
33
     * @param ProductToRemoteCategoryRepository $productToRemoteCategoryRepository
0 ignored issues
show
Documentation introduced by
Should the type for parameter $productToRemoteCategoryRepository not be null|ProductToRemoteCategoryRepository?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
34
     */
35
    public function __construct(
36
        ModelManager $manager,
37
        CategoryRepository $categoryRepository,
38
        RemoteCategoryRepository $remoteCategoryRepository,
39
        Config $config,
40
        CategoryDenormalization $categoryDenormalization,
41
        ProductToRemoteCategoryRepository $productToRemoteCategoryRepository = null
42
    ) {
43
        if (!$productToRemoteCategoryRepository) {
44
            $productToRemoteCategoryRepository = $manager->getRepository(ProductToRemoteCategory::class);
45
        }
46
        parent::__construct(
47
            $manager,
48
            $remoteCategoryRepository,
49
            $productToRemoteCategoryRepository,
50
            $categoryRepository,
51
            $categoryDenormalization
52
        );
53
54
        $this->config = $config;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function resolve(array $categories, $shopId, $stream)
61
    {
62
        $tree = $this->generateTree($categories);
63
64
        // we need to foreach, cause we may have two main nodes
65
        // example:
66
        // Deutsch/Category/Subcategory
67
        // English/Category/Subcategory
68
        $remoteCategories = [];
69
        foreach ($tree as $node) {
70
            $mainCategory = $this->categoryRepository->findOneBy([
71
                'name' => $node['name'],
72
                'parentId' => 1,
73
            ]);
74
            // if connectTree has a Subtree starting with Spanish but MerchantShop has no mainCategory Spanish
75
            // the categories below Spanish won't be created
76
            if ($mainCategory == null) {
77
                continue;
78
            }
79
80
            $remoteCategories = array_merge($remoteCategories, $this->convertTreeToKeys($node['children'], $mainCategory->getId(), $shopId, $stream));
81
        }
82
83
        // Collect all, not only leaf categories. Some customers use them to assign products.
84
        // Do not fetch them from database by name as before.
85
        // it is possible to have more than one subcategory "Boots" - CON-4589
86
        return array_map(function ($category) {
87
            return $category['categoryKey'];
88
        }, $remoteCategories);
89
    }
90
}
91