Skip to content

Commit

Permalink
Add ChangingItemQuantityValidator
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomanhez committed May 21, 2021
1 parent f2ef5a2 commit 8750842
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@
<tag name="validator.constraint_validator" alias="sylius_api_validator_adding_eligible_product_variant_to_cart" />
</service>

<service id="sylius.api.validator.changing_item_quantity_in_cart" class="Sylius\Bundle\ApiBundle\Validator\Constraints\ChangingItemQuantityInCartValidator">
<argument type="service" id="sylius.repository.order_item" />
<argument type="service" id="sylius.repository.order" />
<argument type="service" id="sylius.availability_checker" />
<tag name="validator.constraint_validator" alias="sylius_api_validator_changing_item_guantity_in_cart" />
</service>

<service id="sylius.api.validator.payment_method_eligibility" class="Sylius\Bundle\ApiBundle\Validator\Constraints\OrderPaymentMethodEligibilityValidator">
<argument type="service" id="sylius.repository.order" />
<tag name="validator.constraint_validator" alias="sylius_api_order_payment_method_eligibility" />
Expand Down
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>
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ sylius:
not_exist: 'The product %productName% does not exist.'
product_variant:
not_exist: 'The product variant with %productVariantCode% does not exist.'
not_longer_available: 'The product variant with name %productVariantName% does not exist.'
not_sufficient: 'The product variant with %productVariantCode% code does not have sufficient stock.'
shipment:
shipped: 'You cannot ship a shipment that was shipped before.'
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;
}
}
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()]
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function it_adds_violation_if_product_variant_does_not_exist(
$orderItem->getVariantName()->willReturn('MacPro');

$executionContext
->addViolation('sylius.product_variant.with_name_not_exist', ['%productVariantName%' => 'MacPro'])
->addViolation('sylius.product_variant.not_longer_available', ['%productVariantName%' => 'MacPro'])
->shouldBeCalled()
;

Expand Down Expand Up @@ -137,7 +137,7 @@ function it_adds_violation_if_product_variant_is_disabled(
$productVariant->isEnabled()->willReturn(false);

$executionContext
->addViolation('sylius.product_variant.with_name_not_exist', ['%productVariantName%' => 'Variant Name'])
->addViolation('sylius.product_variant.not_longer_available', ['%productVariantName%' => 'Variant Name'])
->shouldBeCalled()
;

Expand Down

0 comments on commit 8750842

Please sign in to comment.