CreateController::getRefererUrl()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
/**
4
 * This file is part of the Spryker Commerce OS.
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
declare(strict_types = 1);
9
10
namespace Pyz\Yves\ProductReviewWidget\Controller;
11
12
use SprykerShop\Yves\ProductReviewWidget\Controller\CreateController as SprykerCreateController;
13
use Symfony\Component\HttpFoundation\RedirectResponse;
14
use Symfony\Component\HttpFoundation\Request;
15
16
/**
17
 * @method \Pyz\Yves\ProductReviewWidget\ProductReviewWidgetFactory getFactory()
18
 */
19
class CreateController extends SprykerCreateController
20
{
21
    /**
22
     * @param \Symfony\Component\HttpFoundation\Request $request
23
     *
24
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
25
     */
26
    public function indexAction(Request $request): RedirectResponse
27
    {
28
        $this->executeIndexAction($request);
29
30
        return $this->redirectResponseExternal($this->getRefererUrl($request));
31
    }
32
33
    /**
34
     * @param \Symfony\Component\HttpFoundation\Request $request
35
     *
36
     * @return string
37
     */
38
    protected function getRefererUrl(Request $request): string
39
    {
40
        if ($request->headers->has(static::REQUEST_HEADER_REFERER)) {
41
            return $request->headers->get(static::REQUEST_HEADER_REFERER);
42
        }
43
44
        return static::URL_MAIN;
45
    }
46
47
    /**
48
     * @param \Symfony\Component\HttpFoundation\Request $request
49
     *
50
     * @return void
51
     */
52
    protected function executeIndexAction(Request $request): void
53
    {
54
        $idProductAbstract = $request->attributes->get('idProductAbstract');
55
        $productReviewForm = $this->getFactory()
56
            ->createProductReviewForm($idProductAbstract)
57
            ->handleRequest($request);
58
59
        if (!$productReviewForm->isSubmitted()) {
60
            return;
61
        }
62
63
        $customerTransfer = $this->getFactory()->getCustomerClient()->getCustomer();
64
65
        if ($customerTransfer === null) {
66
            $this->addErrorMessage(static::GLOSSARY_KEY_ERROR_NO_CUSTOMER);
67
68
            return;
69
        }
70
71
        if (!$productReviewForm->isValid()) {
72
            foreach ($productReviewForm->getErrors(true) as $error) {
73
                $this->addErrorMessage($error->getMessage());
0 ignored issues
show
Bug introduced by
The method getMessage() does not exist on Symfony\Component\Form\FormErrorIterator. ( Ignorable by Annotation )

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

73
                $this->addErrorMessage($error->/** @scrutinizer ignore-call */ getMessage());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
74
            }
75
76
            return;
77
        }
78
79
        $productReviewRequestTransfer = $productReviewForm->getData()
80
            ->setCustomerReference($customerTransfer->getCustomerReference())
81
            ->setLocaleName($this->getLocale());
82
83
        $productReviewResponseTransfer = $this->getFactory()
84
            ->getProductReviewClient()
85
            ->submitCustomerReview($productReviewRequestTransfer);
86
87
        if ($productReviewResponseTransfer->getIsSuccess() === false) {
88
            $this->addErrorMessage($productReviewResponseTransfer->getErrors()[0]->getMessage());
89
90
            return;
91
        }
92
93
        $this->addSuccessMessage(static::GLOSSARY_KEY_SUCCESS_PRODUCT_REVIEW_SUBMITTED);
94
    }
95
}
96