Completed
Pull Request — master (#366)
by Christian
29:05
created

LocalProductQuery::getConnectProduct()   F

Complexity

Conditions 24
Paths 641

Size

Total Lines 96
Code Lines 59

Duplication

Lines 9
Ratio 9.38 %

Importance

Changes 0
Metric Value
cc 24
eloc 59
nc 641
nop 1
dl 9
loc 96
rs 2.248
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\ProductQuery;
9
10
use Doctrine\ORM\QueryBuilder;
11
use Shopware\Bundle\StoreFrontBundle\Service\ContextServiceInterface;
12
use Shopware\Bundle\StoreFrontBundle\Service\Core\ContextService;
13
use Shopware\Bundle\StoreFrontBundle\Struct\ListProduct;
14
use Shopware\Connect\Struct\Product;
15
use Shopware\Connect\Struct\Property;
16
use ShopwarePlugins\Connect\Components\Exceptions\NoLocalProductException;
17
use ShopwarePlugins\Connect\Components\Marketplace\MarketplaceGateway;
18
use ShopwarePlugins\Connect\Components\MediaService;
19
use ShopwarePlugins\Connect\Components\Translations\ProductTranslatorInterface;
20
use Shopware\Components\Model\ModelManager;
21
use ShopwarePlugins\Connect\Components\Utils\UnitMapper;
22
use Shopware\Connect\Struct\PriceRange;
23
use Enlight_Event_EventManager;
24
25
/**
26
 * Will return a local product (e.g. for export) as Shopware\Connect\Struct\Product
27
 * Configured fields for price- and description export will be taken into account
28
 *
29
 * Class LocalProductQuery
30
 * @package ShopwarePlugins\Connect\Components\ProductQuery
31
 */
32
class LocalProductQuery extends BaseProductQuery
33
{
34
    const IMAGE_LIMIT = 10;
35
36
    const VARIANT_IMAGE_LIMIT = 10;
37
38
    protected $baseProductUrl;
39
40
    /**
41
     * @var \ShopwarePlugins\Connect\Components\Config
42
     */
43
    protected $configComponent;
44
45
    /**
46
     * @var MarketplaceGateway
47
     */
48
    protected $marketplaceGateway;
49
50
    /**
51
     * @var \ShopwarePlugins\Connect\Components\Translations\ProductTranslatorInterface
52
     */
53
    protected $productTranslator;
54
55
    /**
56
     * @var \Shopware\Bundle\StoreFrontBundle\Service\ContextServiceInterface
57
     */
58
    protected $contextService;
59
60
    /**
61
     * @var \Shopware\Bundle\StoreFrontBundle\Service\Core\MediaService
62
     */
63
    protected $localMediaService;
64
65
    /**
66
     * @var \Shopware\Bundle\StoreFrontBundle\Struct\ProductContext
67
     */
68
    protected $productContext;
69
70
    /**
71
     * @var Enlight_Event_EventManager
72
     */
73
    private $eventManager;
74
75
    /**
76
     * LocalProductQuery constructor.
77
     * @param ModelManager $manager
78
     * @param null $baseProductUrl
79
     * @param $configComponent
80
     * @param MarketplaceGateway $marketplaceGateway
81
     * @param ProductTranslatorInterface $productTranslator
82
     * @param ContextServiceInterface $contextService
83
     * @param MediaService $storeFrontMediaService
84
     * @param null $mediaService
85
     * @param Enlight_Event_EventManager $eventManager
86
     */
87
    public function __construct(
88
        ModelManager $manager,
89
        $baseProductUrl,
90
        $configComponent,
91
        MarketplaceGateway $marketplaceGateway,
92
        ProductTranslatorInterface $productTranslator,
93
        ContextServiceInterface $contextService,
94
        MediaService $storeFrontMediaService,
95
        Enlight_Event_EventManager $eventManager,
96
        $mediaService = null
97
    ) {
98
        parent::__construct($manager, $mediaService);
99
100
        $this->baseProductUrl = $baseProductUrl;
101
        $this->configComponent = $configComponent;
102
        $this->marketplaceGateway = $marketplaceGateway;
103
        $this->productTranslator = $productTranslator;
104
        $this->contextService = $contextService;
105
        $this->eventManager = $eventManager;
106
        $this->localMediaService = $storeFrontMediaService;
0 ignored issues
show
Documentation Bug introduced by
It seems like $storeFrontMediaService of type object<ShopwarePlugins\C...omponents\MediaService> is incompatible with the declared type object<Shopware\Bundle\S...vice\Core\MediaService> of property $localMediaService.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
107
108
        // products context is needed to load product media
109
        // it's used for image translations
110
        // in our case translations are not used
111
        // so we don't care about shop language
112
        $this->productContext = $this->contextService->createShopContext(
113
            $this->configComponent->getDefaultShopId(),
114
            null,
115
            ContextService::FALLBACK_CUSTOMER_GROUP
116
        );
117
    }
118
119
    /**
120
     * @return QueryBuilder
121
     */
122
    public function getProductQuery()
123
    {
124
        $articleAttributeAlias = 'attribute';
125
        $exportPriceCustomerGroup = $this->configComponent->getConfig('priceGroupForPriceExport', 'EK');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $exportPriceCustomerGroup is correct as $this->configComponent->...pForPriceExport', 'EK') (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...
126
        $exportPurchasePriceCustomerGroup = $this->configComponent->getConfig('priceGroupForPurchasePriceExport', 'EK');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $exportPurchasePriceCustomerGroup is correct as $this->configComponent->...hasePriceExport', 'EK') (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...
127
        $exportPriceColumn = $this->configComponent->getConfig('priceFieldForPriceExport');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $exportPriceColumn is correct as $this->configComponent->...ceFieldForPriceExport') (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...
128
        $exportPurchasePriceColumn = $this->configComponent->getConfig('priceFieldForPurchasePriceExport');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $exportPurchasePriceColumn is correct as $this->configComponent->...orPurchasePriceExport') (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...
129
130
        $builder = $this->manager->createQueryBuilder();
131
132
        $builder->from('Shopware\CustomModels\Connect\Attribute', 'at');
133
        $builder->join('at.article', 'a');
134
        $builder->join('at.articleDetail', 'd');
135
        $builder->leftJoin('a.supplier', 's');
136
        $builder->join('a.tax', 't');
137
        $builder->leftJoin('d.attribute', 'attribute');
138
        $builder->leftJoin('d.unit', 'u');
139
        $builder->where('at.shopId IS NULL');
140
        $selectColumns = [
141
            'a.id as localId',
142
            'd.id as detailId',
143
            'd.number as sku',
144
            'at.shopId as shopId',
145
            'at.sourceId as sourceId',
146
            'd.kind as detailKind',
147
            'd.ean',
148
            'a.name as title',
149
            's.name as vendorName',
150
            's.image as vendorImage',
151
            's.link as vendorLink',
152
            's.description as vendorDescription',
153
            's.metaTitle as vendorMetaTitle',
154
            't.tax / 100 as vat',
155
156
            'd.releaseDate as deliveryDate',
157
            'd.inStock as availability',
158
            'd.minPurchase as minPurchaseQuantity',
159
160
            'd.width',
161
            'd.height',
162
            'd.len as length',
163
164
            'd.weight',
165
            'u.unit',
166
            'd.purchaseUnit as purchaseUnit',
167
            'd.referenceUnit as referenceUnit',
168
            'd.packUnit as packageUnit',
169
            'd.minPurchase as basicUnit',
170
            'd.supplierNumber as manufacturerNumber',
171
            'at.category as category',
172
            'at.fixedPrice as fixedPrice',
173
            'd.shippingTime as deliveryWorkDays',
174
            'a.lastStock',
175
        ];
176
177
        if ($this->configComponent->getConfig(self::SHORT_DESCRIPTION_FIELD, false)) {
178
            $selectColumns[] = 'a.description as shortDescription';
179
        }
180
181
        if ($this->configComponent->getConfig(self::LONG_DESCRIPTION_FIELD, false)) {
182
            $selectColumns[] = 'a.descriptionLong as longDescription';
183
        }
184
185
        if ($this->configComponent->getConfig(self::CONNECT_DESCRIPTION_FIELD, false)) {
186
            $selectColumns[] = 'attribute.connectProductDescription as additionalDescription';
187
        }
188
189
        if ($exportPriceColumn) {
190
            $selectColumns[] = "exportPrice.{$exportPriceColumn} as price";
191
        }
192
        if ($exportPurchasePriceColumn && $exportPurchasePriceColumn == 'detailPurchasePrice') {
193
            $selectColumns[] = 'd.purchasePrice as purchasePrice';
194
        } elseif ($exportPurchasePriceColumn) {
195
            $selectColumns[] = "exportPurchasePrice.{$exportPurchasePriceColumn} as purchasePrice";
196
        }
197
198
        $builder->select($selectColumns);
199
200
        $builder = $this->addMarketplaceAttributeSelect($builder, $articleAttributeAlias);
201
        $builder = $this->addPriceJoins($builder, $exportPriceColumn, $exportPurchasePriceColumn);
202
203
        $builder->setParameter('priceCustomerGroup', $exportPriceCustomerGroup);
204
        if ($exportPurchasePriceColumn != 'detailPurchasePrice') {
205
            $builder->setParameter('purchasePriceCustomerGroup', $exportPurchasePriceCustomerGroup);
206
        }
207
208
        return $builder;
209
    }
210
211
    /**
212
     * @param array $rows
213
     * @return array
214
     */
215
    public function getConnectProducts($rows)
216
    {
217
        $products = [];
218
        foreach ($rows as $row) {
219
            $products[] = $this->getConnectProduct($row);
220
        }
221
222
        return $products;
223
    }
224
225
    /**
226
     * @param $row
227
     * @throws NoLocalProductException
228
     * @return Product
229
     */
230
    public function getConnectProduct($row)
231
    {
232
        $row = $this->prepareCommonAttributes($row);
233
        $row['translations'] = $this->productTranslator->translate($row['localId'], $row['sourceId']);
234
235
        if (!empty($row['shopId'])) {
236
            throw new NoLocalProductException("Product {$row['title']} is not a local product");
237
        }
238
239
        $row['url'] = $this->getUrlForProduct($row['sourceId']);
240
        $row['priceRanges'] = $this->preparePriceRanges($row['detailId']);
241
242
        $row['properties'] = $this->prepareProperties($row['localId']);
243
244
        $product = new ListProduct($row['localId'], $row['detailId'], $row['sku']);
245
246
        $sku = $row['sku'];
247
        $row['images'] = [];
248
        $mediaFiles = $this->localMediaService->getProductMediaList([$product], $this->productContext);
249
        if (array_key_exists($sku, $mediaFiles) && $mediaFiles[$sku]) {
250
            $mediaFiles[$sku] = array_slice($mediaFiles[$sku], 0, self::IMAGE_LIMIT);
251
            foreach ($mediaFiles[$sku] as $media) {
252
                $row['images'][] = $media->getFile();
253
            }
254
        }
255
256
        $variantMediaFiles = $this->localMediaService->getVariantMediaList([$product], $this->productContext);
257
        if (array_key_exists($sku, $variantMediaFiles) && $variantMediaFiles[$sku]) {
258
            $variantMediaFiles[$sku] = array_slice($variantMediaFiles[$sku], 0, self::VARIANT_IMAGE_LIMIT);
259
            foreach ($variantMediaFiles[$sku] as $media) {
260
                $row['variantImages'][] = $media->getFile();
261
                $row['images'][] = $media->getFile();
262
            }
263
        }
264
265
        //todo@sb: find better way to collect configuration option translations
266
        $row = $this->applyConfiguratorOptions($row);
267
        $row = $this->prepareVendor($row);
268
269
        if ($row['deliveryWorkDays']) {
270
            $row['deliveryWorkDays'] = (int) $row['deliveryWorkDays'];
271
        } else {
272
            $row['deliveryWorkDays'] = null;
273
        }
274
275
        if ($this->hasVariants($row['localId'])) {
276
            $row['groupId'] = $row['localId'];
277
        }
278
279
        unset($row['localId']);
280
        unset($row['detailId']);
281
        unset($row['detailKind']);
282
283
        if (
284
            (array_key_exists(Product::ATTRIBUTE_UNIT, $row['attributes']) && $row['attributes'][Product::ATTRIBUTE_UNIT]) &&
285
            (array_key_exists(Product::ATTRIBUTE_QUANTITY, $row['attributes']) && $row['attributes'][Product::ATTRIBUTE_QUANTITY]) &&
286
            (array_key_exists(Product::ATTRIBUTE_REFERENCE_QUANTITY, $row['attributes']) && $row['attributes'][Product::ATTRIBUTE_REFERENCE_QUANTITY])) {
287
            //Map local unit to connect unit
288
            if ($row['attributes'][Product::ATTRIBUTE_UNIT]) {
289
                $unitMapper = new UnitMapper($this->configComponent, $this->manager);
290
                $row['attributes'][Product::ATTRIBUTE_UNIT] = $unitMapper->getConnectUnit($row['attributes'][Product::ATTRIBUTE_UNIT]);
291
            }
292
293
            $intRefQuantity = (int) $row['attributes'][Product::ATTRIBUTE_REFERENCE_QUANTITY];
294
            if ($row['attributes'][Product::ATTRIBUTE_REFERENCE_QUANTITY] - $intRefQuantity <= 0.0001) {
295
                $row['attributes'][Product::ATTRIBUTE_REFERENCE_QUANTITY] = $intRefQuantity;
296
            }
297
        } else {
298
            unset($row['attributes'][Product::ATTRIBUTE_UNIT]);
299
            $row['attributes'][Product::ATTRIBUTE_QUANTITY] = null;
300
            $row['attributes'][Product::ATTRIBUTE_REFERENCE_QUANTITY] = null;
301
        }
302
303 View Code Duplication
        if (!(array_key_exists(Product::ATTRIBUTE_PACKAGEUNIT, $row['attributes']) && $row['attributes'][Product::ATTRIBUTE_PACKAGEUNIT])) {
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...
304
            unset($row['attributes'][Product::ATTRIBUTE_PACKAGEUNIT]);
305
        }
306
307 View Code Duplication
        if (!(array_key_exists(Product::ATTRIBUTE_MANUFACTURERNUMBER, $row['attributes']) && $row['attributes'][Product::ATTRIBUTE_MANUFACTURERNUMBER])) {
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...
308
            unset($row['attributes'][Product::ATTRIBUTE_MANUFACTURERNUMBER]);
309
        }
310
311 View Code Duplication
        if (!(array_key_exists(Product::ATTRIBUTE_BASICUNIT, $row['attributes']) && $row['attributes'][Product::ATTRIBUTE_BASICUNIT])) {
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...
312
            unset($row['attributes'][Product::ATTRIBUTE_BASICUNIT]);
313
        }
314
        $product = new Product($row);
315
316
        $this->eventManager->notify(
317
            'Connect_Supplier_Get_Single_Product_Before',
318
            [
319
                'subject' => $this,
320
                'product' => $product
321
            ]
322
        );
323
324
        return $product;
325
    }
326
327
    /**
328
     * Will add the correct joins depending on the configuration of the price columns
329
     *
330
     * @param $builder QueryBuilder
331
     * @param $exportPriceColumn
332
     * @param $exportPurchasePriceColumn
333
     * @return QueryBuilder
334
     */
335
    public function addPriceJoins(QueryBuilder $builder, $exportPriceColumn, $exportPurchasePriceColumn)
336
    {
337
        // When the price attribute is used, we need two joins to get it
338
        if ($exportPriceColumn == 'connectPrice') {
339
            $builder->leftJoin(
340
                'd.prices',
341
                'price_join_for_export_price',
342
                'with',
343
                'price_join_for_export_price.from = 1 AND price_join_for_export_price.customerGroupKey = :priceCustomerGroup'
344
            );
345
            $builder->leftJoin('price_join_for_export_price.attribute', 'exportPrice');
346
        } else {
347
            $builder->leftJoin(
348
                'd.prices',
349
                'exportPrice',
350
                'with',
351
                'exportPrice.from = 1 AND exportPrice.customerGroupKey = :priceCustomerGroup'
352
            );
353
        }
354
355
        // When the price attribute is used, we need two joins to get it
356
        if ($exportPurchasePriceColumn == 'connectPrice') {
357
            $builder->leftJoin(
358
                'd.prices',
359
                'price_join_for_export_purchase_price',
360
                'with',
361
                'price_join_for_export_purchase_price.from = 1 AND price_join_for_export_purchase_price.customerGroupKey = :purchasePriceCustomerGroup'
362
            );
363
            $builder->leftJoin('price_join_for_export_purchase_price.attribute', 'exportPurchasePrice');
364
        } elseif ($exportPurchasePriceColumn != 'detailPurchasePrice') {
365
            $builder->leftJoin(
366
                'd.prices',
367
                'exportPurchasePrice',
368
                'with',
369
                'exportPurchasePrice.from = 1 AND exportPurchasePrice.customerGroupKey = :purchasePriceCustomerGroup'
370
            );
371
        }
372
373
        return $builder;
374
    }
375
376 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...
377
    {
378
        $shopId = (int) $shopId;
379
        $url = $this->baseProductUrl . $productId;
380
        if ($shopId > 0) {
381
            $url = $url . '/shId/' . $shopId;
382
        }
383
384
        return $url;
385
    }
386
387
    /**
388
     * Select attributes  which are already mapped to marketplace
389
     *
390
     * @param QueryBuilder $builder
391
     * @param $alias
392
     * @return QueryBuilder
393
     */
394
    private function addMarketplaceAttributeSelect(QueryBuilder $builder, $alias)
395
    {
396
        foreach ($this->marketplaceGateway->getMappings() as $mapping) {
397
            if (strlen($mapping['shopwareAttributeKey']) > 0 && strlen($mapping['attributeKey']) > 0) {
398
                $builder->addSelect("{$alias}.{$mapping['shopwareAttributeKey']}");
399
            }
400
        }
401
402
        return $builder;
403
    }
404
405
    /**
406
     * Returns shopware to martketplace attributes mapping as array
407
     *
408
     * @return array
409
     */
410
    public function getAttributeMapping()
411
    {
412
        $mappings = $this->marketplaceGateway->getMappings();
413
414
        return array_merge(
415
            array_filter(
416
                array_combine(
417
                    array_map(
418
                        function ($mapping) {
419
                            return $mapping['shopwareAttributeKey'];
420
                        },
421
                        $mappings
422
                    ),
423
                    array_map(
424
                        function ($mapping) {
425
                            return $mapping['attributeKey'];
426
                        },
427
                        $mappings
428
                    )
429
                ),
430
                function ($mapping) {
431
                    return strlen($mapping['shopwareAttributeKey']) > 0 && strlen($mapping['attributeKey']) > 0;
432
                }
433
            ),
434
            $this->attributeMapping
435
        );
436
    }
437
438
    /**
439
     * Check whether the product contains variants
440
     *
441
     * @param int $productId
442
     * @return bool
443
     */
444
    public function hasVariants($productId)
445
    {
446
        $result = $this->manager->getConnection()->fetchColumn(
447
            'SELECT a.configurator_set_id FROM s_articles a WHERE a.id = ?',
448
            [(int) $productId]
449
        );
450
451
        return $result > 0;
452
    }
453
454
    /**
455
     * @param $detailId
456
     * @return PriceRange[]
457
     */
458
    protected function preparePriceRanges($detailId)
459
    {
460
        $prices = $this->getPriceRanges($detailId);
461
462
        $priceRanges = [];
463
        foreach ($prices as $price) {
464
            $clonePrice = $price;
465
466
            if ($price['to'] == 'beliebig') {
467
                $clonePrice['to'] = PriceRange::ANY;
468
            } else {
469
                $clonePrice['to'] = (int) $price['to'];
470
            }
471
472
            $priceRanges[] = new PriceRange($clonePrice);
473
        }
474
475
        return $priceRanges;
476
    }
477
478
    /**
479
     * @param int $detailId
480
     * @return array
481
     */
482
    private function getPriceRanges($detailId)
483
    {
484
        $exportPriceCustomerGroup = $this->configComponent->getConfig('priceGroupForPriceExport', 'EK');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $exportPriceCustomerGroup is correct as $this->configComponent->...pForPriceExport', 'EK') (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...
485
        $exportPriceColumn = $this->configComponent->getConfig('priceFieldForPriceExport');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $exportPriceColumn is correct as $this->configComponent->...ceFieldForPriceExport') (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...
486
487
        $columns = ['p.from', 'p.to', 'p.customerGroupKey'];
488
489
        if ($exportPriceColumn) {
490
            $columns[] = "p.{$exportPriceColumn} as price";
491
        }
492
493
        $builder = $this->manager->createQueryBuilder();
494
        $builder->select($columns)
495
            ->from('Shopware\Models\Article\Price', 'p')
496
            ->where('p.articleDetailsId = :detailId')
497
            ->andWhere('p.customerGroupKey = :groupKey')
498
            ->setParameter('detailId', $detailId)
499
            ->setParameter('groupKey', $exportPriceCustomerGroup);
500
501
        return $builder->getQuery()->getArrayResult();
502
    }
503
504
    /**
505
     * @param $articleId
506
     * @return Property[]
507
     */
508
    protected function prepareProperties($articleId)
509
    {
510
        $properties = $this->getProperties($articleId);
511
        $attrGroup = $this->attributeGroup($articleId);
512
513
        // if product property group exist then the
514
        // property values are still old by that
515
        // this will not generate wrong Connect changes
516
        $property = reset($properties);
517
        if ($attrGroup) {
518
            $groupName = $attrGroup->getName();
519
            $groupPosition = $attrGroup->getPosition();
520
        } else {
521
            $groupName = $property['groupName'];
522
            $groupPosition = $property['groupPosition'];
523
        }
524
525
        $propertyArray = [];
526
        foreach ($properties as $property) {
527
            $cloneProperty = $property;
528
            $cloneProperty['groupName'] = $groupName;
529
            $cloneProperty['groupPosition'] = $groupPosition;
530
            $propertyArray[] = new Property($cloneProperty);
531
        }
532
533
        return $propertyArray;
534
    }
535
536
    /**
537
     * @param $row
538
     * @return array
539
     */
540
    private function prepareVendor($row)
541
    {
542
        $row['vendor'] = [
543
            'name' => $row['vendorName'],
544
            'url' => $row['vendorLink'],
545
            'logo_url' => null,
546
            'description' => $row['vendorDescription'],
547
            'page_title' => $row['vendorMetaTitle'],
548
        ];
549
550
        if ($row['vendorImage']) {
551
            $info = pathinfo($row['vendorImage']);
552
            $row['vendor']['logo_url'] = $this->getImagePath($info['basename']);
553
        }
554
555
        unset($row['vendorName']);
556
        unset($row['vendorLink']);
557
        unset($row['vendorImage']);
558
        unset($row['vendorDescription']);
559
        unset($row['vendorMetaTitle']);
560
561
        return $row;
562
    }
563
564
    /**
565
     * Applies configurator options and groups
566
     * to article array
567
     *
568
     * @param array $row
569
     * @return array
570
     */
571
    private function applyConfiguratorOptions($row)
572
    {
573
        $builder = $this->manager->createQueryBuilder();
574
        $builder->from('Shopware\Models\Article\Detail', 'd');
575
        $builder->join('d.configuratorOptions', 'cor');
576
        $builder->join('cor.group', 'cg');
577
        $builder->select([
578
            'cor.name as optionName',
579
            'cor.id as optionId',
580
            'cg.name as groupName',
581
            'cg.id as groupId',
582
        ]);
583
        $builder->where('d.id = :detailId');
584
        $builder->setParameter(':detailId', $row['detailId']);
585
586
        $query = $builder->getQuery();
587
588
        $configuratorData = [];
589
        $configs = $query->getArrayResult();
590
591
        foreach ($configs as $config) {
592
            $row['translations'] = $this->productTranslator->translateConfiguratorGroup($config['groupId'], $config['groupName'], $row['translations']);
593
            $row['translations'] = $this->productTranslator->translateConfiguratorOption($config['optionId'], $config['optionName'], $row['translations']);
594
595
            $groupName = $config['groupName'];
596
            $configuratorData[$groupName] = $config['optionName'];
597
        }
598
599
        $row['variant'] = $configuratorData;
600
601
        foreach ($row['translations'] as $key => $translation) {
602
            try {
603
                // todo@sb: test me
604
                $this->productTranslator->validate($translation, count($configs));
605
            } catch (\Exception $e) {
606
                unset($row['translations'][$key]);
607
            }
608
        }
609
610
        return $row;
611
    }
612
}
613