Issues (3641)

Client/GiftCard/CartCode/GiftCardCartCode.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\Client\GiftCard\CartCode;
9
10
use ArrayObject;
11
use Generated\Shared\Transfer\GiftCardTransfer;
12
use Generated\Shared\Transfer\MessageTransfer;
13
use Generated\Shared\Transfer\QuoteTransfer;
14
15
/**
16
 * @deprecated Will be removed in the next major version.
17
 */
18
class GiftCardCartCode implements GiftCardCartCodeInterface
19
{
20
    /**
21
     * @var string
22
     */
23
    public const CART_GIFT_CARD_APPLY_SUCCESSFUL = 'cart.giftcard.apply.successful';
24
25
    /**
26
     * @var string
27
     */
28
    public const CART_GIFT_CARD_APPLY_FAILED = 'cart.giftcard.apply.failed';
29
30
    /**
31
     * @var string
32
     */
33
    protected const MESSAGE_TYPE_SUCCESS = 'success';
34
35
    /**
36
     * @var string
37
     */
38
    protected const MESSAGE_TYPE_ERROR = 'error';
39
40
    /**
41
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
42
     * @param string $code
43
     *
44
     * @return \Generated\Shared\Transfer\QuoteTransfer
45
     */
46
    public function addCandidate(QuoteTransfer $quoteTransfer, $code): QuoteTransfer
47
    {
48
        if ($this->hasCandidate($quoteTransfer, $code)) {
49
            return $quoteTransfer;
50
        }
51
52
        $giftCard = new GiftCardTransfer();
53
        $giftCard->setCode($code);
54
55
        $quoteTransfer->addGiftCard($giftCard);
56
57
        return $quoteTransfer;
58
    }
59
60
    /**
61
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
62
     * @param string $code
63
     *
64
     * @return \Generated\Shared\Transfer\QuoteTransfer
65
     */
66
    public function removeCode(QuoteTransfer $quoteTransfer, $code): QuoteTransfer
67
    {
68
        $this->removeGiftCard($quoteTransfer, $code);
69
        $this->removeGiftCardPayment($quoteTransfer, $code);
70
71
        return $quoteTransfer;
72
    }
73
74
    /**
75
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
76
     * @param string $code
77
     *
78
     * @return \Generated\Shared\Transfer\MessageTransfer|null
79
     */
80
    public function getOperationResponseMessage(QuoteTransfer $quoteTransfer, $code): ?MessageTransfer
81
    {
82
        $giftCardApplySuccessMessageTransfer = $this->getGiftCardApplySuccessMessage($quoteTransfer, $code);
83
        if ($giftCardApplySuccessMessageTransfer) {
84
            return $giftCardApplySuccessMessageTransfer;
85
        }
86
87
        $giftCardApplyFailedMessageTransfer = $this->getGiftCardApplyFailedMessage($quoteTransfer, $code);
88
        if ($giftCardApplyFailedMessageTransfer) {
89
            return $giftCardApplyFailedMessageTransfer;
90
        }
91
92
        return null;
93
    }
94
95
    /**
96
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
97
     *
98
     * @return \Generated\Shared\Transfer\QuoteTransfer
99
     */
100
    public function clearAllCodes(QuoteTransfer $quoteTransfer): QuoteTransfer
101
    {
102
        $quoteTransfer->setGiftCards(new ArrayObject());
103
104
        $this->removeGiftCardPayment($quoteTransfer);
105
106
        return $quoteTransfer;
107
    }
108
109
    /**
110
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
111
     * @param string $code
112
     *
113
     * @return \Generated\Shared\Transfer\QuoteTransfer
114
     */
115
    protected function removeGiftCard(QuoteTransfer $quoteTransfer, $code): QuoteTransfer
116
    {
117
        $giftCardTransferCollection = $quoteTransfer->getGiftCards();
118
119
        foreach ($giftCardTransferCollection as $index => $giftCardTransfer) {
120
            if ($giftCardTransfer->getCode() === $code) {
121
                $giftCardTransferCollection->offsetUnset($index);
122
            }
123
        }
124
125
        return $quoteTransfer;
126
    }
127
128
    /**
129
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
130
     * @param string|null $code
131
     *
132
     * @return \Generated\Shared\Transfer\QuoteTransfer
133
     */
134
    protected function removeGiftCardPayment(QuoteTransfer $quoteTransfer, ?string $code = null): QuoteTransfer
135
    {
136
        foreach ($quoteTransfer->getPayments() as $index => $payment) {
137
            if ($payment->getGiftCard() && $code === null || $payment->getGiftCard()->getCode() === $code) {
0 ignored issues
show
Consider adding parentheses for clarity. Current Interpretation: ($payment->getGiftCard()...()->getCode() === $code, Probably Intended Meaning: $payment->getGiftCard() ...)->getCode() === $code)
Loading history...
138
                $quoteTransfer->getPayments()->offsetUnset($index);
139
            }
140
        }
141
142
        $quoteTransfer->setPayment(null);
143
144
        return $quoteTransfer;
145
    }
146
147
    /**
148
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
149
     * @param string $code
150
     *
151
     * @return bool
152
     */
153
    protected function hasCandidate(QuoteTransfer $quoteTransfer, string $code): bool
154
    {
155
        foreach ($quoteTransfer->getGiftCards() as $giftCard) {
156
            if ($giftCard->getCode() === $code) {
157
                return true;
158
            }
159
        }
160
161
        return false;
162
    }
163
164
    /**
165
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
166
     * @param string $code
167
     *
168
     * @return \Generated\Shared\Transfer\MessageTransfer|null
169
     */
170
    protected function getGiftCardApplySuccessMessage(QuoteTransfer $quoteTransfer, string $code): ?MessageTransfer
171
    {
172
        foreach ($quoteTransfer->getGiftCards() as $giftCard) {
173
            if ($giftCard->getCode() !== $code) {
174
                continue;
175
            }
176
177
            $messageTransfer = new MessageTransfer();
178
            $messageTransfer
179
                ->setValue(static::CART_GIFT_CARD_APPLY_SUCCESSFUL)
180
                ->setType(static::MESSAGE_TYPE_SUCCESS);
181
182
            return $messageTransfer;
183
        }
184
185
        return null;
186
    }
187
188
    /**
189
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
190
     * @param string $code
191
     *
192
     * @return \Generated\Shared\Transfer\MessageTransfer|null
193
     */
194
    protected function getGiftCardApplyFailedMessage(QuoteTransfer $quoteTransfer, string $code): ?MessageTransfer
195
    {
196
        foreach ($quoteTransfer->getNotApplicableGiftCardCodes() as $giftCardCode) {
197
            if ($giftCardCode !== $code) {
198
                continue;
199
            }
200
201
            $messageTransfer = new MessageTransfer();
202
            $messageTransfer->setValue(static::CART_GIFT_CARD_APPLY_FAILED);
203
            $messageTransfer->setType(static::MESSAGE_TYPE_ERROR);
204
205
            return $messageTransfer;
206
        }
207
208
        return null;
209
    }
210
}
211