Passed
Pull Request — master (#81)
by
unknown
04:35
created

CopySelectedProductsToOtherWishlistHandler   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 14 2
A __construct() 0 9 1
A copyWishlistProductsToOtherWishlist() 0 16 3
1
<?php
2
3
/*
4
 * This file has been created by developers from BitBag.
5
 * Feel free to contact us once you face any issues or want to start
6
 * You can find more information about us on https://bitbag.io and write us
7
 * an email on [email protected].
8
 */
9
10
declare(strict_types=1);
11
12
namespace BitBag\SyliusWishlistPlugin\CommandHandler\Wishlist;
13
14
use BitBag\SyliusWishlistPlugin\Command\Wishlist\CopySelectedProductsToOtherWishlist;
15
use BitBag\SyliusWishlistPlugin\Command\Wishlist\WishlistItemInterface;
16
use BitBag\SyliusWishlistPlugin\Entity\WishlistInterface;
17
use BitBag\SyliusWishlistPlugin\Exception\ProductVariantAlreadyInWishlistException;
18
use BitBag\SyliusWishlistPlugin\Exception\WishlistProductsActionFailedException;
19
use BitBag\SyliusWishlistPlugin\Facade\WishlistProductFactoryFacadeInterface;
20
use BitBag\SyliusWishlistPlugin\Guard\ProductVariantInWishlistGuardInterface;
21
use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface;
22
use Doctrine\Common\Collections\ArrayCollection;
23
use Doctrine\Common\Collections\Collection;
24
25
final class CopySelectedProductsToOtherWishlistHandler
26
{
27
    private WishlistRepositoryInterface $wishlistRepository;
28
29
    private ProductVariantInWishlistGuardInterface $productVariantInWishlistChecker;
30
31
    private WishlistProductFactoryFacadeInterface $wishlistProductVariantCreator;
32
33
    private ArrayCollection $unprocessedProductsName;
34
35
    public function __construct(
36
        WishlistRepositoryInterface $wishlistRepository,
37
        ProductVariantInWishlistGuardInterface $productVariantInWishlistChecker,
38
        WishlistProductFactoryFacadeInterface $wishlistProductVariantCreator
39
    ) {
40
        $this->wishlistRepository = $wishlistRepository;
41
        $this->productVariantInWishlistChecker = $productVariantInWishlistChecker;
42
        $this->wishlistProductVariantCreator = $wishlistProductVariantCreator;
43
        $this->unprocessedProductsName = new ArrayCollection();
44
    }
45
46
    public function __invoke(CopySelectedProductsToOtherWishlist $copySelectedProductsToOtherWishlistCommand): void
47
    {
48
        $destinedWishlistId = $copySelectedProductsToOtherWishlistCommand->getDestinedWishlistId();
49
        $wishlistProducts = $copySelectedProductsToOtherWishlistCommand->getWishlistProducts();
50
51
        /** @var WishlistInterface $destinedWishlist */
52
        $destinedWishlist = $this->wishlistRepository->find($destinedWishlistId);
53
54
        $this->copyWishlistProductsToOtherWishlist($wishlistProducts, $destinedWishlist);
55
56
        if (0 < count($this->unprocessedProductsName)) {
57
            $message = 'variant is already in wishlist.';
58
59
            throw new WishlistProductsActionFailedException($this->unprocessedProductsName, $message);
60
        }
61
    }
62
63
    private function copyWishlistProductsToOtherWishlist(Collection $wishlistProducts, WishlistInterface $destinedWishlist): void
64
    {
65
        /** @var WishlistItemInterface $wishlistProduct */
66
        foreach ($wishlistProducts as $wishlistProduct) {
67
            $variant = $wishlistProduct->getCartItem()->getCartItem()->getVariant();
68
69
            try {
70
                $this->productVariantInWishlistChecker->check($destinedWishlist, $variant);
71
            } catch (ProductVariantAlreadyInWishlistException $exception) {
72
                $this->unprocessedProductsName->add($wishlistProduct->getWishlistProduct()->getProduct()->getName());
73
74
                continue;
75
            }
76
            $this->wishlistProductVariantCreator->createWithProductVariant($destinedWishlist, $variant);
77
        }
78
        $this->wishlistRepository->add($destinedWishlist);
79
    }
80
}
81