-
Notifications
You must be signed in to change notification settings - Fork 90
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
Nenad Stefanovic
authored and
Nenad Stefanovic
committed
Nov 24, 2020
1 parent
ec87c8c
commit 4f51cf3
Showing
10 changed files
with
204 additions
and
122 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,3 @@ | |
|
||
.phpunit.result.cache | ||
/phpunit.xml | ||
|
||
/.idea/ | ||
/docker/ |
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
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
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
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
62 changes: 0 additions & 62 deletions
62
src/Validator/Cart/CartCheckoutItemEligibilityValidator.php
This file was deleted.
Oops, something went wrong.
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,84 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\ShopApiPlugin\Validator\Cart; | ||
|
||
use Sylius\Component\Core\Model\OrderInterface; | ||
use Sylius\Component\Core\Model\OrderItemInterface; | ||
use Sylius\Component\Core\Model\ProductInterface; | ||
use Sylius\Component\Core\Model\ProductVariantInterface; | ||
use Sylius\Component\Core\Repository\OrderRepositoryInterface; | ||
use Sylius\ShopApiPlugin\Validator\Constraints\CartEligibility; | ||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
use Webmozart\Assert\Assert; | ||
|
||
final class CartEligibilityValidator extends ConstraintValidator | ||
{ | ||
/** | ||
* @var OrderRepositoryInterface | ||
*/ | ||
private $_cartRepository; | ||
|
||
/** | ||
* CartEligibilityValidator constructor. | ||
* | ||
* @param OrderRepositoryInterface $cartRepository | ||
*/ | ||
public function __construct(OrderRepositoryInterface $cartRepository) | ||
{ | ||
$this->_cartRepository = $cartRepository; | ||
} | ||
|
||
/** | ||
* @param mixed $token | ||
* @param Constraint $constraint | ||
* | ||
* @return void | ||
*/ | ||
public function validate($token, Constraint $constraint): void | ||
{ | ||
Assert::isInstanceOf($constraint, CartEligibility::class); | ||
|
||
$cart = $this->_cartRepository->findOneBy( | ||
[ | ||
'tokenValue' => $token, | ||
'state' => OrderInterface::STATE_CART | ||
] | ||
); | ||
|
||
if ($cart === null) { | ||
return; | ||
} | ||
|
||
Assert::isInstanceOf($cart, OrderInterface::class); | ||
|
||
$cartItems = $cart->getItems(); | ||
foreach ($cartItems as $item) { | ||
Assert::isInstanceOf($item, OrderItemInterface::class); | ||
|
||
$variant = $item->getVariant(); | ||
|
||
Assert::isInstanceOf($variant, ProductVariantInterface::class); | ||
if (!$variant->isEnabled()) { | ||
$this->context->addViolation( | ||
$constraint->nonEligibleCartItemVariantMessage | ||
); | ||
|
||
break; | ||
} | ||
|
||
$product = $variant->getProduct(); | ||
|
||
Assert::isInstanceOf($product, ProductInterface::class); | ||
if (!$product->isEnabled()) { | ||
$this->context->addViolation( | ||
$constraint->nonEligibleCartItemMessage | ||
); | ||
|
||
break; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.