Completed
Push — master ( 24cb02...ca1164 )
by
unknown
17s queued 11s
created

CodeController::isSuccessMessageExists()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 9
rs 10
cc 3
nc 3
nop 1
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 SprykerShop\Yves\CartCodeWidget\Controller;
9
10
use Generated\Shared\Transfer\CartCodeRequestTransfer;
11
use Generated\Shared\Transfer\CartCodeResponseTransfer;
12
use Generated\Shared\Transfer\MessageTransfer;
13
use Generated\Shared\Transfer\QuoteTransfer;
14
use SprykerShop\Yves\CartCodeWidget\Form\CartCodeForm;
15
use SprykerShop\Yves\ShopApplication\Controller\AbstractController;
16
use Symfony\Component\HttpFoundation\RedirectResponse;
17
use Symfony\Component\HttpFoundation\Request;
18
19
/**
20
 * @method \SprykerShop\Yves\CartCodeWidget\CartCodeWidgetFactory getFactory()
21
 */
22
class CodeController extends AbstractController
23
{
24
    public const PARAM_CODE = 'code';
25
26
    protected const MESSAGE_TYPE_SUCCESS = 'success';
27
    protected const MESSAGE_TYPE_ERROR = 'error';
28
29
    protected const GLOSSARY_KEY_CODE_APPLY_FAILED = 'cart.code.apply.failed';
30
31
    /**
32
     * @param \Symfony\Component\HttpFoundation\Request $request
33
     *
34
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
35
     */
36
    public function addAction(Request $request)
37
    {
38
        $form = $this->getFactory()->getCartCodeForm()->handleRequest($request);
39
        if (!$form->isSubmitted() || !$form->isValid()) {
40
            return $this->redirectResponseExternal($request->headers->get('referer'));
41
        }
42
43
        $code = (string)$form->get(CartCodeForm::FIELD_CODE)->getData();
44
45
        $quoteTransfer = $this->getFactory()
46
            ->getQuoteClient()
47
            ->getQuote();
48
49
        $cartCodeResponseTransfer = $this->getFactory()
50
            ->getCartCodeClient()
51
            ->addCartCode($this->createCartCodeRequestTransfer($quoteTransfer, $code));
52
53
        $this->processErrorResponseMessage($cartCodeResponseTransfer);
54
55
        return $this->redirectResponse($cartCodeResponseTransfer, $request);
56
    }
57
58
    /**
59
     * @param \Symfony\Component\HttpFoundation\Request $request
60
     *
61
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
62
     */
63
    public function removeAction(Request $request)
64
    {
65
        $code = (string)$request->query->get(static::PARAM_CODE);
66
        if (empty($code)) {
67
            return $this->redirectResponseExternal($request->headers->get('referer'));
68
        }
69
70
        $quoteTransfer = $this->getFactory()->getQuoteClient()->getQuote();
71
        $cartCodeResponseTransfer = $this->getFactory()
72
            ->getCartCodeClient()
73
            ->removeCartCode($this->createCartCodeRequestTransfer($quoteTransfer, $code));
74
75
        return $this->redirectResponse($cartCodeResponseTransfer, $request);
76
    }
77
78
    /**
79
     * @param \Symfony\Component\HttpFoundation\Request $request
80
     *
81
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
82
     */
83
    public function clearAction(Request $request)
84
    {
85
        $quoteTransfer = $this->getFactory()->getQuoteClient()->getQuote();
86
87
        $cartCodeResponseTransfer = $this->getFactory()
88
            ->getCartCodeClient()
89
            ->clearCartCodes($this->createCartCodeRequestTransfer($quoteTransfer));
90
91
        return $this->redirectResponse($cartCodeResponseTransfer, $request);
92
    }
93
94
    /**
95
     * @param \Generated\Shared\Transfer\CartCodeResponseTransfer $cartCodeResponseTransfer
96
     * @param \Symfony\Component\HttpFoundation\Request $request
97
     *
98
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
99
     */
100
    protected function redirectResponse(
101
        CartCodeResponseTransfer $cartCodeResponseTransfer,
102
        Request $request
103
    ): RedirectResponse {
104
        $this->getFactory()->getQuoteClient()->setQuote($cartCodeResponseTransfer->getQuote());
105
        $this->getFactory()->getZedRequestClient()->addFlashMessagesFromLastZedRequest();
106
107
        foreach ($cartCodeResponseTransfer->getMessages() as $messageTransfer) {
108
            $this->handleMessage($messageTransfer);
109
        }
110
111
        return $this->redirectResponseExternal($request->headers->get('referer'));
112
    }
113
114
    /**
115
     * @param \Generated\Shared\Transfer\CartCodeResponseTransfer $cartCodeResponseTransfer
116
     *
117
     * @return void
118
     */
119
    protected function processErrorResponseMessage(CartCodeResponseTransfer $cartCodeResponseTransfer): void
120
    {
121
        if ($this->isSuccessMessageExists($cartCodeResponseTransfer)) {
122
            return;
123
        }
124
125
        $this->addErrorMessage(static::GLOSSARY_KEY_CODE_APPLY_FAILED);
126
    }
127
128
    /**
129
     * @param \Generated\Shared\Transfer\CartCodeResponseTransfer $cartCodeResponseTransfer
130
     *
131
     * @return bool
132
     */
133
    protected function isSuccessMessageExists(CartCodeResponseTransfer $cartCodeResponseTransfer): bool
134
    {
135
        foreach ($cartCodeResponseTransfer->getMessages() as $messageTransfer) {
136
            if ($messageTransfer->getType() === static::MESSAGE_TYPE_SUCCESS) {
137
                return true;
138
            }
139
        }
140
141
        return false;
142
    }
143
144
    /**
145
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
146
     * @param string|null $code
147
     *
148
     * @return \Generated\Shared\Transfer\CartCodeRequestTransfer
149
     */
150
    protected function createCartCodeRequestTransfer(
151
        QuoteTransfer $quoteTransfer,
152
        ?string $code = null
153
    ): CartCodeRequestTransfer {
154
        return (new CartCodeRequestTransfer())
155
            ->setQuote($quoteTransfer)
156
            ->setCartCode($code);
157
    }
158
159
    /**
160
     * @param \Generated\Shared\Transfer\MessageTransfer $messageTransfer
161
     *
162
     * @return void
163
     */
164
    protected function handleMessage(MessageTransfer $messageTransfer): void
165
    {
166
        if ($messageTransfer->getType() === static::MESSAGE_TYPE_ERROR) {
167
            return;
168
        }
169
170
        switch ($messageTransfer->getType()) {
171
            case self::MESSAGE_TYPE_SUCCESS:
172
                $this->addSuccessMessage($messageTransfer->getValue());
173
                break;
174
            default:
175
                $this->addInfoMessage($messageTransfer->getValue());
176
        }
177
    }
178
}
179