Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added new logic to take care of adding products to wishlist #154

Merged
merged 6 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 %}