Issues (3641)

Business/Shipment/ShipmentMethodFilter.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\Zed\GiftCard\Business\Shipment;
9
10
use ArrayObject;
11
use Generated\Shared\Transfer\ItemTransfer;
12
use Generated\Shared\Transfer\QuoteTransfer;
13
use Spryker\Zed\GiftCard\GiftCardConfig;
14
15
/**
16
 * @deprecated Use {@link \Spryker\Zed\GiftCard\Business\ShipmentGroup\ShipmentGroupMethodFilter} instead.
17
 */
18
class ShipmentMethodFilter implements ShipmentMethodFilterInterface
19
{
20
    /**
21
     * @deprecated Use {@link \Spryker\Zed\GiftCard\GiftCardConfig::getGiftCardOnlyShipmentMethods()} instead.
22
     *
23
     * @var string
24
     */
25
    public const NO_SHIPMENT_METHOD = 'No shipment';
26
27
    /**
28
     * @var \Spryker\Zed\GiftCard\GiftCardConfig
29
     */
30
    protected $giftCardConfig;
31
32
    /**
33
     * @param \Spryker\Zed\GiftCard\GiftCardConfig $giftCardConfig
34
     */
35
    public function __construct(GiftCardConfig $giftCardConfig)
36
    {
37
        $this->giftCardConfig = $giftCardConfig;
38
    }
39
40
    /**
41
     * @param \ArrayObject<int, \Generated\Shared\Transfer\ShipmentMethodTransfer> $shipmentMethods
42
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
43
     *
44
     * @return \ArrayObject<int, \Generated\Shared\Transfer\ShipmentMethodTransfer>
45
     */
46
    public function filterShipmentMethods(ArrayObject $shipmentMethods, QuoteTransfer $quoteTransfer)
47
    {
48
        if ($this->containsOnlyGiftCardItems($quoteTransfer)) {
49
            return $this->allowGiftCardOnlyShipmentMethods($shipmentMethods);
50
        }
51
52
        return $this->disallowGiftCardOnlyShipmentMethods($shipmentMethods);
53
    }
54
55
    /**
56
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
57
     *
58
     * @return bool
59
     */
60
    protected function containsOnlyGiftCardItems(QuoteTransfer $quoteTransfer)
61
    {
62
        foreach ($quoteTransfer->getItems() as $itemTransfer) {
63
            if (!$this->isGiftCard($itemTransfer)) {
64
                return false;
65
            }
66
        }
67
68
        return true;
69
    }
70
71
    /**
72
     * @param \Generated\Shared\Transfer\ItemTransfer $itemTransfer
73
     *
74
     * @return bool
75
     */
76
    protected function isGiftCard(ItemTransfer $itemTransfer)
77
    {
78
        $metadata = $itemTransfer->getGiftCardMetadata();
79
80
        if (!$metadata) {
81
            return false;
82
        }
83
84
        return $metadata->getIsGiftCard();
85
    }
86
87
    /**
88
     * @param \ArrayObject<int, \Generated\Shared\Transfer\ShipmentMethodTransfer> $shipmentMethods $shipmentMethods
89
     *
90
     * @return \ArrayObject<int, \Generated\Shared\Transfer\ShipmentMethodTransfer>
91
     */
92
    protected function allowGiftCardOnlyShipmentMethods(ArrayObject $shipmentMethods): ArrayObject
93
    {
94
        /** @var \ArrayObject<int, \Generated\Shared\Transfer\ShipmentMethodTransfer> $result */
95
        $result = new ArrayObject();
96
        $giftCardOnlyShipmentMethods = $this->getGiftCardOnlyShipmentMethods();
97
        foreach ($shipmentMethods as $shipmentMethod) {
98
            if (in_array($shipmentMethod->getName(), $giftCardOnlyShipmentMethods)) {
99
                $result[] = $shipmentMethod;
100
            }
101
        }
102
103
        return $result;
104
    }
105
106
    /**
107
     * @param \ArrayObject<int, \Generated\Shared\Transfer\ShipmentMethodTransfer> $shipmentMethods $shipmentMethods
108
     *
109
     * @return \ArrayObject<int, \Generated\Shared\Transfer\ShipmentMethodTransfer>
110
     */
111
    protected function disallowGiftCardOnlyShipmentMethods(ArrayObject $shipmentMethods): ArrayObject
112
    {
113
        /** @var \ArrayObject<int, \Generated\Shared\Transfer\ShipmentMethodTransfer> $result */
114
        $result = new ArrayObject();
115
        $giftCardOnlyShipmentMethods = $this->getGiftCardOnlyShipmentMethods();
116
        foreach ($shipmentMethods as $shipmentMethod) {
117
            if (!in_array($shipmentMethod->getName(), $giftCardOnlyShipmentMethods)) {
118
                $result[] = $shipmentMethod;
119
            }
120
        }
121
122
        return $result;
123
    }
124
125
    /**
126
     * @deprecated Added for BC reasons, will be removed in next major release. Use GiftCardConfig::getGiftCardOnlyShipmentMethods() instead.
127
     *
128
     * @return array
129
     */
130
    protected function getGiftCardOnlyShipmentMethods(): array
131
    {
132
        $giftCardOnlyShipmentMethods = $this->giftCardConfig->getGiftCardOnlyShipmentMethods();
133
134
        if ($giftCardOnlyShipmentMethods) {
0 ignored issues
show
$giftCardOnlyShipmentMethods is an empty array, thus is always false.
Loading history...
135
            return $giftCardOnlyShipmentMethods;
136
        }
137
138
        return [static::NO_SHIPMENT_METHOD];
139
    }
140
}
141