Completed
Pull Request — master (#369)
by Stefan
02:34
created

CategoryResolver   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 86
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
resolve() 0 1 ?
generateTree() 0 1 ?
B storeRemoteCategories() 0 33 5
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;
9
10
use Shopware\CustomModels\Connect\ProductToRemoteCategory;
11
use Shopware\CustomModels\Connect\RemoteCategory;
12
use Shopware\CustomModels\Connect\RemoteCategoryRepository;
13
use Shopware\Components\Model\ModelManager;
14
use Shopware\CustomModels\Connect\ProductToRemoteCategoryRepository;
15
16
abstract class CategoryResolver
17
{
18
    /**
19
     * @var ModelManager
20
     */
21
    protected $manager;
22
23
    /**
24
     * @var \Shopware\CustomModels\Connect\RemoteCategoryRepository
25
     */
26
    protected $remoteCategoryRepository;
27
28
    /**
29
     * @var \Shopware\CustomModels\Connect\ProductToRemoteCategoryRepository
30
     */
31
    protected $productToRemoteCategoryRepository;
32
33
    public function __construct(
34
        ModelManager $manager,
35
        RemoteCategoryRepository $remoteCategoryRepository,
36
        ProductToRemoteCategoryRepository $productToRemoteCategoryRepository
37
    ) {
38
        $this->manager = $manager;
39
        $this->remoteCategoryRepository = $remoteCategoryRepository;
40
        $this->productToRemoteCategoryRepository = $productToRemoteCategoryRepository;
41
    }
42
43
    /**
44
     * Returns array with category entities
45
     * if they don't exist will be created
46
     *
47
     * @param array $categories
48
     * @return \Shopware\Models\Category\Category[]
49
     */
50
    abstract public function resolve(array $categories);
51
52
    /**
53
     * Generates categories tree by given array of categories
54
     *
55
     * @param array $categories
56
     * @param string $idPrefix
57
     * @return array
58
     */
59
    abstract public function generateTree(array $categories, $idPrefix = '');
60
61
    /**
62
     * Stores raw Shopware Connect categories
63
     *
64
     * @param array $categories
65
     * @param int $articleId
66
     * @return void
67
     */
68
    public function storeRemoteCategories(array $categories, $articleId)
69
    {
70
        $remoteCategories = [];
71
        foreach ($categories as $categoryKey => $category) {
72
            $remoteCategory = $this->remoteCategoryRepository->findOneBy(['categoryKey' => $categoryKey]);
73
            if (!$remoteCategory) {
74
                $remoteCategory = new RemoteCategory();
75
                $remoteCategory->setCategoryKey($categoryKey);
76
            }
77
            $remoteCategory->setLabel($category);
78
            $this->manager->persist($remoteCategory);
79
            $remoteCategories[] = $remoteCategory;
80
        }
81
        $this->manager->flush();
82
83
        /** @var $remoteCategory \Shopware\CustomModels\Connect\RemoteCategory */
84
        foreach ($remoteCategories as $remoteCategory) {
85
            $productToCategory = $this->productToRemoteCategoryRepository->findOneBy([
86
                'articleId' => $articleId,
87
                'connectCategoryId' => $remoteCategory->getId(),
88
            ]);
89
            if ($productToCategory) {
90
                continue;
91
            }
92
93
            $productToCategory = new ProductToRemoteCategory();
94
            $productToCategory->setArticleId($articleId);
95
            $productToCategory->setConnectCategory($remoteCategory);
96
            $this->manager->persist($productToCategory);
97
        }
98
99
        $this->manager->flush();
100
    }
101
}
102