Completed
Push — master ( ffc46f...f233a4 )
by
unknown
20s queued 13s
created

handleQuoteResponseTransferErrors()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 2
nc 2
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\OrderCustomReferenceWidget\Controller;
9
10
use Generated\Shared\Transfer\QuoteResponseTransfer;
11
use Spryker\Yves\Kernel\Controller\AbstractController;
12
use SprykerShop\Yves\OrderCustomReferenceWidget\Form\OrderCustomReferenceForm;
13
use Symfony\Component\HttpFoundation\RedirectResponse;
14
use Symfony\Component\HttpFoundation\Request;
15
16
/**
17
 * @method \SprykerShop\Yves\OrderCustomReferenceWidget\OrderCustomReferenceWidgetFactory getFactory()
18
 */
19
class OrderCustomReferenceController extends AbstractController
20
{
21
    protected const GLOSSARY_KEY_ORDER_CUSTOM_REFERENCE_SAVED = 'order_custom_reference.reference_saved';
22
23
    /**
24
     * @param \Symfony\Component\HttpFoundation\Request $request
25
     *
26
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
27
     */
28
    public function saveAction(Request $request): RedirectResponse
29
    {
30
        $redirectResponse = $this->executeSaveAction($request);
31
32
        return $redirectResponse;
33
    }
34
35
    /**
36
     * @param \Symfony\Component\HttpFoundation\Request $request
37
     *
38
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
39
     */
40
    protected function executeSaveAction(Request $request): RedirectResponse
41
    {
42
        $orderCustomReferenceForm = $this->getFactory()
43
            ->getOrderCustomReferenceForm()
44
            ->handleRequest($request);
45
        $quoteClient = $this->getFactory()->getQuoteClient();
46
47
        if ($orderCustomReferenceForm->isSubmitted() && $orderCustomReferenceForm->isValid()) {
48
            $quoteResponseTransfer = $this->getFactory()
49
                ->getOrderCustomReferenceClient()
50
                ->setOrderCustomReference(
51
                    $orderCustomReferenceForm->getData()[OrderCustomReferenceForm::FIELD_ORDER_CUSTOM_REFERENCE] ?? '',
52
                    $quoteClient->getQuote()
53
                );
54
55
            if ($quoteResponseTransfer->getIsSuccessful()) {
56
                $quoteClient->setQuote($quoteResponseTransfer->getQuoteTransfer());
57
                $this->addSuccessMessage(static::GLOSSARY_KEY_ORDER_CUSTOM_REFERENCE_SAVED);
58
            }
59
            $this->handleQuoteResponseTransferErrors($quoteResponseTransfer);
60
        }
61
62
        return $this->redirectResponseExternal(
63
            $orderCustomReferenceForm->getData()[OrderCustomReferenceForm::FIELD_BACK_URL]
64
        );
65
    }
66
67
    /**
68
     * @param \Generated\Shared\Transfer\QuoteResponseTransfer $quoteResponseTransfer
69
     *
70
     * @return void
71
     */
72
    protected function handleQuoteResponseTransferErrors(QuoteResponseTransfer $quoteResponseTransfer): void
73
    {
74
        foreach ($quoteResponseTransfer->getErrors() as $quoteErrorTransfer) {
75
            $this->addErrorMessage($quoteErrorTransfer->getMessage());
76
        }
77
    }
78
}
79