Skip to content

Commit

Permalink
[shopsys] transport restrictions (#3397)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitek-rostislav authored Sep 30, 2024
2 parents 8971465 + 818aec1 commit 22f25f4
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 16 deletions.
24 changes: 24 additions & 0 deletions src/Component/Constraints/TransportInCartValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Shopsys\FrontendApiBundle\Model\Cart\CartApiFacade;
use Shopsys\FrontendApiBundle\Model\Transport\Exception\InvalidTransportPaymentCombinationException;
use Shopsys\FrontendApiBundle\Model\Transport\Exception\MissingPickupPlaceIdentifierException;
use Shopsys\FrontendApiBundle\Model\Transport\Exception\TransportUnavailableForProductsInCartException;
use Shopsys\FrontendApiBundle\Model\Transport\Exception\TransportWeightLimitExceededException;
use Shopsys\FrontendApiBundle\Model\Transport\TransportValidationFacade;
use Symfony\Component\Validator\Constraint;
Expand Down Expand Up @@ -58,6 +59,7 @@ public function validate($value, Constraint $constraint)
$this->checkTransportPaymentRelation($transport, $value->cartUuid, $constraint);
$this->checkRequiredPickupPlaceIdentifier($transport, $pickupPlaceIdentifier, $constraint);
$this->checkPersonalPickupStoreAvailability($transport, $pickupPlaceIdentifier, $constraint);
$this->checkTransportAvailabilityForProductsInCart($transport, $value->cartUuid, $constraint);
$this->checkTransportWeightLimit($transport, $value->cartUuid, $constraint);
} catch (TransportNotFoundException $exception) {
$this->context->buildViolation($constraint->unavailableTransportMessage)
Expand Down Expand Up @@ -131,6 +133,28 @@ protected function checkTransportWeightLimit(
}
}

/**
* @param \Shopsys\FrameworkBundle\Model\Transport\Transport $transport
* @param string|null $cartUuid
* @param \Shopsys\FrontendApiBundle\Component\Constraints\TransportInCart $transportInCartConstraint
*/
protected function checkTransportAvailabilityForProductsInCart(
Transport $transport,
?string $cartUuid,
TransportInCart $transportInCartConstraint,
): void {
$customerUser = $this->currentCustomerUser->findCurrentCustomerUser();
$cart = $this->cartApiFacade->getCartCreateIfNotExists($customerUser, $cartUuid);

try {
$this->transportValidationFacade->checkTransportAvailabilityForProductsInCart($transport, $cart);
} catch (TransportUnavailableForProductsInCartException) {
$this->context->buildViolation($transportInCartConstraint->unavailableTransportMessage)
->setCode(TransportInCart::UNAVAILABLE_TRANSPORT_ERROR)
->addViolation();
}
}

/**
* @param \Shopsys\FrameworkBundle\Model\Transport\Transport $transport
* @param string|null $cartUuid
Expand Down
29 changes: 19 additions & 10 deletions src/Model/Cart/TransportAndPaymentWatcherFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
use Shopsys\FrameworkBundle\Model\Payment\Payment;
use Shopsys\FrameworkBundle\Model\Payment\PaymentFacade;
use Shopsys\FrameworkBundle\Model\Store\Exception\StoreByUuidNotFoundException;
use Shopsys\FrameworkBundle\Model\Transport\Exception\TransportPriceNotFoundException;
use Shopsys\FrameworkBundle\Model\Transport\Transport;
use Shopsys\FrameworkBundle\Model\Transport\TransportFacade;
use Shopsys\FrameworkBundle\Model\TransportAndPayment\FreeTransportAndPaymentFacade;
use Shopsys\FrontendApiBundle\Model\Order\Exception\InvalidPacketeryAddressIdUserError;
use Shopsys\FrontendApiBundle\Model\Payment\Exception\PaymentPriceChangedException;
use Shopsys\FrontendApiBundle\Model\Payment\PaymentValidationFacade;
use Shopsys\FrontendApiBundle\Model\Transport\Exception\TransportPriceChangedException;
use Shopsys\FrontendApiBundle\Model\Transport\Exception\TransportWeightLimitExceededException;
use Shopsys\FrontendApiBundle\Model\Transport\Exception\TransportUnavailableForProductsInCartException;
use Shopsys\FrontendApiBundle\Model\Transport\TransportValidationFacade;

class TransportAndPaymentWatcherFacade
Expand Down Expand Up @@ -67,6 +68,7 @@ public function checkTransportAndPayment(
Cart $cart,
): CartWithModificationsResult {
$this->cartWithModificationsResult = $cartWithModificationsResult;
$this->checkTransport($cart);

$domainId = $this->domain->getId();

Expand Down Expand Up @@ -96,7 +98,6 @@ public function checkTransportAndPayment(
);
$this->cartWithModificationsResult->setRoundingPrice($orderData->totalPricesByItemType[OrderItemTypeEnum::TYPE_ROUNDING]);

$this->checkTransport($cart);
$this->checkPayment($cart);

return $this->cartWithModificationsResult;
Expand All @@ -106,13 +107,16 @@ public function checkTransportAndPayment(
* @param \Shopsys\FrameworkBundle\Model\Transport\Transport $transport
* @param \Shopsys\FrameworkBundle\Model\Cart\Cart $cart
*/
protected function checkTransportPrice(Transport $transport, Cart $cart): void
protected function checkTransportPriceAndWeightLimit(Transport $transport, Cart $cart): void
{
try {
$this->transportValidationFacade->checkTransportPrice($transport, $cart);
$this->transportValidationFacade->checkTransportPriceAndWeightLimit($transport, $cart);
} catch (TransportPriceChangedException $exception) {
$this->cartWithModificationsResult->setTransportPriceChanged(true);
$this->cartTransportFacade->setTransportWatchedPrice($cart, $exception->getCurrentTransportPrice()->getPriceWithVat());
} catch (TransportPriceNotFoundException) {
$this->cartWithModificationsResult->setTransportWeightLimitExceeded(true);
$this->cartTransportFacade->unsetCartTransport($cart);
}
}

Expand All @@ -134,12 +138,12 @@ protected function checkPaymentPrice(Cart $cart, Payment $payment): void
* @param \Shopsys\FrameworkBundle\Model\Transport\Transport $transport
* @param \Shopsys\FrameworkBundle\Model\Cart\Cart $cart
*/
protected function checkTransportWeightLimit(Transport $transport, Cart $cart): void
protected function checkTransportAvailabilityForProductsInCart(Transport $transport, Cart $cart): void
{
try {
$this->transportValidationFacade->checkTransportWeightLimit($transport, $cart);
} catch (TransportWeightLimitExceededException) {
$this->cartWithModificationsResult->setTransportWeightLimitExceeded(true);
$this->transportValidationFacade->checkTransportAvailabilityForProductsInCart($transport, $cart);
} catch (TransportUnavailableForProductsInCartException) {
$this->cartWithModificationsResult->setTransportIsUnavailable();
$this->cartTransportFacade->unsetCartTransport($cart);
}
}
Expand Down Expand Up @@ -194,8 +198,13 @@ protected function checkTransport(Cart $cart): void

return;
}
$this->checkTransportPrice($transport, $cart);
$this->checkTransportWeightLimit($transport, $cart);

$this->checkTransportPriceAndWeightLimit($transport, $cart);

if ($cart->getTransport() === null) {
return;
}
$this->checkTransportAvailabilityForProductsInCart($transport, $cart);
$this->checkPersonalPickupStoreAvailability($transport, $cart);
$this->checkPacketeryIdIsValid($transport, $cart);
}
Expand Down
4 changes: 1 addition & 3 deletions src/Model/Resolver/Price/PriceQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,7 @@ public function priceByTransportQuery(
protected function calculateIndependentTransportPrice(Transport $transport): Price
{
return $this->transportPriceCalculation->calculateIndependentPrice(
$transport,
$this->currencyFacade->getDomainDefaultCurrencyByDomainId($this->domain->getId()),
$this->domain->getId(),
$transport->getLowestPriceOnDomain($this->domain->getId()),
);
}
}
2 changes: 1 addition & 1 deletion src/Model/Resolver/Transport/TransportsQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ public function transportsQuery(?string $cartUuid = null): array
return $this->transportFacade->getVisibleOnCurrentDomainWithEagerLoadedDomainsAndTranslations();
}

return $this->transportFacade->getVisibleOnCurrentDomainWithEagerLoadedDomainsAndTranslations($cart->getTotalWeight());
return $this->transportFacade->getVisibleOnCurrentDomainWithEagerLoadedDomainsAndTranslations($cart);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Shopsys\FrontendApiBundle\Model\Transport\Exception;

use Exception;

class TransportUnavailableForProductsInCartException extends Exception
{
}
29 changes: 27 additions & 2 deletions src/Model/Transport/TransportValidationFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@
use Shopsys\FrameworkBundle\Model\Cart\Cart;
use Shopsys\FrameworkBundle\Model\Customer\User\CurrentCustomerUser;
use Shopsys\FrameworkBundle\Model\Store\StoreFacade;
use Shopsys\FrameworkBundle\Model\Transport\Exception\TransportPriceNotFoundException;
use Shopsys\FrameworkBundle\Model\Transport\Transport;
use Shopsys\FrameworkBundle\Model\Transport\TransportPriceFacade;
use Shopsys\FrameworkBundle\Model\Transport\TransportPriceProvider;
use Shopsys\FrameworkBundle\Model\Transport\TransportVisibilityCalculation;
use Shopsys\FrontendApiBundle\Model\Cart\CartApiFacade;
use Shopsys\FrontendApiBundle\Model\Transport\Exception\InvalidTransportPaymentCombinationException;
use Shopsys\FrontendApiBundle\Model\Transport\Exception\MissingPickupPlaceIdentifierException;
use Shopsys\FrontendApiBundle\Model\Transport\Exception\TransportPriceChangedException;
use Shopsys\FrontendApiBundle\Model\Transport\Exception\TransportUnavailableForProductsInCartException;
use Shopsys\FrontendApiBundle\Model\Transport\Exception\TransportWeightLimitExceededException;

class TransportValidationFacade
Expand All @@ -24,13 +28,17 @@ class TransportValidationFacade
* @param \Shopsys\FrameworkBundle\Model\Customer\User\CurrentCustomerUser $currentCustomerUser
* @param \Shopsys\FrontendApiBundle\Model\Cart\CartApiFacade $cartApiFacade
* @param \Shopsys\FrameworkBundle\Model\Transport\TransportPriceProvider $transportPriceProvider
* @param \Shopsys\FrameworkBundle\Model\Transport\TransportVisibilityCalculation $transportVisibilityCalculation
* @param \Shopsys\FrameworkBundle\Model\Transport\TransportPriceFacade $transportPriceFacade
*/
public function __construct(
protected readonly StoreFacade $storeFacade,
protected readonly Domain $domain,
protected readonly CurrentCustomerUser $currentCustomerUser,
protected readonly CartApiFacade $cartApiFacade,
protected readonly TransportPriceProvider $transportPriceProvider,
protected readonly TransportVisibilityCalculation $transportVisibilityCalculation,
protected readonly TransportPriceFacade $transportPriceFacade,
) {
}

Expand All @@ -56,7 +64,13 @@ public function checkPersonalPickupStoreAvailability(Transport $transport, ?stri
*/
public function checkTransportWeightLimit(Transport $transport, Cart $cart): void
{
if ($transport->getMaxWeight() !== null && $transport->getMaxWeight() < $cart->getTotalWeight()) {
try {
$this->transportPriceFacade->getTransportPriceOnDomainByTransportAndClosestWeight(
$this->domain->getId(),
$transport,
$cart->getTotalWeight(),
);
} catch (TransportPriceNotFoundException) {
throw new TransportWeightLimitExceededException();
}
}
Expand All @@ -65,7 +79,18 @@ public function checkTransportWeightLimit(Transport $transport, Cart $cart): voi
* @param \Shopsys\FrameworkBundle\Model\Transport\Transport $transport
* @param \Shopsys\FrameworkBundle\Model\Cart\Cart $cart
*/
public function checkTransportPrice(Transport $transport, Cart $cart): void
public function checkTransportAvailabilityForProductsInCart(Transport $transport, Cart $cart): void
{
if ($this->transportVisibilityCalculation->filterTransportsByProductsInCart([$transport], $cart) === []) {
throw new TransportUnavailableForProductsInCartException();
}
}

/**
* @param \Shopsys\FrameworkBundle\Model\Transport\Transport $transport
* @param \Shopsys\FrameworkBundle\Model\Cart\Cart $cart
*/
public function checkTransportPriceAndWeightLimit(Transport $transport, Cart $cart): void
{
$calculatedTransportPrice = $this->transportPriceProvider->getTransportPrice(
$cart,
Expand Down

0 comments on commit 22f25f4

Please sign in to comment.