Skip to content

Commit

Permalink
Add next iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomanhez committed Sep 22, 2020
1 parent c384acf commit 51eb736
Show file tree
Hide file tree
Showing 15 changed files with 130 additions and 231 deletions.
2 changes: 1 addition & 1 deletion src/Sylius/Behat/Context/Api/Shop/CartContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public function iRemoveProductFromTheCart(ProductInterface $product, string $tok
*/
public function iDoNotHaveAccessToSeeTheSummaryOfMyCart(): void
{
Assert::true($this->getCart()['code'] === 404);
Assert::same($this->getCart()['code'], 404);
}

/**
Expand Down
6 changes: 5 additions & 1 deletion src/Sylius/Behat/Context/Api/Shop/CheckoutContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ public function iConfirmMyOrder(): void
*/
public function iProceededWithShippingMethod(ShippingMethodInterface $shippingMethod): void
{
$y = $this->getCart();

$x = (string) $this->iriConverter->getItemFromIri($y['shipments'][0])->getId();

$this->client->request(
Request::METHOD_PATCH,
\sprintf(
Expand All @@ -276,7 +280,7 @@ public function iProceededWithShippingMethod(ShippingMethodInterface $shippingMe
*/
public function theVisitorHasNoProceedWithShippingMethodInTheCustomerCart(): void
{
Assert::true($this->getCart()['code'] === 404);
Assert::same($this->getCart()['code'], 404);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Doctrine\Persistence\ObjectManager;
use SM\Factory\FactoryInterface as StateMachineFactoryInterface;
use Sylius\Bundle\ApiBundle\Command\Checkout\AddressOrder;
use Sylius\Bundle\ApiBundle\Context\CartVisitorsCustomerContextInterface;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\OrderCheckoutTransitions;
Expand All @@ -43,23 +42,18 @@ final class AddressOrderHandler
/** @var StateMachineFactoryInterface */
private $stateMachineFactory;

/** @var CartVisitorsCustomerContextInterface */
private $cartVisitorsCustomerContext;

public function __construct(
OrderRepositoryInterface $orderRepository,
CustomerRepositoryInterface $customerRepository,
FactoryInterface $customerFactory,
ObjectManager $manager,
StateMachineFactoryInterface $stateMachineFactory,
CartVisitorsCustomerContextInterface $cartVisitorsCustomerContext
StateMachineFactoryInterface $stateMachineFactory
) {
$this->orderRepository = $orderRepository;
$this->customerRepository = $customerRepository;
$this->customerFactory = $customerFactory;
$this->manager = $manager;
$this->stateMachineFactory = $stateMachineFactory;
$this->cartVisitorsCustomerContext = $cartVisitorsCustomerContext;
}

public function __invoke(AddressOrder $addressOrder): OrderInterface
Expand All @@ -82,8 +76,6 @@ public function __invoke(AddressOrder $addressOrder): OrderInterface
$customer = $this->provideCustomerByEmail($addressOrder->email);

$order->setCustomer($customer);

$this->cartVisitorsCustomerContext->setCartCustomerId((string) $customer->getId());
}

$order->setBillingAddress($addressOrder->billingAddress);
Expand All @@ -106,7 +98,6 @@ private function provideCustomerByEmail(?string $email): CustomerInterface
$customer = $this->customerFactory->createNew();
$customer->setEmail($email);
$this->manager->persist($customer);
$this->manager->flush();
}

return $customer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Doctrine\Persistence\ObjectManager;
use Sylius\Bundle\AdminApiBundle\Model\UserInterface;
use Sylius\Bundle\ApiBundle\Command\Cart\PickupCart;
use Sylius\Bundle\ApiBundle\Context\CartVisitorsCustomerContextInterface;
use Sylius\Bundle\ApiBundle\Context\UserContextInterface;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Core\Model\ChannelInterface;
Expand Down Expand Up @@ -46,23 +45,18 @@ final class PickupCartHandler implements MessageHandlerInterface
/** @var RandomnessGeneratorInterface */
private $generator;

/** @var CartVisitorsCustomerContextInterface */
private $cartVisitorsCustomerContext;

public function __construct(
FactoryInterface $cartFactory,
ChannelContextInterface $channelContext,
UserContextInterface $userContext,
ObjectManager $orderManager,
RandomnessGeneratorInterface $generator,
CartVisitorsCustomerContextInterface $cartVisitorsCustomerContext
RandomnessGeneratorInterface $generator
) {
$this->cartFactory = $cartFactory;
$this->channelContext = $channelContext;
$this->userContext = $userContext;
$this->orderManager = $orderManager;
$this->generator = $generator;
$this->cartVisitorsCustomerContext = $cartVisitorsCustomerContext;
}

public function __invoke(PickupCart $pickupCart)
Expand All @@ -83,8 +77,6 @@ public function __invoke(PickupCart $pickupCart)
$cart->setCustomer($customer);
}

$this->cartVisitorsCustomerContext->setCartCustomerId(null);

$cart->setChannel($channel);
$cart->setLocaleCode($locale->getCode());
$cart->setCurrencyCode($currency->getCode());
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,12 @@
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryItemExtensionInterface;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use Doctrine\ORM\QueryBuilder;
use Sylius\Bundle\ApiBundle\Context\CartVisitorsCustomerContextInterface;
use Sylius\Bundle\ApiBundle\Context\UserContextInterface;
use Sylius\Bundle\ApiBundle\Doctrine\ApiShopRequestMethodsTypes;
use Sylius\Bundle\ApiBundle\Serializer\ContextKeys;
use Sylius\Component\Core\Model\AdminUserInterface;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\ShopUserInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\Security\Core\User\UserInterface;

Expand All @@ -35,15 +31,9 @@ final class OrderGetMethodItemExtension implements QueryItemExtensionInterface
/** @var UserContextInterface */
private $userContext;

/** @var CartVisitorsCustomerContextInterface */
private $cartVisitorsCustomerContext;

public function __construct(
UserContextInterface $userContext,
CartVisitorsCustomerContextInterface $cartVisitorsCustomerContext
) {
public function __construct(UserContextInterface $userContext)
{
$this->userContext = $userContext;
$this->cartVisitorsCustomerContext = $cartVisitorsCustomerContext;
}

public function applyToItem(
Expand Down Expand Up @@ -73,19 +63,12 @@ private function applyToItemForGetMethod(
QueryBuilder $queryBuilder,
string $rootAlias
): void {
/** @var string|null $cartCustomerId */
$cartCustomerId = $this->cartVisitorsCustomerContext->getCartCustomerId();

if ($user === null && $cartCustomerId === null) {
$queryBuilder->andWhere(sprintf('%s.customer IS NULL', $rootAlias));

return;
}

if ($user === null && $cartCustomerId !== null) {
if ($user === null) {
$queryBuilder
->andWhere(sprintf('%s.customer = :customer', $rootAlias))
->setParameter('customer', $cartCustomerId)
->leftJoin(sprintf('%s.customer', $rootAlias), 'customer')
->leftJoin('customer.user', 'user')
->andWhere('user IS NULL')
->orWhere(sprintf('%s.customer IS NULL', $rootAlias))
;

return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryItemExtensionInterface;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use Doctrine\ORM\QueryBuilder;
use Sylius\Bundle\ApiBundle\Context\CartVisitorsCustomerContextInterface;
use Sylius\Bundle\ApiBundle\Context\UserContextInterface;
use Sylius\Bundle\ApiBundle\Serializer\ContextKeys;
use Sylius\Component\Core\Model\AdminUserInterface;
Expand All @@ -32,15 +31,9 @@ final class OrderMethodsItemExtension implements QueryItemExtensionInterface
/** @var UserContextInterface */
private $userContext;

/** @var CartVisitorsCustomerContextInterface */
private $cartVisitorsCustomerContext;

public function __construct(
UserContextInterface $userContext,
CartVisitorsCustomerContextInterface $cartVisitorsCustomerContext
) {
public function __construct(UserContextInterface $userContext)
{
$this->userContext = $userContext;
$this->cartVisitorsCustomerContext = $cartVisitorsCustomerContext;
}

public function applyToItem(
Expand Down Expand Up @@ -73,23 +66,12 @@ private function applyUserRulesToItem(
string $rootAlias,
string $httpRequestMethodType
): void {
/** @var string|null $cartCustomerId */
$cartCustomerId = $this->cartVisitorsCustomerContext->getCartCustomerId();

if ($user === null && $cartCustomerId === null) {
$queryBuilder
->andWhere(sprintf('%s.customer IS NULL', $rootAlias))
->andWhere(sprintf('%s.state = :state', $rootAlias))
->setParameter('state', OrderInterface::STATE_CART)
;

return;
}

if ($user === null && $cartCustomerId !== null) {
if ($user === null) {
$queryBuilder
->andWhere(sprintf('%s.customer = :customer', $rootAlias))
->setParameter('customer', $cartCustomerId)
->leftJoin(sprintf('%s.customer', $rootAlias), 'customer')
->leftJoin('customer.user', 'user')
->andWhere('user IS NULL')
->orWhere(sprintf('%s.customer IS NULL', $rootAlias))
->andWhere(sprintf('%s.state = :state', $rootAlias))
->setParameter('state', OrderInterface::STATE_CART)
;
Expand All @@ -108,9 +90,11 @@ private function applyUserRulesToItem(
return;
}

if ($user instanceof AdminUserInterface &&
if (
$user instanceof AdminUserInterface &&
in_array('ROLE_API_ACCESS', $user->getRoles(), true) &&
$httpRequestMethodType === Request::METHOD_DELETE) {
$httpRequestMethodType === Request::METHOD_DELETE
) {
$queryBuilder
->andWhere(sprintf('%s.state = :state', $rootAlias))
->setParameter('state', OrderInterface::STATE_CART)
Expand All @@ -119,9 +103,11 @@ private function applyUserRulesToItem(
return;
}

if ($user instanceof AdminUserInterface &&
if (
$user instanceof AdminUserInterface &&
in_array('ROLE_API_ACCESS', $user->getRoles(), true) &&
$httpRequestMethodType !== Request::METHOD_DELETE) {
$httpRequestMethodType !== Request::METHOD_DELETE
) {
//admin has also access to modified orders in states other than cart

return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
<argument type="service" id="sylius.api.context.user" />
<argument type="service" id="sylius.manager.order" />
<argument type="service" id="sylius.random_generator" />
<argument type="service" id="sylius.api.context.cart.visitors.customer" />
<tag name="messenger.message_handler" />
</service>

Expand All @@ -56,7 +55,6 @@
<argument type="service" id="sylius.factory.customer"/>
<argument type="service" id="sylius.manager.customer"/>
<argument type="service" id="sm.factory" />
<argument type="service" id="sylius.api.context.cart.visitors.customer" />
<tag name="messenger.message_handler" />
</service>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,5 @@
<service id="sylius.api.context.user" class="Sylius\Bundle\ApiBundle\Context\TokenBasedUserContext">
<argument type="service" id="security.token_storage" />
</service>

<service id="sylius.api.context.cart.visitors.customer" class="Sylius\Bundle\ApiBundle\Context\CartVisitorsCustomerContext">
<argument type="service" id="session"/>
</service>
</services>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
public="true"
>
<argument type="service" id="sylius.api.context.user" />
<argument type="service" id="sylius.api.context.cart.visitors.customer" />
<tag name="api_platform.doctrine.orm.query_extension.item" />
</service>

Expand All @@ -50,7 +49,6 @@
public="true"
>
<argument type="service" id="sylius.api.context.user" />
<argument type="service" id="sylius.api.context.cart.visitors.customer" />
<tag name="api_platform.doctrine.orm.query_extension.item" />
</service>
</services>
Expand Down

0 comments on commit 51eb736

Please sign in to comment.