Passed
Pull Request — master (#81)
by
unknown
13:46
created

AddSelectedProductsToCartHandler::addFlashMessage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 9
rs 10
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\WishlistItem;
15
use Doctrine\Common\Collections\Collection;
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 $addSelectedProductsToCartCommand): void
50
    {
51
        $this->addSelectedProductsToCart($addSelectedProductsToCartCommand->getWishlistProducts());
52
53
        $this->flashBag->add('success', $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.added_selected_wishlist_items_to_cart'));
54
    }
55
56
    private function addSelectedProductsToCart(Collection $wishlistProducts): void
57
    {
58
        /** @var WishlistItem $wishlistProduct */
59
        foreach ($wishlistProducts as $wishlistProduct) {
60
            if (!$this->isInStock($wishlistProduct)) {
61
                continue;
62
            }
63
64
            $this->addProductToWishlist($wishlistProduct);
65
        }
66
    }
67
68
    private function isInStock(WishlistItem $wishlistProduct): bool
69
    {
70
        $cartItem = $wishlistProduct->getCartItem()->getCartItem();
71
72
        if ($wishlistProduct->getCartItem()->getCartItem()->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

72
        if ($wishlistProduct->getCartItem()->getCartItem()->/** @scrutinizer ignore-call */ getVariant()->isInStock()) {
Loading history...
73
            return true;
74
        }
75
76
        $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

76
        $message = sprintf(' "%s" does not have sufficient stock.', $cartItem->/** @scrutinizer ignore-call */ getProductName());
Loading history...
77
        $this->flashBag->add('error', $this->translator->trans($message));
78
79
        return false;
80
    }
81
82
    private function addProductToWishlist(WishlistItem $wishlistProduct): void
83
    {
84
        $cart = $wishlistProduct->getCartItem()->getCart();
85
        $cartItem = $wishlistProduct->getCartItem()->getCartItem();
86
87
        if (0 === $cartItem->getQuantity()) {
88
            $this->itemQuantityModifier->modify($cartItem, 1);
89
        }
90
91
        $this->orderModifier->addToOrder($cart, $cartItem);
92
        $this->orderRepository->add($cart);
93
    }
94
}
95