Skip to content

Commit

Permalink
Merge pull request #148 from BitBagCommerce/OPSRC-620_add_products_to…
Browse files Browse the repository at this point in the history
…_cart_error_500_fix

Opsrc 620 add products to cart error 500 fix
  • Loading branch information
milwoz authored Aug 2, 2022
2 parents 8edc3fe + 2f14bcd commit 58444b1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 19 deletions.
51 changes: 32 additions & 19 deletions src/Controller/Action/BaseWishlistProductsAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

abstract class BaseWishlistProductsAction
{
Expand All @@ -39,14 +40,17 @@ abstract class BaseWishlistProductsAction

private WishlistRepositoryInterface $wishlistRepository;

private TranslatorInterface $translator;

public function __construct(
CartContextInterface $cartContext,
FormFactoryInterface $formFactory,
FlashBagInterface $flashBag,
WishlistCommandProcessorInterface $wishlistCommandProcessor,
MessageBusInterface $messageBus,
UrlGeneratorInterface $urlGenerator,
WishlistRepositoryInterface $wishlistRepository
WishlistRepositoryInterface $wishlistRepository,
TranslatorInterface $translator
) {
$this->cartContext = $cartContext;
$this->formFactory = $formFactory;
Expand All @@ -55,22 +59,27 @@ public function __construct(
$this->messageBus = $messageBus;
$this->urlGenerator = $urlGenerator;
$this->wishlistRepository = $wishlistRepository;
$this->translator = $translator;
}

public function __invoke(int $wishlistId, Request $request): Response
{
$form = $this->createForm($wishlistId);

$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
$this->handleCommand($form);

if ($this->createForm($wishlistId) == null) {
return new RedirectResponse(
$this->urlGenerator->generate('bitbag_sylius_wishlist_plugin_shop_wishlist_show_chosen_wishlist', [
'wishlistId' => $wishlistId,
])
);
$this->urlGenerator->generate('bitbag_sylius_wishlist_plugin_shop_wishlist_list_wishlists'));
} else {
$form = $this->createForm($wishlistId);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
$this->handleCommand($form);

return new RedirectResponse(
$this->urlGenerator->generate('bitbag_sylius_wishlist_plugin_shop_wishlist_show_chosen_wishlist', [
'wishlistId' => $wishlistId,
])
);
}
}

foreach ($form->getErrors() as $error) {
Expand All @@ -86,15 +95,19 @@ public function __invoke(int $wishlistId, Request $request): Response

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

private function createForm(int $wishlistId): FormInterface
private function createForm(int $wishlistId): ?FormInterface
{
$wishlist = $this->wishlistRepository->find($wishlistId);
$cart = $this->cartContext->getCart();

$commandsArray = $this->wishlistCommandProcessor->createWishlistItemsCollection($wishlist->getWishlistProducts());

return $this->formFactory->create(WishlistCollectionType::class, ['items' => $commandsArray], [
'cart' => $cart,
]);
if ($wishlist == null) {
$this->flashBag->add('error', $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.wishlist_not_exists'));
return null;
} else {
$commandsArray = $this->wishlistCommandProcessor->createWishlistItemsCollection($wishlist->getWishlistProducts());

return $this->formFactory->create(WishlistCollectionType::class, ['items' => $commandsArray], [
'cart' => $cart,
]);
}
}
}
1 change: 1 addition & 0 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ services:
- "@sylius.command_bus"
- "@router"
- "@bitbag_sylius_wishlist_plugin.repository.wishlist"
- "@translator"

bitbag_sylius_wishlist_plugin.controller.action.base_wishlists_listing_action:
abstract: true
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 @@ -27,3 +27,4 @@ bitbag_sylius_wishlist_plugin:
variant_already_in_wishlist: Wariant znajduje się już w liście życzeń.
selected_wishlist_items_successfully_exported: Wybrane produkty zostały pomyślnie wyeksportowane.
copied_selected_wishlist_items: Wybrane produkty zostały pomyślnie skopiowane.
wishlist_not_exists: Your wishlist does not exists.

0 comments on commit 58444b1

Please sign in to comment.