-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #170 from BitBagCommerce/OPSRC-588/Saving_wishlist
Opsrc 588/saving wishlist
- Loading branch information
Showing
12 changed files
with
265 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
/* | ||
* This file was created by developers working at BitBag | ||
* Do you need more information about us and what we do? Visit our https://bitbag.io website! | ||
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BitBag\SyliusWishlistPlugin\Command\Wishlist; | ||
|
||
use BitBag\SyliusWishlistPlugin\Entity\WishlistInterface; | ||
use Sylius\Component\Core\Model\ShopUserInterface; | ||
|
||
final class AddWishlistToUser | ||
{ | ||
private WishlistInterface $wishlist; | ||
|
||
private ShopUserInterface $shopUser; | ||
|
||
public function __construct( | ||
WishlistInterface $wishlist, | ||
ShopUserInterface $shopUser | ||
) { | ||
$this->wishlist = $wishlist; | ||
$this->shopUser = $shopUser; | ||
} | ||
|
||
public function getWishlist(): WishlistInterface | ||
{ | ||
return $this->wishlist; | ||
} | ||
|
||
public function getShopUser(): ShopUserInterface | ||
{ | ||
return $this->shopUser; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
/* | ||
* This file was created by developers working at BitBag | ||
* Do you need more information about us and what we do? Visit our https://bitbag.io website! | ||
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BitBag\SyliusWishlistPlugin\CommandHandler\Wishlist; | ||
|
||
use BitBag\SyliusWishlistPlugin\Command\Wishlist\AddWishlistToUser; | ||
use BitBag\SyliusWishlistPlugin\Entity\WishlistInterface; | ||
use BitBag\SyliusWishlistPlugin\Exception\WishlistHasAnotherShopUserException; | ||
use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface; | ||
use BitBag\SyliusWishlistPlugin\Resolver\WishlistCookieTokenResolverInterface; | ||
use Symfony\Component\Messenger\Handler\MessageHandlerInterface; | ||
|
||
final class AddWishlistToUserHandler implements MessageHandlerInterface | ||
{ | ||
private WishlistRepositoryInterface $wishlistRepository; | ||
|
||
private WishlistCookieTokenResolverInterface $wishlistCookieTokenResolver; | ||
|
||
public function __construct( | ||
WishlistRepositoryInterface $wishlistRepository, | ||
WishlistCookieTokenResolverInterface $wishlistCookieTokenResolver | ||
) { | ||
$this->wishlistRepository = $wishlistRepository; | ||
$this->wishlistCookieTokenResolver = $wishlistCookieTokenResolver; | ||
} | ||
|
||
public function __invoke(AddWishlistToUser $addWishlistsToUser): void | ||
{ | ||
$wishlist = $addWishlistsToUser->getWishlist(); | ||
$user = $addWishlistsToUser->getShopUser(); | ||
$wishlistCookieToken = $this->wishlistCookieTokenResolver->resolve(); | ||
|
||
if ($wishlistCookieToken !== $wishlist->getToken()){ | ||
throw new WishlistHasAnotherShopUserException(); | ||
} | ||
|
||
if ($this->wishlistRepository->findOneByShopUserAndName($user, $wishlist->getName()) instanceof WishlistInterface) { | ||
$wishlist->setName($wishlist->getName().$wishlist->getId()); | ||
} | ||
|
||
$wishlist->setShopUser($user); | ||
$this->wishlistRepository->add($wishlist); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
/* | ||
* This file was created by developers working at BitBag | ||
* Do you need more information about us and what we do? Visit our https://bitbag.io website! | ||
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BitBag\SyliusWishlistPlugin\Controller\Action; | ||
|
||
use BitBag\SyliusWishlistPlugin\Command\Wishlist\AddWishlistToUser; | ||
use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface; | ||
use Sylius\Component\Core\Model\ShopUserInterface; | ||
use Symfony\Component\HttpFoundation\RedirectResponse; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; | ||
use Symfony\Component\Messenger\Exception\HandlerFailedException; | ||
use Symfony\Component\Messenger\MessageBusInterface; | ||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; | ||
use Symfony\Contracts\Translation\TranslatorInterface; | ||
use Webmozart\Assert\Assert; | ||
|
||
final class AddWishlistToUserAction | ||
{ | ||
private MessageBusInterface $commandBus; | ||
|
||
private FlashBagInterface $flashBag; | ||
|
||
private TranslatorInterface $translator; | ||
|
||
private WishlistRepositoryInterface $wishlistRepository; | ||
|
||
private UrlGeneratorInterface $urlGenerator; | ||
|
||
private TokenStorageInterface $tokenStorage; | ||
|
||
public function __construct( | ||
MessageBusInterface $commandBus, | ||
FlashBagInterface $flashBag, | ||
TranslatorInterface $translator, | ||
WishlistRepositoryInterface $wishlistRepository, | ||
UrlGeneratorInterface $urlGenerator, | ||
TokenStorageInterface $tokenStorage | ||
) { | ||
$this->commandBus = $commandBus; | ||
$this->flashBag = $flashBag; | ||
$this->translator = $translator; | ||
$this->wishlistRepository = $wishlistRepository; | ||
$this->urlGenerator = $urlGenerator; | ||
$this->tokenStorage = $tokenStorage; | ||
} | ||
|
||
public function __invoke(Request $request): Response | ||
{ | ||
/** @var ShopUserInterface $shopUser */ | ||
$shopUser = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; | ||
$wishlistId = $request->attributes->getInt('id'); | ||
$wishlist = $this->wishlistRepository->find($wishlistId); | ||
|
||
try { | ||
$updateWishlistName = new AddWishlistToUser($wishlist, $shopUser); | ||
$this->commandBus->dispatch($updateWishlistName); | ||
|
||
$this->flashBag->add( | ||
'success', | ||
$this->translator->trans('bitbag_sylius_wishlist_plugin.ui.wishlist_saved') | ||
); | ||
} catch (HandlerFailedException $exception) { | ||
$this->flashBag->add( | ||
'error', | ||
$this->translator->trans('bitbag_sylius_wishlist_plugin.ui.you_have_no_access_to_that_wishlist') | ||
); | ||
} | ||
|
||
return new RedirectResponse($this->urlGenerator->generate('bitbag_sylius_wishlist_plugin_shop_wishlist_list_wishlists')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
/* | ||
* This file was created by developers working at BitBag | ||
* Do you need more information about us and what we do? Visit our https://bitbag.io website! | ||
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BitBag\SyliusWishlistPlugin\Exception; | ||
|
||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||
|
||
final class WishlistHasAnotherShopUserException extends NotFoundHttpException | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters