Skip to content

Commit

Permalink
check if user is real authenticated, clean code (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
damonsson authored Oct 4, 2019
1 parent c82b7ba commit 0672dba
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 20 deletions.
9 changes: 6 additions & 3 deletions spec/Controller/Action/AddProductToWishlistActionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\User\User;
use Symfony\Component\Translation\TranslatorInterface;

final class AddProductToWishlistActionSpec extends ObjectBehavior
{
function let(
TokenStorageInterface $tokenStorage,
ProductRepositoryInterface $productRepository,
WishlistContextInterface $wishlistContext,
WishlistProductFactoryInterface $wishlistProductFactory,
Expand All @@ -32,6 +36,7 @@ function let(
UrlGeneratorInterface $urlGenerator
): void {
$this->beConstructedWith(
$tokenStorage,
$productRepository,
$wishlistContext,
$wishlistProductFactory,
Expand Down Expand Up @@ -70,7 +75,6 @@ function it_handles_the_request_and_persist_new_wishlist_for_logged_shop_user(
UrlGeneratorInterface $urlGenerator
): void {
$request->get('productId')->willReturn(1);
$request->getUser()->willReturn('shop_user');

$productRepository->find(1)->willReturn($product);
$wishlistContext->getWishlist($request)->willReturn($wishlist);
Expand All @@ -83,7 +87,7 @@ function it_handles_the_request_and_persist_new_wishlist_for_logged_shop_user(
$wishlistManager->persist($wishlist)->shouldBeCalled();
$wishlistManager->flush()->shouldBeCalled();
$flashBag->add('success', 'Product has been added to your wishlist.')->shouldBeCalled();
$wishlist->getToken()->shouldNotBeCalled();
$wishlist->getToken()->shouldBeCalled();

$this->__invoke($request)->shouldHaveType(RedirectResponse::class);
}
Expand All @@ -102,7 +106,6 @@ function it_handles_the_request_and_persist_new_wishlist_for_anonymous_user(
UrlGeneratorInterface $urlGenerator
): void {
$request->get('productId')->willReturn(1);
$request->getUser()->willReturn(null);
$productRepository->find(1)->willReturn($product);
$wishlistContext->getWishlist($request)->willReturn($wishlist);
$wishlistProductFactory->createForWishlistAndProduct($wishlist, $product)->willReturn($wishlistProduct);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Translation\TranslatorInterface;

final class AddProductVariantToWishlistActionSpec extends ObjectBehavior
{
function let(
TokenStorageInterface $tokenStorage,
ProductVariantRepositoryInterface $productVariantRepository,
WishlistContextInterface $wishlistContext,
WishlistProductFactoryInterface $wishlistProductFactory,
Expand All @@ -32,6 +34,7 @@ function let(
UrlGeneratorInterface $urlGenerator
): void {
$this->beConstructedWith(
$tokenStorage,
$productVariantRepository,
$wishlistContext,
$wishlistProductFactory,
Expand Down Expand Up @@ -70,7 +73,6 @@ function it_handles_the_request_and_persist_new_wishlist_for_logged_shop_user(
UrlGeneratorInterface $urlGenerator
): void {
$request->get('variantId')->willReturn(1);
$request->getUser()->willReturn('shop_user');

$productVariantRepository->find(1)->willReturn($productVariant);
$wishlistContext->getWishlist($request)->willReturn($wishlist);
Expand All @@ -83,7 +85,7 @@ function it_handles_the_request_and_persist_new_wishlist_for_logged_shop_user(
$wishlistManager->persist($wishlist)->shouldBeCalled();
$wishlistManager->flush()->shouldBeCalled();
$flashBag->add('success', 'Product has been added to your wishlist.')->shouldBeCalled();
$wishlist->getToken()->shouldNotBeCalled();
$wishlist->getToken()->shouldBeCalled();

$this->__invoke($request)->shouldHaveType(RedirectResponse::class);
}
Expand All @@ -102,7 +104,6 @@ function it_handles_the_request_and_persist_new_wishlist_for_anonymous_user(
UrlGeneratorInterface $urlGenerator
): void {
$request->get('variantId')->willReturn(1);
$request->getUser()->willReturn(null);
$productVariantRepository->find(1)->willReturn($productVariant);
$wishlistContext->getWishlist($request)->willReturn($wishlist);
$wishlistProductFactory->createForWishlistAndVariant($wishlist, $productVariant)->willReturn($wishlistProduct);
Expand Down
18 changes: 12 additions & 6 deletions src/Controller/Action/AddProductToWishlistAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Translation\TranslatorInterface;

final class AddProductToWishlistAction
{
/** @var TokenStorageInterface */
private $tokenStorage;

/** @var ProductRepositoryInterface */
private $productRepository;

Expand All @@ -55,6 +59,7 @@ final class AddProductToWishlistAction
private $wishlistCookieToken;

public function __construct(
TokenStorageInterface $tokenStorage,
ProductRepositoryInterface $productRepository,
WishlistContextInterface $wishlistContext,
WishlistProductFactoryInterface $wishlistProductFactory,
Expand All @@ -64,6 +69,7 @@ public function __construct(
UrlGeneratorInterface $urlGenerator,
string $wishlistCookieToken
) {
$this->tokenStorage = $tokenStorage;
$this->productRepository = $productRepository;
$this->wishlistContext = $wishlistContext;
$this->wishlistProductFactory = $wishlistProductFactory;
Expand Down Expand Up @@ -100,17 +106,17 @@ public function __invoke(Request $request): Response

$response = new RedirectResponse($this->urlGenerator->generate('bitbag_sylius_wishlist_plugin_shop_wishlist_list_products'));

$this->addWishlistToResponseCookie($wishlist, $response, $request->getUser());
$token = $this->tokenStorage->getToken();

if (null === $token || !is_object($token->getUser())) {
$this->addWishlistToResponseCookie($wishlist, $response);
}

return $response;
}

private function addWishlistToResponseCookie(WishlistInterface $wishlist, Response $response, ?string $user): void
private function addWishlistToResponseCookie(WishlistInterface $wishlist, Response $response): void
{
if (null !== $user) {
return;
}

$cookie = new Cookie($this->wishlistCookieToken, $wishlist->getToken(), strtotime('+1 year'));

$response->headers->setCookie($cookie);
Expand Down
18 changes: 12 additions & 6 deletions src/Controller/Action/AddProductVariantToWishlistAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Translation\TranslatorInterface;

final class AddProductVariantToWishlistAction
{
/** @var TokenStorageInterface */
private $tokenStorage;

/** @var ProductVariantRepositoryInterface */
private $productVariantRepository;

Expand All @@ -55,6 +59,7 @@ final class AddProductVariantToWishlistAction
private $wishlistCookieToken;

public function __construct(
TokenStorageInterface $tokenStorage,
ProductVariantRepositoryInterface $productVariantRepository,
WishlistContextInterface $wishlistContext,
WishlistProductFactoryInterface $wishlistProductFactory,
Expand All @@ -64,6 +69,7 @@ public function __construct(
UrlGeneratorInterface $urlGenerator,
string $wishlistCookieToken
) {
$this->tokenStorage = $tokenStorage;
$this->productVariantRepository = $productVariantRepository;
$this->wishlistContext = $wishlistContext;
$this->wishlistProductFactory = $wishlistProductFactory;
Expand Down Expand Up @@ -100,17 +106,17 @@ public function __invoke(Request $request): Response

$response = new RedirectResponse($this->urlGenerator->generate('bitbag_sylius_wishlist_plugin_shop_wishlist_list_products'));

$this->addWishlistToResponseCookie($wishlist, $response, $request->getUser());
$token = $this->tokenStorage->getToken();

if (null === $token || !is_object($token->getUser())) {
$this->addWishlistToResponseCookie($wishlist, $response);
}

return $response;
}

private function addWishlistToResponseCookie(WishlistInterface $wishlist, Response $response, ?string $user): void
private function addWishlistToResponseCookie(WishlistInterface $wishlist, Response $response): void
{
if (null !== $user) {
return;
}

$cookie = new Cookie($this->wishlistCookieToken, $wishlist->getToken(), strtotime('+1 year'));

$response->headers->setCookie($cookie);
Expand Down
4 changes: 2 additions & 2 deletions src/Form/Extension/AddToCartTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ public function configureOptions(OptionsResolver $resolver)
->setAllowedTypes('is_wishlist', 'bool')
;
}

public function getExtendedType(): string
{
return AddToCartType::class;
}

public static function getExtendedTypes(): array
{
return [AddToCartType::class];
Expand Down
2 changes: 2 additions & 0 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ services:
bitbag_sylius_wishlist_plugin.controller.action.add_product_to_wishlist:
class: BitBag\SyliusWishlistPlugin\Controller\Action\AddProductToWishlistAction
arguments:
- "@security.token_storage"
- "@sylius.repository.product"
- "@bitbag_sylius_wishlist_plugin.context.wishlist"
- "@bitbag_sylius_wishlist_plugin.factory.wishlist_product"
Expand All @@ -28,6 +29,7 @@ services:
bitbag_sylius_wishlist_plugin.controller.action.add_product_variant_to_wishlist:
class: BitBag\SyliusWishlistPlugin\Controller\Action\AddProductVariantToWishlistAction
arguments:
- "@security.token_storage"
- "@sylius.repository.product_variant"
- "@bitbag_sylius_wishlist_plugin.context.wishlist"
- "@bitbag_sylius_wishlist_plugin.factory.wishlist_product"
Expand Down

0 comments on commit 0672dba

Please sign in to comment.