Skip to content

Commit

Permalink
Merge pull request #154 from BitBagCommerce/Copying_products_fix
Browse files Browse the repository at this point in the history
Added new logic to take care of adding products to wishlist
  • Loading branch information
milwoz authored Aug 10, 2022
2 parents a1660ac + 2dc2ed8 commit 7f6b350
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 115 deletions.
17 changes: 10 additions & 7 deletions spec/Duplicator/WishlistProductsToOtherWishlistDuplicatorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,24 @@
use PhpSpec\ObjectBehavior;
use Sylius\Component\Core\Model\ProductVariantInterface;
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

final class WishlistProductsToOtherWishlistDuplicatorSpec extends ObjectBehavior
{
public function let(
ProductVariantInWishlistGuardInterface $productVariantInWishlistGuard,
WishlistProductFactoryFacadeInterface $wishlistProductVariantFactory,
ProductVariantRepositoryInterface $productVariantRepository,
WishlistRepositoryInterface $wishlistRepository
WishlistRepositoryInterface $wishlistRepository,
FlashBagInterface $flashBag,
TranslatorInterface $translator
): void {
$this->beConstructedWith(
$productVariantInWishlistGuard,
$wishlistProductVariantFactory,
$productVariantRepository,
$wishlistRepository
$wishlistRepository,
$flashBag,
$translator
);
}

Expand All @@ -46,15 +50,14 @@ public function it_copy_wishlist_products(
ProductVariantRepositoryInterface $productVariantRepository,
ProductVariantInterface $variant1,
ProductVariantInterface $variant2,
ProductVariantInWishlistGuardInterface $productVariantInWishlistGuard,
WishlistInterface $destinedWishlist,
WishlistRepositoryInterface $wishlistRepository
): void {
$productVariantRepository->find("1")->willReturn($variant1);
$productVariantRepository->find("24")->willReturn($variant2);

$productVariantInWishlistGuard->check($destinedWishlist, $variant1)->shouldBeCalledOnce();
$productVariantInWishlistGuard->check($destinedWishlist, $variant2)->shouldBeCalledOnce();
$destinedWishlist->hasProductVariant($variant1)->shouldBeCalled();
$destinedWishlist->hasProductVariant($variant2)->shouldBeCalled();

$wishlistRepository->add($destinedWishlist)->shouldBeCalledOnce();

Expand Down
47 changes: 0 additions & 47 deletions spec/Guard/ProductVariantInWishlistGuardSpec.php

This file was deleted.

28 changes: 21 additions & 7 deletions src/Duplicator/WishlistProductsToOtherWishlistDuplicator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,33 @@
use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface;
use Doctrine\Common\Collections\Collection;
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

final class WishlistProductsToOtherWishlistDuplicator implements WishlistProductsToOtherWishlistDuplicatorInterface
{
private ProductVariantInWishlistGuardInterface $productVariantInWishlistGuard;

private WishlistProductFactoryFacadeInterface $wishlistProductVariantFactory;

private ProductVariantRepositoryInterface $productVariantRepository;

private WishlistRepositoryInterface $wishlistRepository;

private FlashBagInterface $flashBag;

private TranslatorInterface $translator;

public function __construct(
ProductVariantInWishlistGuardInterface $productVariantInWishlistGuard,
WishlistProductFactoryFacadeInterface $wishlistProductVariantFactory,
ProductVariantRepositoryInterface $productVariantRepository,
WishlistRepositoryInterface $wishlistRepository
WishlistRepositoryInterface $wishlistRepository,
FlashBagInterface $flashBag,
TranslatorInterface $translator
) {
$this->productVariantInWishlistGuard = $productVariantInWishlistGuard;
$this->wishlistProductVariantFactory = $wishlistProductVariantFactory;
$this->productVariantRepository = $productVariantRepository;
$this->wishlistRepository = $wishlistRepository;
$this->flashBag = $flashBag;
$this->translator = $translator;
}

public function copyWishlistProductsToOtherWishlist(Collection $wishlistProducts, WishlistInterface $destinedWishlist): void
Expand All @@ -47,8 +53,16 @@ public function copyWishlistProductsToOtherWishlist(Collection $wishlistProducts
foreach ($wishlistProducts as $wishlistProduct) {
$variant = $this->productVariantRepository->find($wishlistProduct['variant']);

$this->productVariantInWishlistGuard->check($destinedWishlist, $variant);
$this->wishlistProductVariantFactory->createWithProductVariant($destinedWishlist, $variant);
if ($destinedWishlist->hasProductVariant($variant)) {
$message = $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.product_variant_exists_in_another_wishlist');

$this->flashBag->add(
'error',
sprintf("%s".$message, $variant)
);
} else {
$this->wishlistProductVariantFactory->createWithProductVariant($destinedWishlist, $variant);
}
}
$this->wishlistRepository->add($destinedWishlist);
}
Expand Down
26 changes: 0 additions & 26 deletions src/Guard/ProductVariantInWishlistGuard.php

This file was deleted.

20 changes: 0 additions & 20 deletions src/Guard/ProductVariantInWishlistGuardInterface.php

This file was deleted.

5 changes: 3 additions & 2 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -333,14 +333,15 @@ services:
bitbag_sylius_wishlist_plugin.services.duplicator:
class: BitBag\SyliusWishlistPlugin\Duplicator\WishlistProductsToOtherWishlistDuplicator
arguments:
- "@bitbag_sylius_wishlist_plugin.guard.product_variant_already_in_wishlist"
- "@bitbag_sylius_wishlist_plugin.facade.wishlist_product_factory_facade"
- "@sylius.repository.product_variant"
- "@bitbag_sylius_wishlist_plugin.repository.wishlist"
- "@session.flash_bag"
- "@translator"

bitbag_sylius_wishlist_plugin.checker.wishlist_name_checker:
class: BitBag\SyliusWishlistPlugin\Checker\WishlistNameChecker

bitbag_sylius_wishlist_plugin.services.product_quantity_checker:
class: BitBag\SyliusWishlistPlugin\Checker\ProductQuantityChecker

Expand Down
3 changes: 0 additions & 3 deletions src/Resources/config/services/guard.yml

This file was deleted.

2 changes: 2 additions & 0 deletions src/Resources/translations/messages.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,5 @@ bitbag_sylius_wishlist_plugin:
csv_file_contains_incorrect_products: CSV file contains incorrect products.
wishlist_name_already_exists: Wishlist name already exists, try another.
does_not_have_sufficient_stock: does not have sufficient stock.
change_wishlist_name: Change wishlist name.
product_variant_exists_in_another_wishlist: exists in another wishlist.
6 changes: 3 additions & 3 deletions src/Resources/views/CreateWishlist/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
{% block content %}
{{ form_start(form) }}
<h2>

{{ ('bitbag_sylius_wishlist_plugin.ui.change_wishlist_name'|trans) }}
</h2>
<section class="bb-wishlist-create">
<div class="bb-wishlist-create-name">
{{ form_row(form.name) }}
</div>

{{ form_row(form.save) }}
</section>
{{ form_end(form) }}
{% endblock %}
{% endblock %}

0 comments on commit 7f6b350

Please sign in to comment.