Completed
Pull Request — master (#405)
by Stefan
02:47
created

UnitMapper::getSdkLocalizedUnits()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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\Utils;
9
10
use Shopware\Connect\Units;
11
use ShopwarePlugins\Connect\Components\Config;
12
use Shopware\Components\Model\ModelManager;
13
use Shopware\Models\Article\Unit;
14
15
/**
16
 * Class UnitMapper
17
 * @package ShopwarePlugins\Connect\Components\Utils
18
 */
19
class UnitMapper
20
{
21
    const ADOPT_UNIT_KEY = 'connect_adopt_unit';
22
23
    /** @var \ShopwarePlugins\Connect\Components\Config */
24
    private $configComponent;
25
26
    private $manager;
27
28
    /** @var \Shopware\Connect\Units */
29
    private $sdkUnits;
30
31
    private $repository;
32
33
    /**
34
     * @param Config $configComponent
35
     * @param ModelManager $manager
36
     */
37
    public function __construct(
38
        Config $configComponent,
39
        ModelManager $manager
40
    ) {
41
        $this->configComponent = $configComponent;
42
        $this->manager = $manager;
43
    }
44
45
    /**
46
     * Returns connect unit
47
     * @param $shopwareUnit
48
     * @return string
49
     */
50
    public function getConnectUnit($shopwareUnit)
51
    {
52
        // search for configured unit mapping
53
        $unit = $this->configComponent->getConfig($shopwareUnit);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $unit is correct as $this->configComponent->getConfig($shopwareUnit) (which targets ShopwarePlugins\Connect\...nts\Config::getConfig()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
54
        if ($unit) {
55
            return $unit;
56
        }
57
58
        $connectUnits = $this->getSdkLocalizedUnits();
59
60
        // search for same key in connect units
61
        if ($connectUnits[$shopwareUnit]) {
62
            return $shopwareUnit;
63
        }
64
65
        // search for same label in connect units
66
        $repository = $this->getUnitRepository();
67
        $unitModel = $repository->findOneBy(['unit' => $shopwareUnit]);
68
69
        if ($unitModel) {
70
            $unitName = $unitModel->getName();
71
72
            foreach ($connectUnits as $key => $connectUnit) {
73
                if ($connectUnit == $unitName) {
74
                    return $key;
75
                }
76
            }
77
78
            // search in "de" connect units
79
            $deConnectUnits = $this->getSdkLocalizedUnits('de');
80
            foreach ($deConnectUnits as $key => $connectUnit) {
81
                if ($connectUnit == $unitName) {
82
                    return $key;
83
                }
84
            }
85
        }
86
87
        return $shopwareUnit;
88
    }
89
90
    /**
91
     * Returns shopware unit
92
     * @param $connectUnit
93
     * @return mixed
94
     */
95
    public function getShopwareUnit($connectUnit)
96
    {
97
        // search for configured unit mapping
98
        $config = $this->configComponent->getConfigByValue($connectUnit);
99
        if ($config) {
100
            return $config->getName();
101
        }
102
103
        // search for same key in Shopware units
104
        $repository = $this->getUnitRepository();
105
        /** @var \Shopware\Models\Article\Unit $unitModel */
106
        $unitModel = $repository->findOneBy(['unit' => $connectUnit]);
107
108
        if ($unitModel) {
109
            return $unitModel->getUnit();
110
        }
111
112
        $connectUnits = $this->getSdkLocalizedUnits();
113
114
        // search for same label in Shopware units
115 View Code Duplication
        if ($connectUnits[$connectUnit]) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
116
            $unitModel = $repository->findOneBy(['name' => $connectUnits[$connectUnit]]);
117
118
            if ($unitModel) {
119
                return $unitModel->getUnit();
120
            }
121
        }
122
123
        // search for same label in "de" Shopware units
124
        $deConnectUnits = $this->getSdkLocalizedUnits('de');
125 View Code Duplication
        if ($deConnectUnits[$connectUnit]) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
126
            $unitModel = $repository->findOneBy(['name' => $deConnectUnits[$connectUnit]]);
127
128
            if ($unitModel) {
129
                return $unitModel->getUnit();
130
            }
131
        }
132
133
        if ($this->configComponent->getConfig('createUnitsAutomatically', false) == true) {
134
            // only german units for now
135
            $units = $this->createUnits([$connectUnit]);
136
            /** @var \Shopware\Models\Article\Unit $unit */
137
            $unit = $units[0];
138
139
            return $unit->getUnit();
140
        }
141
142
        return $connectUnit;
143
    }
144
145
    /**
146
     * Creates units shopware entities
147
     *
148
     * @param array $units
149
     * @return \Shopware\Models\Article\Unit[]
150
     */
151
    public function createUnits(array $units)
152
    {
153
        $deConnectUnits = $this->getSdkLocalizedUnits('de');
154
155
        $models = [];
156
157
        foreach ($units as $unitKey) {
158
            $unit = $this->getUnitRepository()->findOneBy([
159
                'unit' => $unitKey
160
            ]);
161
162
            if (!$unit) {
163
                if (!isset($deConnectUnits[$unitKey])) {
164
                    continue;
165
                }
166
                $unit = new Unit();
167
                $unit->setName($deConnectUnits[$unitKey]);
168
                $unit->setUnit($unitKey);
169
                $this->manager->persist($unit);
170
                $this->manager->flush($unit);
171
            }
172
173
            $models[] = $unit;
174
            $this->configComponent->setConfig($unitKey, $unitKey, null, 'units');
175
        }
176
177
        return $models;
178
    }
179
180
    /**
181
     * @return Units
182
     */
183
    private function getSdkUnits()
184
    {
185
        if ($this->sdkUnits === null) {
186
            $this->sdkUnits = new Units();
187
        }
188
189
        return $this->sdkUnits;
190
    }
191
192
    /**
193
     * Returns connect units
194
     * @param string $locale
195
     * @return array
196
     */
197
    private function getSdkLocalizedUnits($locale = 'en')
198
    {
199
        return $this->getSdkUnits()->getLocalizedUnits($locale);
200
    }
201
202
    /**
203
     * Returns Shopware units repository instance
204
     * @return mixed
205
     */
206
    private function getUnitRepository()
207
    {
208
        if ($this->repository === null) {
209
            $this->repository = $this->manager->getRepository('Shopware\Models\Article\Unit');
210
        }
211
212
        return $this->repository;
213
    }
214
}
215