WishlistController::addItemAction()   B
last analyzed

Complexity

Conditions 8
Paths 10

Size

Total Lines 51
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 51
rs 8.1954
c 0
b 0
f 0
cc 8
nc 10
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\WishlistPage\Controller;
11
12
use Generated\Shared\Transfer\WishlistResponseTransfer;
13
use Generated\Shared\Transfer\WishlistTransfer;
14
use SprykerShop\Yves\CustomerPage\Plugin\Router\CustomerPageRouteProviderPlugin;
15
use SprykerShop\Yves\WishlistPage\Controller\WishlistController as SprykerWishlistController;
16
use SprykerShop\Yves\WishlistPage\Plugin\Router\WishlistPageRouteProviderPlugin;
17
use Symfony\Component\HttpFoundation\RedirectResponse;
18
use Symfony\Component\HttpFoundation\Request;
19
20
/**
21
 * @method \SprykerShop\Yves\WishlistPage\WishlistPageFactory getFactory()
22
 */
23
class WishlistController extends SprykerWishlistController
24
{
25
    /**
26
     * @var string
27
     */
28
    public const REQUEST_HEADER_REFERER = 'referer';
29
30
    /**
31
     * @param \Symfony\Component\HttpFoundation\Request $request
32
     *
33
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
34
     */
35
    public function addItemAction(Request $request): RedirectResponse
36
    {
37
        $wishlistItemTransfer = $this->getWishlistItemTransferFromRequest($request);
38
        if (!$wishlistItemTransfer) {
39
            return $this->redirectResponseInternal(CustomerPageRouteProviderPlugin::ROUTE_NAME_LOGIN);
40
        }
41
42
        $wishlistAddItemForm = $this->getFactory()->getWishlistAddItemForm()->handleRequest($request);
43
44
        if (!$wishlistAddItemForm->isSubmitted() || !$wishlistAddItemForm->isValid()) {
45
            $this->addErrorMessage(static::MESSAGE_FORM_CSRF_VALIDATION_ERROR);
46
47
            return $this->redirectResponseInternal(WishlistPageRouteProviderPlugin::ROUTE_NAME_WISHLIST_DETAILS, [
48
                'wishlistName' => $wishlistItemTransfer->getWishlistName(),
49
            ]);
50
        }
51
52
        $wishlistResponseTransfer = new WishlistResponseTransfer();
53
        if ($wishlistItemTransfer->getWishlistName() === static::DEFAULT_NAME) {
54
            $wishlistResponseTransfer = $this->getFactory()->getWishlistClient()->validateAndCreateWishlist(
55
                (new WishlistTransfer())
56
                    ->setName(static::DEFAULT_NAME)
57
                    ->setFkCustomer($wishlistItemTransfer->getFkCustomer()),
58
            );
59
        }
60
61
        $wishlistItemTransfer = $this->getFactory()
62
            ->getWishlistClient()
63
            ->addItem($wishlistItemTransfer);
64
        if (!$wishlistItemTransfer->getIdWishlistItem()) {
65
            if ($wishlistResponseTransfer->getWishlist()) {
66
                $this->getFactory()->getWishlistClient()->removeWishlistByName($wishlistResponseTransfer->getWishlist());
67
            }
68
69
            $this->addErrorMessage('customer.account.wishlist.item.not_added');
70
71
            return $this->redirectResponseInternal(WishlistPageRouteProviderPlugin::ROUTE_NAME_WISHLIST_OVERVIEW, [
72
                'wishlistName' => $wishlistItemTransfer->getWishlistName(),
73
            ]);
74
        } else {
75
            $this->addSuccessMessage('cart.add.items.success');
76
        }
77
78
        if ($request->headers->has(static::REQUEST_HEADER_REFERER)) {
79
            return $this->redirectResponseExternal($request->headers->get(static::REQUEST_HEADER_REFERER));
80
        }
81
82
        return $this->redirectResponseInternal(
83
            WishlistPageRouteProviderPlugin::ROUTE_NAME_WISHLIST_DETAILS,
84
            [
85
                'wishlistName' => $wishlistItemTransfer->getWishlistName(),
86
            ],
87
        );
88
    }
89
}
90