Passed
Push — master ( 46a541...ba1118 )
by Christian
13:16 queued 10s
created

WishlistController::ajaxMerge()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 4
nop 3
dl 0
loc 17
rs 9.9666
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\Exception\DuplicateWishlistProductException;
7
use Shopware\Core\Checkout\Customer\SalesChannel\AbstractAddWishlistProductRoute;
8
use Shopware\Core\Checkout\Customer\SalesChannel\AbstractLoadWishlistRoute;
9
use Shopware\Core\Checkout\Customer\SalesChannel\AbstractMergeWishlistProductRoute;
10
use Shopware\Core\Checkout\Customer\SalesChannel\AbstractRemoveWishlistProductRoute;
11
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
12
use Shopware\Core\Framework\Feature;
13
use Shopware\Core\Framework\Routing\Annotation\LoginRequired;
14
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
15
use Shopware\Core\Framework\Routing\Annotation\Since;
16
use Shopware\Core\Framework\Routing\Exception\MissingRequestParameterException;
17
use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
18
use Shopware\Core\System\SalesChannel\SalesChannelContext;
19
use Shopware\Storefront\Page\Wishlist\WishlistPageLoader;
20
use Symfony\Component\HttpFoundation\JsonResponse;
21
use Symfony\Component\HttpFoundation\Request;
22
use Symfony\Component\HttpFoundation\Response;
23
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
24
use Symfony\Component\Routing\Annotation\Route;
25
26
/**
27
 * @RouteScope(scopes={"storefront"})
28
 */
29
class WishlistController extends StorefrontController
30
{
31
    /**
32
     * @var WishlistPageLoader
33
     */
34
    private $wishlistPageLoader;
35
36
    /**
37
     * @var AbstractLoadWishlistRoute
38
     */
39
    private $wishlistLoadRoute;
40
41
    /**
42
     * @var AbstractAddWishlistProductRoute
43
     */
44
    private $addWishlistRoute;
45
46
    /**
47
     * @var AbstractRemoveWishlistProductRoute
48
     */
49
    private $removeWishlistProductRoute;
50
51
    /**
52
     * @var AbstractMergeWishlistProductRoute
53
     */
54
    private $mergeWishlistProductRoute;
55
56
    public function __construct(
57
        WishlistPageLoader $wishlistPageLoader,
58
        AbstractLoadWishlistRoute $wishlistLoadRoute,
59
        AbstractAddWishlistProductRoute $addWishlistRoute,
60
        AbstractRemoveWishlistProductRoute $removeWishlistProductRoute,
61
        AbstractMergeWishlistProductRoute $mergeWishlistProductRoute
62
    ) {
63
        $this->wishlistPageLoader = $wishlistPageLoader;
64
        $this->wishlistLoadRoute = $wishlistLoadRoute;
65
        $this->addWishlistRoute = $addWishlistRoute;
66
        $this->removeWishlistProductRoute = $removeWishlistProductRoute;
67
        $this->mergeWishlistProductRoute = $mergeWishlistProductRoute;
68
    }
69
70
    /**
71
     * @Since("6.3.4.0")
72
     * @LoginRequired()
73
     * @Route("/wishlist", name="frontend.wishlist.page", methods={"GET"})
74
     */
75
    public function index(Request $request, SalesChannelContext $context): Response
76
    {
77
        $page = $this->wishlistPageLoader->load($request, $context);
78
79
        return $this->renderStorefront('@Storefront/storefront/page/wishlist/index.html.twig', ['page' => $page]);
80
    }
81
82
    /**
83
     * @Since("6.3.4.0")
84
     * @LoginRequired()
85
     * @Route("/widgets/wishlist", name="widgets.wishlist.pagelet", methods={"GET", "POST"}, defaults={"XmlHttpRequest"=true})
86
     */
87
    public function ajaxPagination(Request $request, SalesChannelContext $context): Response
88
    {
89
        $request->request->set('no-aggregations', true);
90
91
        $page = $this->wishlistPageLoader->load($request, $context);
92
93
        return $this->renderStorefront('@Storefront/storefront/page/wishlist/index.html.twig', ['page' => $page]);
94
    }
95
96
    /**
97
     * @Since("6.3.4.0")
98
     * @Route("/wishlist/list", name="frontend.wishlist.product.list", options={"seo"="false"}, methods={"GET"}, defaults={"XmlHttpRequest"=true})
99
     */
100
    public function ajaxList(Request $request, SalesChannelContext $context): Response
101
    {
102
        if (!Feature::isActive('FEATURE_NEXT_10549')) {
103
            throw new NotFoundHttpException();
104
        }
105
106
        try {
107
            $res = $this->wishlistLoadRoute->load($request, $context, new Criteria());
108
        } catch (CustomerWishlistNotFoundException $exception) {
109
            return new JsonResponse([]);
110
        }
111
112
        return new JsonResponse($res->getProductListing()->getIds());
113
    }
114
115
    /**
116
     * @Since("6.3.4.0")
117
     * @LoginRequired()
118
     * @Route("/wishlist/product/delete/{id}", name="frontend.wishlist.product.delete", methods={"POST", "DELETE"}, defaults={"XmlHttpRequest"=true})
119
     */
120
    public function remove(string $id, Request $request, SalesChannelContext $context): Response
121
    {
122
        if (!$id) {
123
            throw new MissingRequestParameterException('Parameter id missing');
124
        }
125
126
        try {
127
            $this->removeWishlistProductRoute->delete($id, $context);
128
129
            $this->addFlash('success', $this->trans('wishlist.itemDeleteSuccess'));
130
        } catch (\Throwable $exception) {
131
            $this->addFlash('danger', $this->trans('error.message-default'));
132
        }
133
134
        return $this->createActionResponse($request);
135
    }
136
137
    /**
138
     * @Since("6.3.4.0")
139
     * @LoginRequired()
140
     * @Route("/wishlist/add/{productId}", name="frontend.wishlist.product.add", options={"seo"="false"}, methods={"POST"}, defaults={"XmlHttpRequest"=true})
141
     */
142
    public function ajaxAdd(string $productId, SalesChannelContext $context): JsonResponse
143
    {
144
        if (!Feature::isActive('FEATURE_NEXT_10549')) {
145
            throw new NotFoundHttpException();
146
        }
147
148
        $this->addWishlistRoute->add($productId, $context);
149
150
        return new JsonResponse([
151
            'success' => true,
152
        ]);
153
    }
154
155
    /**
156
     * @Since("6.3.4.0")
157
     * @LoginRequired()
158
     * @Route("/wishlist/remove/{productId}", name="frontend.wishlist.product.remove", options={"seo"="false"}, methods={"POST"}, defaults={"XmlHttpRequest"=true})
159
     */
160
    public function ajaxRemove(string $productId, SalesChannelContext $context): JsonResponse
161
    {
162
        if (!Feature::isActive('FEATURE_NEXT_10549')) {
163
            throw new NotFoundHttpException();
164
        }
165
166
        $this->removeWishlistProductRoute->delete($productId, $context);
167
168
        return new JsonResponse([
169
            'success' => true,
170
        ]);
171
    }
172
173
    /**
174
     * @Since("6.3.4.0")
175
     * @LoginRequired()
176
     * @Route("/wishlist/add-after-login/{productId}", name="frontend.wishlist.add.after.login", options={"seo"="false"}, methods={"GET"})
177
     */
178
    public function addAfterLogin(string $productId, SalesChannelContext $context): Response
179
    {
180
        if (!Feature::isActive('FEATURE_NEXT_10549')) {
181
            throw new NotFoundHttpException();
182
        }
183
184
        try {
185
            $this->addWishlistRoute->add($productId, $context);
186
187
            $this->addFlash('success', $this->trans('wishlist.itemAddedSuccess'));
188
        } catch (DuplicateWishlistProductException $exception) {
189
            $this->addFlash('warning', $exception->getMessage());
190
        } catch (\Throwable $exception) {
191
            $this->addFlash('danger', $this->trans('error.message-default'));
192
        }
193
194
        return $this->redirectToRoute('frontend.home.page');
195
    }
196
197
    /**
198
     * @Since("6.3.4.0")
199
     * @LoginRequired()
200
     * @Route("/wishlist/merge", name="frontend.wishlist.product.merge", options={"seo"="false"}, methods={"POST"}, defaults={"XmlHttpRequest"=true})
201
     */
202
    public function ajaxMerge(RequestDataBag $requestDataBag, Request $request, SalesChannelContext $context): Response
203
    {
204
        if (!Feature::isActive('FEATURE_NEXT_10549')) {
205
            throw new NotFoundHttpException();
206
        }
207
208
        try {
209
            $this->mergeWishlistProductRoute->merge($requestDataBag, $context);
210
211
            return $this->renderStorefront('@Storefront/storefront/utilities/alert.html.twig', [
212
                'type' => 'info', 'content' => $this->trans('wishlist.wishlistMergeHint'),
213
            ]);
214
        } catch (\Throwable $exception) {
215
            $this->addFlash('danger', $this->trans('error.message-default'));
216
        }
217
218
        return $this->createActionResponse($request);
219
    }
220
221
    /**
222
     * @Since("6.3.4.0")
223
     * @LoginRequired()
224
     * @Route("/wishlist/merge/pagelet", name="frontend.wishlist.product.merge.pagelet", methods={"GET", "POST"}, defaults={"XmlHttpRequest"=true})
225
     */
226
    public function ajaxPagelet(Request $request, SalesChannelContext $context): Response
227
    {
228
        $request->request->set('no-aggregations', true);
229
230
        $page = $this->wishlistPageLoader->load($request, $context);
231
232
        return $this->renderStorefront('@Storefront/storefront/page/wishlist/wishlist-pagelet.html.twig', ['page' => $page]);
233
    }
234
}
235