Issues (3641)

MerchantRelationshipPriceProductFilterPlugin.php (1 issue)

1
<?php
2
3
/**
4
 * Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace Spryker\Service\PriceProductMerchantRelationship\Plugin\PriceProductExtension;
9
10
use Generated\Shared\Transfer\PriceProductFilterTransfer;
11
use Generated\Shared\Transfer\PriceProductTransfer;
12
use Spryker\Service\Kernel\AbstractPlugin;
13
use Spryker\Service\PriceProductExtension\Dependency\Plugin\PriceProductFilterPluginInterface;
14
use Spryker\Shared\PriceProduct\PriceProductConfig;
15
use Spryker\Shared\PriceProductMerchantRelationship\PriceProductMerchantRelationshipConfig;
16
17
/**
18
 * @deprecated Will be removed without replacement. {@link \Spryker\Service\PriceProduct\FilterStrategy\SinglePriceProductFilterMinStrategy} already implements the same logic.
19
 *
20
 * @method \Spryker\Service\PriceProductMerchantRelationship\PriceProductMerchantRelationshipConfig getConfig()
21
 */
22
class MerchantRelationshipPriceProductFilterPlugin extends AbstractPlugin implements PriceProductFilterPluginInterface
23
{
24
    /**
25
     * {@inheritDoc}
26
     *
27
     * @api
28
     *
29
     * @param array<\Generated\Shared\Transfer\PriceProductTransfer> $priceProductTransfers
30
     * @param \Generated\Shared\Transfer\PriceProductFilterTransfer $priceProductFilterTransfer
31
     *
32
     * @return array<\Generated\Shared\Transfer\PriceProductTransfer>
33
     */
34
    public function filter(array $priceProductTransfers, PriceProductFilterTransfer $priceProductFilterTransfer): array
35
    {
36
        $priceProductFilterTransfer->requirePriceMode();
37
38
        $resultPriceProductTransfers = [];
39
        $minPriceProductTransfer = null;
40
41
        foreach ($priceProductTransfers as $priceProductTransfer) {
42
            $priceProductTransfer->requirePriceDimension();
43
44
            if (!$priceProductTransfer->getPriceDimension()->getIdMerchantRelationship()) {
45
                $resultPriceProductTransfers[] = $priceProductTransfer;
46
            }
47
48
            if ($minPriceProductTransfer === null || !$this->hasPriceByPriceMode($minPriceProductTransfer, $priceProductFilterTransfer->getPriceMode())) {
0 ignored issues
show
$minPriceProductTransfer of type null is incompatible with the type Generated\Shared\Transfer\PriceProductTransfer expected by parameter $priceProductTransfer of Spryker\Service\PricePro...::hasPriceByPriceMode(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

48
            if ($minPriceProductTransfer === null || !$this->hasPriceByPriceMode(/** @scrutinizer ignore-type */ $minPriceProductTransfer, $priceProductFilterTransfer->getPriceMode())) {
Loading history...
49
                $minPriceProductTransfer = $priceProductTransfer;
50
51
                continue;
52
            }
53
54
            if (!$this->hasPriceByPriceMode($priceProductTransfer, $priceProductFilterTransfer->getPriceMode())) {
55
                continue;
56
            }
57
58
            if ($priceProductFilterTransfer->getPriceMode() === PriceProductConfig::PRICE_GROSS_MODE) {
59
                if ($minPriceProductTransfer->getMoneyValue()->getGrossAmount() > $priceProductTransfer->getMoneyValue()->getGrossAmount()) {
60
                    $minPriceProductTransfer = $priceProductTransfer;
61
                }
62
63
                continue;
64
            }
65
66
            if ($minPriceProductTransfer->getMoneyValue()->getNetAmount() > $priceProductTransfer->getMoneyValue()->getNetAmount()) {
67
                $minPriceProductTransfer = $priceProductTransfer;
68
            }
69
        }
70
71
        foreach ($priceProductTransfers as $priceProductTransfer) {
72
            if ($minPriceProductTransfer->getPriceDimension()->getIdMerchantRelationship() === $priceProductTransfer->getPriceDimension()->getIdMerchantRelationship()) {
73
                $resultPriceProductTransfers[] = $priceProductTransfer;
74
            }
75
        }
76
77
        return $resultPriceProductTransfers;
78
    }
79
80
    /**
81
     * {@inheritDoc}
82
     *
83
     * @api
84
     *
85
     * @return string
86
     */
87
    public function getDimensionName(): string
88
    {
89
        return PriceProductMerchantRelationshipConfig::PRICE_DIMENSION_MERCHANT_RELATIONSHIP;
90
    }
91
92
    /**
93
     * @param \Generated\Shared\Transfer\PriceProductTransfer $priceProductTransfer
94
     * @param string $priceMode
95
     *
96
     * @return bool
97
     */
98
    protected function hasPriceByPriceMode(PriceProductTransfer $priceProductTransfer, string $priceMode): bool
99
    {
100
        return ($priceMode === PriceProductConfig::PRICE_GROSS_MODE && $priceProductTransfer->getMoneyValue()->getGrossAmount() !== null) ||
101
            ($priceMode !== PriceProductConfig::PRICE_GROSS_MODE && $priceProductTransfer->getMoneyValue()->getNetAmount() !== null);
102
    }
103
}
104