Passed
Pull Request — master (#365)
by Dmitry
21:01
created

CreateController::executePyzIndexAction()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 42
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 42
c 0
b 0
f 0
rs 8.9137
cc 6
nc 6
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
namespace Pyz\Yves\ProductReviewWidget\Controller;
9
10
use SprykerShop\Yves\ProductReviewWidget\Controller\CreateController as SprykerCreateController;
11
use Symfony\Component\HttpFoundation\RedirectResponse;
12
use Symfony\Component\HttpFoundation\Request;
13
14
/**
15
 * @method \Pyz\Yves\ProductReviewWidget\ProductReviewWidgetFactory getFactory()
16
 */
17
class CreateController extends SprykerCreateController
18
{
19
    /**
20
     * @param \Symfony\Component\HttpFoundation\Request $request
21
     *
22
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
23
     */
24
    public function indexAction(Request $request): RedirectResponse
25
    {
26
        $this->executeIndexAction($request);
27
28
        return $this->redirectResponseExternal($this->getRefererUrl($request));
29
    }
30
31
    /**
32
     * @param \Symfony\Component\HttpFoundation\Request $request
33
     *
34
     * @return string
35
     */
36
    protected function getRefererUrl(Request $request): string
37
    {
38
        if ($request->headers->has(static::REQUEST_HEADER_REFERER)) {
39
            return $request->headers->get(static::REQUEST_HEADER_REFERER);
40
        }
41
42
        return static::URL_MAIN;
43
    }
44
45
    /**
46
     * @param \Symfony\Component\HttpFoundation\Request $request
47
     *
48
     * @return void
49
     */
50
    protected function executeIndexAction(Request $request): void
51
    {
52
        $idProductAbstract = $request->attributes->get('idProductAbstract');
53
        $productReviewForm = $this->getFactory()
54
            ->createProductReviewForm($idProductAbstract)
55
            ->handleRequest($request);
56
57
        if (!$productReviewForm->isSubmitted()) {
58
            return;
59
        }
60
61
        $customerTransfer = $this->getFactory()->getCustomerClient()->getCustomer();
62
63
        if ($customerTransfer === null) {
64
            $this->addErrorMessage(static::GLOSSARY_KEY_ERROR_NO_CUSTOMER);
65
66
            return;
67
        }
68
69
        if (!$productReviewForm->isValid()) {
70
            foreach ($productReviewForm->getErrors(true) as $error) {
71
                $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

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