-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Cart] Add custom put item to cart command provider
- Loading branch information
Showing
6 changed files
with
163 additions
and
41 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 |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace spec\Sylius\ShopApiPlugin\CommandProvider; | ||
|
||
use PhpSpec\ObjectBehavior; | ||
use Sylius\ShopApiPlugin\CommandProvider\CommandProviderInterface; | ||
use Sylius\ShopApiPlugin\Request\Cart\PutOptionBasedConfigurableItemToCartRequest; | ||
use Sylius\ShopApiPlugin\Request\Cart\PutSimpleItemToCartRequest; | ||
use Sylius\ShopApiPlugin\Request\Cart\PutVariantBasedConfigurableItemToCartRequest; | ||
use Symfony\Component\HttpFoundation\ParameterBag; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\Validator\ConstraintViolationListInterface; | ||
use Symfony\Component\Validator\Validator\ValidatorInterface; | ||
|
||
final class PutItemToCartCommandProviderSpec extends ObjectBehavior | ||
{ | ||
function let(ValidatorInterface $validator): void | ||
{ | ||
$this->beConstructedWith($validator); | ||
} | ||
|
||
function it_implements_command_provider_interface(): void | ||
{ | ||
$this->shouldHaveType(CommandProviderInterface::class); | ||
} | ||
|
||
function it_validates_put_simple_item_to_cart_request( | ||
ValidatorInterface $validator, | ||
Request $httpRequest, | ||
ConstraintViolationListInterface $constraintViolationList | ||
): void { | ||
$httpRequest->attributes = new ParameterBag(['token' => 'ORDERTOKEN']); | ||
$httpRequest->request = new ParameterBag([ | ||
'productCode' => 'HACKTOBERFEST_TSHIRT_CODE', | ||
'quantity' => 4, | ||
]); | ||
|
||
$validator | ||
->validate(PutSimpleItemToCartRequest::fromHttpRequest($httpRequest->getWrappedObject())) | ||
->willReturn($constraintViolationList) | ||
; | ||
|
||
$this->validate($httpRequest)->shouldReturn($constraintViolationList); | ||
} | ||
|
||
function it_validates_put_variant_based_configurable_item_to_cart_request( | ||
ValidatorInterface $validator, | ||
Request $httpRequest, | ||
ConstraintViolationListInterface $constraintViolationList | ||
): void { | ||
$httpRequest->attributes = new ParameterBag(['token' => 'ORDERTOKEN']); | ||
$httpRequest->request = new ParameterBag([ | ||
'productCode' => 'HACKTOBERFEST_TSHIRT_CODE', | ||
'variantCode' => 'LARGE_HACKTOBERFEST_TSHIRT_CODE', | ||
'quantity' => 4, | ||
]); | ||
|
||
$validator | ||
->validate(PutVariantBasedConfigurableItemToCartRequest::fromHttpRequest($httpRequest->getWrappedObject())) | ||
->willReturn($constraintViolationList) | ||
; | ||
|
||
$this->validate($httpRequest)->shouldReturn($constraintViolationList); | ||
} | ||
|
||
function it_validates_put_option_based_configurable_item_to_cart_request( | ||
ValidatorInterface $validator, | ||
Request $httpRequest, | ||
ConstraintViolationListInterface $constraintViolationList | ||
): void { | ||
$httpRequest->attributes = new ParameterBag(['token' => 'ORDERTOKEN']); | ||
$httpRequest->request = new ParameterBag([ | ||
'productCode' => 'HACKTOBERFEST_TSHIRT_CODE', | ||
'options' => ['LARGE_CODE'], | ||
'quantity' => 4, | ||
]); | ||
|
||
$validator | ||
->validate(PutOptionBasedConfigurableItemToCartRequest::fromHttpRequest($httpRequest->getWrappedObject())) | ||
->willReturn($constraintViolationList) | ||
; | ||
|
||
$this->validate($httpRequest)->shouldReturn($constraintViolationList); | ||
} | ||
} |
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,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\ShopApiPlugin\CommandProvider; | ||
|
||
use Sylius\ShopApiPlugin\Command\CommandInterface; | ||
use Sylius\ShopApiPlugin\Request\Cart\PutOptionBasedConfigurableItemToCartRequest; | ||
use Sylius\ShopApiPlugin\Request\Cart\PutSimpleItemToCartRequest; | ||
use Sylius\ShopApiPlugin\Request\Cart\PutVariantBasedConfigurableItemToCartRequest; | ||
use Sylius\ShopApiPlugin\Request\RequestInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||
use Symfony\Component\Validator\ConstraintViolationListInterface; | ||
use Symfony\Component\Validator\Validator\ValidatorInterface; | ||
|
||
final class PutItemToCartCommandProvider implements CommandProviderInterface | ||
{ | ||
/** @var ValidatorInterface */ | ||
private $validator; | ||
|
||
public function __construct(ValidatorInterface $validator) | ||
{ | ||
$this->validator = $validator; | ||
} | ||
|
||
public function validate(Request $httpRequest): ConstraintViolationListInterface | ||
{ | ||
return $this->validator->validate($this->transformHttpRequest($httpRequest)); | ||
} | ||
|
||
public function getCommand(Request $httpRequest): CommandInterface | ||
{ | ||
return $this->transformHttpRequest($httpRequest)->getCommand(); | ||
} | ||
|
||
private function transformHttpRequest(Request $httpRequest): RequestInterface | ||
{ | ||
$hasVariantCode = $httpRequest->request->has('variantCode'); | ||
$hasOptionCode = $httpRequest->request->has('options'); | ||
|
||
if (!$hasVariantCode && !$hasOptionCode) { | ||
return PutSimpleItemToCartRequest::fromHttpRequest($httpRequest); | ||
} | ||
|
||
if ($hasVariantCode && !$hasOptionCode) { | ||
return PutVariantBasedConfigurableItemToCartRequest::fromHttpRequest($httpRequest); | ||
} | ||
|
||
if (!$hasVariantCode && $hasOptionCode) { | ||
return PutOptionBasedConfigurableItemToCartRequest::fromHttpRequest($httpRequest); | ||
} | ||
|
||
throw new NotFoundHttpException('Variant not found for given configuration'); | ||
} | ||
} |
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