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

ProductTranslator::getLocaleRepository()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 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\Translations;
9
10
use ShopwarePlugins\Connect\Components\Config;
11
use ShopwarePlugins\Connect\Components\Gateway\ProductTranslationsGateway;
12
use Shopware\Components\Model\ModelManager;
13
use Shopware\Connect\Struct\Translation;
14
15
class ProductTranslator implements ProductTranslatorInterface
16
{
17
    /**
18
     * @var \ShopwarePlugins\Connect\Components\Config
19
     */
20
    private $config;
21
22
    /**
23
     * @var \ShopwarePlugins\Connect\Components\Gateway\ProductTranslationsGateway
24
     */
25
    private $productTranslationsGateway;
26
27
    /**
28
     * @var \Shopware\Components\Model\ModelManager
29
     */
30
    private $manager;
31
32
    /**
33
     * @var string
34
     */
35
    private $baseProductUrl;
36
37
    private $shopRepository;
38
39
    private $localeRepository;
0 ignored issues
show
Unused Code introduced by
The property $localeRepository is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
40
41
    public function __construct(
42
        Config $config,
43
        ProductTranslationsGateway $productTranslationsGateway,
44
        ModelManager $manager,
45
        $baseProductUrl
46
    ) {
47
        $this->config = $config;
48
        $this->productTranslationsGateway = $productTranslationsGateway;
49
        $this->manager = $manager;
50
        $this->baseProductUrl = $baseProductUrl;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function translate($productId, $sourceId)
57
    {
58
        $exportLanguages = $this->config->getConfig('exportLanguages');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $exportLanguages is correct as $this->config->getConfig('exportLanguages') (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...
59
        $exportLanguages = $exportLanguages ?: [];
60
61
        $translations = $this->productTranslationsGateway->getTranslations($productId, $exportLanguages);
62
63
        $result = [];
64
        foreach ($translations as $shopId => $translation) {
65
            /** @var \Shopware\Models\Shop\Shop $shop */
66
            $shop = $this->getShopRepository()->find($shopId);
67
            if (!$shop) {
68
                continue;
69
            }
70
71
            /** @var \Shopware\Models\Shop\Locale $locale */
72
            $locale = $shop->getLocale();
73
            if (strlen($locale->getLocale()) === 0) {
74
                continue;
75
            }
76
77
            $localeCode = explode('_', $locale->getLocale());
78
79
            if (count($localeCode) === 0) {
80
                continue;
81
            }
82
83
            $result[$localeCode[0]] = new Translation(
84
                [
85
                    'title' => isset($translation['title']) ? $translation['title'] : '',
86
                    'shortDescription' => isset($translation['shortDescription']) ? $translation['shortDescription'] : '',
87
                    'longDescription' => isset($translation['longDescription']) ? $translation['longDescription'] : '',
88
                    'additionalDescription' => isset($translation['additionalDescription']) ? $translation['additionalDescription'] : '',
89
                    'url' => $this->getUrlForProduct($sourceId, $shop->getId()),
90
                ]
91
            );
92
        }
93
94
        return $result;
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function translateConfiguratorGroup($groupId, $groupName, $translations)
101
    {
102
        // exportLanguages actually is shop ids
103
        $exportLanguages = $this->config->getConfig('exportLanguages');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $exportLanguages is correct as $this->config->getConfig('exportLanguages') (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...
104
        $exportLanguages = $exportLanguages ?: [];
105
106
        $groupTranslations = $this->productTranslationsGateway->getConfiguratorGroupTranslations($groupId, $exportLanguages);
107
        foreach ($exportLanguages as $shopId) {
108
            if ($shopId === 1) {
109
                continue;
110
            }
111
            /** @var \Shopware\Models\Shop\Shop $shop */
112
            $shop = $this->getShopRepository()->find($shopId);
113
            if (!$shop) {
114
                continue;
115
            }
116
117
            /** @var \Shopware\Models\Shop\Locale $locale */
118
            $locale = $shop->getLocale();
119
            if (!$locale) {
120
                continue;
121
            }
122
123
            $localeCode = explode('_', $locale->getLocale());
124
            if (count($localeCode) === 0) {
125
                continue;
126
            }
127
128
            $groupTranslation = isset($groupTranslations[$shopId]) ? $groupTranslations[$shopId] : '';
129
            if (!array_key_exists($localeCode[0], $translations)) {
130
                continue;
131
            }
132
            $translationStruct = $translations[$localeCode[0]];
133
            if (!$translationStruct instanceof Translation) {
134
                continue;
135
            }
136
137
            $translationStruct->variantLabels[$groupName] = $groupTranslation;
138
        }
139
140
        return $translations;
141
    }
142
143
    /**
144
     * {@inheritdoc}
145
     */
146
    public function translateConfiguratorOption($optionId, $optionName, $translations)
147
    {
148
        $exportLanguages = $this->config->getConfig('exportLanguages');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $exportLanguages is correct as $this->config->getConfig('exportLanguages') (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...
149
        $exportLanguages = $exportLanguages ?: [];
150
151
        $optionTranslations = $this->productTranslationsGateway->getConfiguratorOptionTranslations($optionId, $exportLanguages);
152
        foreach ($exportLanguages as $shopId) {
153
            if ($shopId === 1) {
154
                continue;
155
            }
156
            /** @var \Shopware\Models\Shop\Shop $shop */
157
            $shop = $this->getShopRepository()->find($shopId);
158
            /** @var \Shopware\Models\Shop\Locale $locale */
159
            $locale = $shop->getLocale();
160
            if (!$locale) {
161
                continue;
162
            }
163
164
            $localeCode = explode('_', $locale->getLocale());
165
            if (count($localeCode) === 0) {
166
                continue;
167
            }
168
169
            $optionTranslation = isset($optionTranslations[$shopId]) ? $optionTranslations[$shopId] : '';
170
            $translationStruct = $translations[$localeCode[0]];
171
            if (!$translationStruct instanceof Translation) {
172
                continue;
173
            }
174
175
            $translationStruct->variantValues[$optionName] = $optionTranslation;
176
        }
177
178
        return $translations;
179
    }
180
181
    /**
182
     * {@inheritdoc}
183
     */
184
    public function validate(Translation $translation, $optionsCount)
185
    {
186
        // currently we dont want to stop the other translations like description
187
        // even we dont have translated title
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
188
//        if (strlen($translation->title) === 0) {
189
//            throw new \Exception('Translation title cannot be empty string.');
190
//        }
191
192
        if (strlen($translation->url) === 0) {
193
            throw new \Exception('Translation url cannot be empty string.');
194
        }
195
196
        if (count($translation->variantLabels) != $optionsCount) {
197
            throw new \Exception('variantLabels property has not correct items count.');
198
        }
199
200
        if (count($translation->variantValues) != $optionsCount) {
201
            throw new \Exception('variantValues property has not correct items count.');
202
        }
203
204
        return true;
205
    }
206
207 View Code Duplication
    public function getUrlForProduct($productId, $shopId = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
208
    {
209
        $shopId = (int) $shopId;
210
        $url = $this->baseProductUrl . $productId;
211
        if ($shopId > 0) {
212
            $url = $url . '/shId/' . $shopId;
213
        }
214
215
        return $url;
216
    }
217
218
    private function getShopRepository()
219
    {
220
        if (!$this->shopRepository) {
221
            $this->shopRepository = $this->manager->getRepository('Shopware\Models\Shop\Shop');
222
        }
223
224
        return $this->shopRepository;
225
    }
226
}
227