Passed
Pull Request — master (#84)
by
unknown
03:57
created

AddSelectedProductsToCartHandler   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 55
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 12 3
A __construct() 0 12 1
A handleItem() 0 14 2
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\CommandHandler\Wishlist;
12
13
use BitBag\SyliusWishlistPlugin\Command\Wishlist\AddSelectedProductsToCart;
14
use BitBag\SyliusWishlistPlugin\Command\Wishlist\AddWishlistProduct;
15
use Sylius\Bundle\OrderBundle\Controller\AddToCartCommandInterface;
16
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
17
use Sylius\Component\Order\Modifier\OrderItemQuantityModifierInterface;
18
use Sylius\Component\Order\Modifier\OrderModifierInterface;
19
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
20
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
21
use Symfony\Contracts\Translation\TranslatorInterface;
22
23
final class AddSelectedProductsToCartHandler implements MessageHandlerInterface
24
{
25
    private FlashBagInterface $flashBag;
26
27
    private TranslatorInterface $translator;
28
29
    private OrderItemQuantityModifierInterface $itemQuantityModifier;
30
31
    private OrderModifierInterface $orderModifier;
32
33
    private OrderRepositoryInterface $orderRepository;
34
35
    public function __construct(
36
        FlashBagInterface $flashBag,
37
        TranslatorInterface $translator,
38
        OrderItemQuantityModifierInterface $itemQuantityModifier,
39
        OrderModifierInterface $orderModifier,
40
        OrderRepositoryInterface $orderRepository
41
    ) {
42
        $this->flashBag = $flashBag;
43
        $this->translator = $translator;
44
        $this->itemQuantityModifier = $itemQuantityModifier;
45
        $this->orderModifier = $orderModifier;
46
        $this->orderRepository = $orderRepository;
47
    }
48
49
    public function __invoke(AddSelectedProductsToCart $addSelectedProductsToCart): void
50
    {
51
        /** @var AddWishlistProduct $wishlistProduct */
52
        foreach ($addSelectedProductsToCart->getWishlistProducts() as $wishlistProduct) {
53
            if (!$wishlistProduct->isSelected()) {
54
                continue;
55
            }
56
57
            /** @var AddToCartCommandInterface $addToCartCommand */
58
            $addToCartCommand = $wishlistProduct->getCartItem();
59
60
            $this->handleItem($addToCartCommand);
61
        }
62
    }
63
64
    private function handleItem(AddToCartCommandInterface $addToCartCommand): void
65
    {
66
        $cart = $addToCartCommand->getCart();
67
        $cartItem = $addToCartCommand->getCartItem();
68
69
        if (!$cartItem->getVariant()->isInStock()) {
0 ignored issues
show
Bug introduced by
The method getVariant() does not exist on Sylius\Component\Order\Model\OrderItemInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Sylius\Component\Order\Model\OrderItem. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

69
        if (!$cartItem->/** @scrutinizer ignore-call */ getVariant()->isInStock()) {
Loading history...
70
            $message = sprintf('%s does not have sufficient stock.', $cartItem->getProductName());
0 ignored issues
show
Bug introduced by
The method getProductName() does not exist on Sylius\Component\Order\Model\OrderItemInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Sylius\Component\Order\Model\OrderItem. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
            $message = sprintf('%s does not have sufficient stock.', $cartItem->/** @scrutinizer ignore-call */ getProductName());
Loading history...
71
            $this->flashBag->add('error', $this->translator->trans($message));
72
        } else {
73
            $this->itemQuantityModifier->modify($cartItem, 1);
74
            $this->orderModifier->addToOrder($cart, $cartItem);
75
            $this->orderRepository->add($cart);
76
77
            $this->flashBag->add('success', $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.added_to_cart'));
78
        }
79
    }
80
}
81