|
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\Checker\ProductVariantInWishlistCheckerInterface; |
|
15
|
|
|
use BitBag\SyliusWishlistPlugin\Command\Wishlist\CopySelectedProductsToOtherWishlist; |
|
16
|
|
|
use BitBag\SyliusWishlistPlugin\Command\Wishlist\WishlistItemInterface; |
|
17
|
|
|
use BitBag\SyliusWishlistPlugin\Creator\WishlistProductVariantCreatorInterface; |
|
18
|
|
|
use BitBag\SyliusWishlistPlugin\Entity\WishlistInterface; |
|
19
|
|
|
use BitBag\SyliusWishlistPlugin\Exception\ProductVariantAlreadyInWishlistException; |
|
20
|
|
|
use BitBag\SyliusWishlistPlugin\Exception\WishlistProductsActionFailedException; |
|
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 ProductVariantInWishlistCheckerInterface $productVariantInWishlistChecker; |
|
30
|
|
|
|
|
31
|
|
|
private WishlistProductVariantCreatorInterface $wishlistProductVariantCreator; |
|
32
|
|
|
|
|
33
|
|
|
private ArrayCollection $unprocessedProductsName; |
|
34
|
|
|
|
|
35
|
|
|
public function __construct( |
|
36
|
|
|
WishlistRepositoryInterface $wishlistRepository, |
|
37
|
|
|
ProductVariantInWishlistCheckerInterface $productVariantInWishlistChecker, |
|
38
|
|
|
WishlistProductVariantCreatorInterface $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
|
|
|
continue; |
|
74
|
|
|
} |
|
75
|
|
|
$this->wishlistProductVariantCreator->create($destinedWishlist, $variant); |
|
76
|
|
|
} |
|
77
|
|
|
$this->wishlistRepository->add($destinedWishlist); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|