-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
191 additions
and
2 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
29 changes: 29 additions & 0 deletions
29
src/Sylius/Bundle/ApiBundle/Resources/config/validation/ChangeItemQuantityInCart.xml
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,29 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<!-- | ||
This file is part of the Sylius package. | ||
(c) Paweł Jędrzejewski | ||
For the full copyright and license information, please view the LICENSE | ||
file that was distributed with this source code. | ||
--> | ||
|
||
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/services/constraint-mapping-1.0.xsd"> | ||
<class name="Sylius\Bundle\ApiBundle\Command\Cart\ChangeItemQuantityInCart"> | ||
<constraint name="Sylius\Bundle\ApiBundle\Validator\Constraints\ChangingItemQuantityInCart"> | ||
<option name="groups"> | ||
<value>sylius</value> | ||
</option> | ||
</constraint> | ||
<property name="quantity"> | ||
<constraint name="Range"> | ||
<option name="min">1</option> | ||
<option name="minMessage">sylius.order_item.quantity.min</option> | ||
<option name="groups">sylius</option> | ||
</constraint> | ||
</property> | ||
</class> | ||
</constraint-mapping> |
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
38 changes: 38 additions & 0 deletions
38
src/Sylius/Bundle/ApiBundle/Validator/Constraints/ChangingItemQuantityInCart.php
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,38 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\ApiBundle\Validator\Constraints; | ||
|
||
use Symfony\Component\Validator\Constraint; | ||
|
||
final class ChangingItemQuantityInCart extends Constraint | ||
{ | ||
/** @var string */ | ||
public $productNotExistMessage = 'sylius.product.not_exist'; | ||
|
||
/** @var string */ | ||
public $productVariantNotLongerAvailable = 'sylius.product_variant.not_longer_available'; | ||
|
||
/** @var string */ | ||
public $productVariantNotSufficient = 'sylius.product_variant.not_sufficient'; | ||
|
||
public function validatedBy(): string | ||
{ | ||
return 'sylius_api_validator_changing_item_guantity_in_cart'; | ||
} | ||
|
||
public function getTargets(): string | ||
{ | ||
return self::CLASS_CONSTRAINT; | ||
} | ||
} |
114 changes: 114 additions & 0 deletions
114
src/Sylius/Bundle/ApiBundle/Validator/Constraints/ChangingItemQuantityInCartValidator.php
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,114 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\ApiBundle\Validator\Constraints; | ||
|
||
use Sylius\Bundle\ApiBundle\Command\Cart\ChangeItemQuantityInCart; | ||
use Sylius\Component\Core\Model\OrderInterface; | ||
use Sylius\Component\Core\Model\OrderItemInterface; | ||
use Sylius\Component\Core\Model\ProductInterface; | ||
use Sylius\Component\Core\Repository\OrderRepositoryInterface; | ||
use Sylius\Component\Inventory\Checker\AvailabilityCheckerInterface; | ||
use Sylius\Component\Resource\Repository\RepositoryInterface; | ||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
use Webmozart\Assert\Assert; | ||
|
||
final class ChangingItemQuantityInCartValidator extends ConstraintValidator | ||
{ | ||
/** @var RepositoryInterface */ | ||
private $orderItemRepository; | ||
|
||
/** @var OrderRepositoryInterface */ | ||
private $orderRepository; | ||
|
||
/** @var AvailabilityCheckerInterface */ | ||
private $availabilityChecker; | ||
|
||
public function __construct( | ||
RepositoryInterface $orderItemRepository, | ||
OrderRepositoryInterface $orderRepository, | ||
AvailabilityCheckerInterface $availabilityChecker | ||
) { | ||
$this->orderItemRepository = $orderItemRepository; | ||
$this->orderRepository = $orderRepository; | ||
$this->availabilityChecker = $availabilityChecker; | ||
} | ||
|
||
public function validate($value, Constraint $constraint) | ||
{ | ||
Assert::isInstanceOf($value, ChangeItemQuantityInCart::class); | ||
|
||
/** @var ChangingItemQuantityInCart $constraint */ | ||
Assert::isInstanceOf($constraint, ChangingItemQuantityInCart::class); | ||
|
||
/** @var OrderItemInterface|null $orderItem */ | ||
$orderItem = $this->orderItemRepository->findOneBy(['id' => $value->orderItemId]); | ||
Assert::notNull($orderItem); | ||
|
||
$productVariant = $orderItem->getVariant(); | ||
|
||
if ($productVariant === null) { | ||
$this->context->addViolation( | ||
$constraint->productVariantNotLongerAvailable, | ||
['%productVariantName%' => $orderItem->getVariantName()] | ||
); | ||
|
||
return; | ||
} | ||
|
||
$productVariantCode = $productVariant->getCode(); | ||
|
||
/** @var ProductInterface $product */ | ||
$product = $productVariant->getProduct(); | ||
if (!$product->isEnabled()) { | ||
$this->context->addViolation( | ||
$constraint->productNotExistMessage, | ||
['%productName%' => $product->getName()] | ||
); | ||
|
||
return; | ||
} | ||
|
||
if (!$productVariant->isEnabled()) { | ||
$this->context->addViolation( | ||
$constraint->productVariantNotLongerAvailable, | ||
['%productVariantName%' => $orderItem->getVariantName()] | ||
); | ||
|
||
return; | ||
} | ||
|
||
if (!$this->availabilityChecker->isStockSufficient($productVariant, $value->quantity)) { | ||
$this->context->addViolation( | ||
$constraint->productVariantNotSufficient, | ||
['%productVariantCode%' => $productVariantCode] | ||
); | ||
|
||
return; | ||
} | ||
|
||
/** @var OrderInterface|null $cart */ | ||
$cart = $this->orderRepository->findCartByTokenValue($value->getOrderTokenValue()); | ||
Assert::notNull($cart); | ||
$channel = $cart->getChannel(); | ||
Assert::notNull($channel); | ||
|
||
if (!$product->hasChannel($channel)) { | ||
$this->context->addViolation( | ||
$constraint->productNotExistMessage, | ||
['%productName%' => $product->getName()] | ||
); | ||
} | ||
} | ||
} |
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