Skip to content

Commit

Permalink
OP-291: Refactor RemoveSelectedProductsFromWishlistHandler to handle …
Browse files Browse the repository at this point in the history
…exceptions in controller
  • Loading branch information
hmfilar committed Jul 25, 2024
1 parent b732a4e commit 6b74600
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,25 @@
use BitBag\SyliusWishlistPlugin\Command\Wishlist\WishlistItem;
use BitBag\SyliusWishlistPlugin\Command\Wishlist\WishlistItemInterface;
use BitBag\SyliusWishlistPlugin\Entity\WishlistProductInterface;
use BitBag\SyliusWishlistPlugin\Exception\ProductNotFoundException;
use BitBag\SyliusWishlistPlugin\Exception\WishlistProductNotFoundException;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\EntityManagerInterface;
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Contracts\Translation\TranslatorInterface;

#[AsMessageHandler]
final class RemoveSelectedProductsFromWishlistHandler
{
public function __construct(
private ProductVariantRepositoryInterface $productVariantRepository,
private EntityManagerInterface $wishlistProductManager,
private RequestStack $requestStack,
private TranslatorInterface $translator,
) {
}

public function __invoke(RemoveSelectedProductsFromWishlist $removeSelectedProductsFromWishlistCommand): void
{
$this->removeSelectedProductsFromWishlist($removeSelectedProductsFromWishlistCommand->getWishlistProducts());

/** @var Session $session */
$session = $this->requestStack->getSession();

$session->getFlashBag()->add('success', $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.removed_selected_wishlist_items'));
}

private function removeSelectedProductsFromWishlist(Collection $wishlistProducts): void
Expand All @@ -60,13 +50,13 @@ private function removeProductFromWishlist(WishlistItemInterface $wishlistItem):
$wishlistProduct = $wishlistItem->getWishlistProduct();

if (null === $wishlistProduct) {
throw new ResourceNotFoundException();
throw new WishlistProductNotFoundException();
}

$productVariant = $this->productVariantRepository->find($wishlistProduct->getVariant());

if (null === $productVariant) {
throw new NotFoundHttpException();
throw new ProductNotFoundException();
}

$this->wishlistProductManager->remove($wishlistProduct);
Expand Down
9 changes: 9 additions & 0 deletions src/Controller/Action/BaseWishlistProductsAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
Expand Down Expand Up @@ -79,6 +80,14 @@ public function __invoke(int $wishlistId, Request $request): Response

abstract protected function handleCommand(FormInterface $form): void;

protected function getFlashBag(): FlashBagInterface
{
/** @var Session $session */
$session = $this->requestStack->getSession();

return $session->getFlashBag();
}

private function createForm(int $wishlistId): ?FormInterface
{
/** @var ?WishlistInterface $wishlist */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,19 @@

use BitBag\SyliusWishlistPlugin\Command\Wishlist\RemoveSelectedProductsFromWishlist;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Messenger\Exception\HandlerFailedException;

final class RemoveSelectedProductsFromWishlistAction extends BaseWishlistProductsAction
{
protected function handleCommand(FormInterface $form): void
{
$command = new RemoveSelectedProductsFromWishlist($form->getData());
$this->messageBus->dispatch($command);

try {
$this->messageBus->dispatch($command);
$this->getFlashBag()->add('success', $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.removed_selected_wishlist_items'));
} catch (HandlerFailedException) {
$this->getFlashBag()->add('error', $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.wishlist_product_not_found'));
}
}
}
9 changes: 9 additions & 0 deletions src/Exception/WishlistProductNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace BitBag\SyliusWishlistPlugin\Exception;

final class WishlistProductNotFoundException extends \Exception
{
}
2 changes: 0 additions & 2 deletions src/Resources/config/services/message_handler.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@
<service id="bitbag_sylius_wishlist_plugin.command_handler.wishlist.remove_selected_products_from_wishlist" class="BitBag\SyliusWishlistPlugin\CommandHandler\Wishlist\RemoveSelectedProductsFromWishlistHandler">
<argument type="service" id="sylius.repository.product_variant"/>
<argument type="service" id="sylius.manager.order"/>
<argument type="service" id="request_stack"/>
<argument type="service" id="translator"/>
<tag name="bitbag.sylius_wishlist_plugin.command_bus" bus="sylius.command_bus"/>
</service>

Expand Down
1 change: 1 addition & 0 deletions src/Resources/translations/messages.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ bitbag_sylius_wishlist_plugin:
copied_selected_wishlist_items: Selected products have been successfully copied.
wishlist_not_found: Wishlist not found
wishlist_for_channel_not_found: Wishlist for channel not found
wishlist_product_not_found: Wishlist product not found
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.
Expand Down
1 change: 1 addition & 0 deletions src/Resources/translations/messages.pl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ bitbag_sylius_wishlist_plugin:
product_variant_exists_in_another_wishlist: 'istnieje w innej liście życzeń.'
create_new_wishlist: 'Nowa lista życzeń została utworzona.'
wishlist_for_channel_not_found: 'Nie znaleziono listy życzeń dla aktualnego kanału'
wishlist_product_not_found: Nie znaleziono produktu z listy życzeń
clear_wishlist: 'Wyczyść listę życzeń'
cleared_wishlist: 'Lista życzeń została wyczyszczona'
wishlist_name_already_exists: 'Istnieje już lista życzeń z taką nazwą, proszę podaj inną.'
Expand Down

0 comments on commit 6b74600

Please sign in to comment.