Passed
Push — master ( 611072...ac6e1a )
by Christian
23:13 queued 09:41
created

WishlistController::ajaxAdd()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Storefront\Controller;
4
5
use Shopware\Core\Checkout\Customer\Exception\CustomerWishlistNotFoundException;
6
use Shopware\Core\Checkout\Customer\SalesChannel\AbstractAddWishlistProductRoute;
7
use Shopware\Core\Checkout\Customer\SalesChannel\AbstractLoadWishlistRoute;
8
use Shopware\Core\Checkout\Customer\SalesChannel\AbstractRemoveWishlistProductRoute;
9
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
10
use Shopware\Core\Framework\Feature;
11
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
12
use Shopware\Core\Framework\Routing\Annotation\Since;
13
use Shopware\Core\Framework\Routing\Exception\MissingRequestParameterException;
14
use Shopware\Core\System\SalesChannel\SalesChannelContext;
15
use Shopware\Storefront\Page\Wishlist\WishlistPageLoader;
16
use Symfony\Component\HttpFoundation\JsonResponse;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\Response;
19
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
20
use Symfony\Component\Routing\Annotation\Route;
21
22
/**
23
 * @RouteScope(scopes={"storefront"})
24
 */
25
class WishlistController extends StorefrontController
26
{
27
    /**
28
     * @var WishlistPageLoader
29
     */
30
    private $wishlistPageLoader;
31
32
    /**
33
     * @var AbstractLoadWishlistRoute
34
     */
35
    private $wishlistLoadRoute;
36
37
    /**
38
     * @var AbstractAddWishlistProductRoute
39
     */
40
    private $addWishlistRoute;
41
42
    /**
43
     * @var AbstractRemoveWishlistProductRoute
44
     */
45
    private $removeWishlistProductRoute;
46
47
    public function __construct(
48
        WishlistPageLoader $wishlistPageLoader,
49
        AbstractLoadWishlistRoute $wishlistLoadRoute,
50
        AbstractAddWishlistProductRoute $addWishlistRoute,
51
        AbstractRemoveWishlistProductRoute $removeWishlistProductRoute
52
    ) {
53
        $this->wishlistPageLoader = $wishlistPageLoader;
54
        $this->wishlistLoadRoute = $wishlistLoadRoute;
55
        $this->addWishlistRoute = $addWishlistRoute;
56
        $this->removeWishlistProductRoute = $removeWishlistProductRoute;
57
    }
58
59
    /**
60
     * @Since("6.3.4.0")
61
     * @Route("/wishlist", name="frontend.wishlist.page", methods={"GET"})
62
     */
63
    public function index(Request $request, SalesChannelContext $context): Response
64
    {
65
        $this->denyAccessUnlessLoggedIn();
66
67
        $page = $this->wishlistPageLoader->load($request, $context);
68
69
        return $this->renderStorefront('@Storefront/storefront/page/wishlist/index.html.twig', ['page' => $page]);
70
    }
71
72
    /**
73
     * @Since("6.3.4.0")
74
     * @Route("/widgets/wishlist", name="widgets.wishlist.pagelet", methods={"GET", "POST"}, defaults={"XmlHttpRequest"=true})
75
     */
76
    public function ajaxPagination(Request $request, SalesChannelContext $context): Response
77
    {
78
        $this->denyAccessUnlessLoggedIn();
79
80
        $request->request->set('no-aggregations', true);
81
82
        $page = $this->wishlistPageLoader->load($request, $context);
83
84
        return $this->renderStorefront('@Storefront/storefront/page/wishlist/index.html.twig', ['page' => $page]);
85
    }
86
87
    /**
88
     * @Since("6.3.4.0")
89
     * @Route("/wishlist/list", name="frontend.wishlist.product.list", options={"seo"="false"}, methods={"GET"}, defaults={"XmlHttpRequest"=true})
90
     */
91
    public function ajaxList(Request $request, SalesChannelContext $context): Response
92
    {
93
        if (!Feature::isActive('FEATURE_NEXT_10549')) {
94
            throw new NotFoundHttpException();
95
        }
96
97
        $this->denyAccessUnlessLoggedIn();
98
99
        try {
100
            $res = $this->wishlistLoadRoute->load($request, $context, new Criteria());
101
        } catch (CustomerWishlistNotFoundException $exception) {
102
            return new JsonResponse([]);
103
        }
104
105
        return new JsonResponse($res->getProductListing()->getIds());
106
    }
107
108
    /**
109
     * @Since("6.3.4.0")
110
     * @Route("/wishlist/product/delete/{id}", name="frontend.wishlist.product.delete", methods={"POST", "DELETE"}, defaults={"XmlHttpRequest"=true})
111
     */
112
    public function remove(string $id, Request $request, SalesChannelContext $context): Response
113
    {
114
        $this->denyAccessUnlessLoggedIn();
115
116
        if (!$id) {
117
            throw new MissingRequestParameterException('Parameter id missing');
118
        }
119
120
        try {
121
            $this->removeWishlistProductRoute->delete($id, $context);
122
123
            $this->addFlash('success', $this->trans('wishlist.itemDeleteSuccess'));
124
        } catch (\Throwable $exception) {
125
            $this->addFlash('danger', $this->trans('error.message-default'));
126
        }
127
128
        return $this->createActionResponse($request);
129
    }
130
131
    /**
132
     * @Since("6.3.4.0")
133
     * @Route("/wishlist/add/{productId}", name="frontend.wishlist.product.add", options={"seo"="false"}, methods={"POST"}, defaults={"XmlHttpRequest"=true})
134
     */
135
    public function ajaxAdd(string $productId, SalesChannelContext $context): JsonResponse
136
    {
137
        if (!Feature::isActive('FEATURE_NEXT_10549')) {
138
            throw new NotFoundHttpException();
139
        }
140
141
        $this->denyAccessUnlessLoggedIn();
142
143
        $this->addWishlistRoute->add($productId, $context);
144
145
        return new JsonResponse([
146
            'success' => true,
147
        ]);
148
    }
149
150
    /**
151
     * @Since("6.3.4.0")
152
     * @Route("/wishlist/remove/{productId}", name="frontend.wishlist.product.remove", options={"seo"="false"}, methods={"POST"}, defaults={"XmlHttpRequest"=true})
153
     */
154
    public function ajaxRemove(string $productId, SalesChannelContext $context): JsonResponse
155
    {
156
        if (!Feature::isActive('FEATURE_NEXT_10549')) {
157
            throw new NotFoundHttpException();
158
        }
159
160
        $this->denyAccessUnlessLoggedIn();
161
162
        $this->removeWishlistProductRoute->delete($productId, $context);
163
164
        return new JsonResponse([
165
            'success' => true,
166
        ]);
167
    }
168
}
169