Passed
Push — master ( 35c30e...6daa1b )
by Przemysław eRIZ
04:42
created

ListWishlistProductsAction::handleCartItems()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 16
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file was created by developers working at BitBag
5
 * Do you need more information about us and what we do? Visit our https://bitbag.io website!
6
 * We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
7
*/
8
9
declare(strict_types=1);
10
11
namespace BitBag\SyliusWishlistPlugin\Controller\Action;
12
13
use BitBag\SyliusWishlistPlugin\Context\WishlistContextInterface;
14
use BitBag\SyliusWishlistPlugin\Form\Type\WishlistCollectionType;
15
use BitBag\SyliusWishlistPlugin\Processor\WishlistCommandProcessorInterface;
16
use Sylius\Component\Order\Context\CartContextInterface;
17
use Symfony\Component\Form\FormFactoryInterface;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpFoundation\Response;
20
use Twig\Environment;
21
22
final class ListWishlistProductsAction
23
{
24
    private WishlistContextInterface $wishlistContext;
25
26
    private CartContextInterface $cartContext;
27
28
    private FormFactoryInterface $formFactory;
29
30
    private Environment $twigEnvironment;
31
32
    private WishlistCommandProcessorInterface $wishlistCommandProcessor;
33
34
    public function __construct(
35
        WishlistContextInterface $wishlistContext,
36
        CartContextInterface $cartContext,
37
        FormFactoryInterface $formFactory,
38
        Environment $twigEnvironment,
39
        WishlistCommandProcessorInterface $wishlistCommandProcessor
40
    ) {
41
        $this->wishlistContext = $wishlistContext;
42
        $this->cartContext = $cartContext;
43
        $this->formFactory = $formFactory;
44
        $this->twigEnvironment = $twigEnvironment;
45
        $this->wishlistCommandProcessor = $wishlistCommandProcessor;
46
    }
47
48
    public function __invoke(Request $request): Response
49
    {
50
        $wishlist = $this->wishlistContext->getWishlist($request);
51
        $cart = $this->cartContext->getCart();
52
53
        $commandsArray = $this->wishlistCommandProcessor->createAddCommandCollectionFromWishlistProducts($wishlist->getWishlistProducts());
54
55
        $form = $this->formFactory->create(WishlistCollectionType::class, ['items' => $commandsArray], [
56
            'cart' => $cart,
57
        ]);
58
59
        return new Response(
60
            $this->twigEnvironment->render('@BitBagSyliusWishlistPlugin/WishlistDetails/index.html.twig', [
61
                'wishlist' => $wishlist,
62
                'form' => $form->createView(),
63
            ])
64
        );
65
    }
66
}
67