-
-
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.
Merge pull request #206 from lchrusciel/add-multiple-items
[Cart] Add multiple items
- Loading branch information
Showing
15 changed files
with
681 additions
and
32 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\ShopApiPlugin\Controller\Cart; | ||
|
||
use FOS\RestBundle\View\View; | ||
use FOS\RestBundle\View\ViewHandlerInterface; | ||
use League\Tactician\CommandBus; | ||
use Sylius\ShopApiPlugin\Factory\ValidationErrorViewFactoryInterface; | ||
use Sylius\ShopApiPlugin\View\ValidationErrorView; | ||
use Sylius\ShopApiPlugin\ViewRepository\CartViewRepositoryInterface; | ||
use Sylius\ShopApiPlugin\Request\PutOptionBasedConfigurableItemToCartRequest; | ||
use Sylius\ShopApiPlugin\Request\PutSimpleItemToCartRequest; | ||
use Sylius\ShopApiPlugin\Request\PutVariantBasedConfigurableItemToCartRequest; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; | ||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||
use Symfony\Component\Validator\ConstraintViolationInterface; | ||
use Symfony\Component\Validator\ConstraintViolationListInterface; | ||
use Symfony\Component\Validator\Validator\ValidatorInterface; | ||
|
||
final class PutItemsToCartAction | ||
{ | ||
/** @var ViewHandlerInterface */ | ||
private $viewHandler; | ||
|
||
/** @var CommandBus */ | ||
private $bus; | ||
|
||
/** @var ValidatorInterface */ | ||
private $validator; | ||
|
||
/** @var CartViewRepositoryInterface */ | ||
private $cartQuery; | ||
|
||
/** @var string */ | ||
private $validationErrorViewClass; | ||
|
||
public function __construct( | ||
ViewHandlerInterface $viewHandler, | ||
CommandBus $bus, | ||
ValidatorInterface $validator, | ||
CartViewRepositoryInterface $cartQuery, | ||
string $validationErrorViewClass | ||
) { | ||
$this->viewHandler = $viewHandler; | ||
$this->bus = $bus; | ||
$this->validator = $validator; | ||
$this->cartQuery = $cartQuery; | ||
$this->validationErrorViewClass = $validationErrorViewClass; | ||
} | ||
|
||
public function __invoke(Request $request): Response | ||
{ | ||
/** @var ConstraintViolationListInterface[] $validationResults */ | ||
$validationResults = []; | ||
$commandRequests = []; | ||
$commandsToExecute = []; | ||
$token = $request->attributes->get('token'); | ||
|
||
foreach ($request->request->all() as $item) { | ||
$item['token'] = $token; | ||
$commandRequests[] = $this->provideCommandRequest($item); | ||
} | ||
|
||
foreach ($commandRequests as $commandRequest) { | ||
$validationResult = $this->validator->validate($commandRequest); | ||
|
||
if (0 === count($validationResult)) { | ||
$commandsToExecute[] = $commandRequest->getCommand(); | ||
} | ||
|
||
$validationResults[] = $validationResult; | ||
} | ||
|
||
if (!$this->isValid($validationResults)) { | ||
/** @var ValidationErrorView $errorMessage */ | ||
$errorMessage = new $this->validationErrorViewClass(); | ||
|
||
$errorMessage->code = Response::HTTP_BAD_REQUEST; | ||
$errorMessage->message = 'Validation failed'; | ||
|
||
foreach ($validationResults as $validationResult) { | ||
$errors = []; | ||
|
||
/** @var ConstraintViolationInterface $result */ | ||
foreach ($validationResult as $result) { | ||
$errors[$result->getPropertyPath()][] = $result->getMessage(); | ||
} | ||
|
||
$errorMessage->errors[] = $errors; | ||
} | ||
|
||
return $this->viewHandler->handle(View::create($errorMessage, Response::HTTP_BAD_REQUEST)); | ||
} | ||
|
||
foreach ($commandsToExecute as $commandToExecute) { | ||
$this->bus->handle($commandToExecute); | ||
} | ||
|
||
try { | ||
return $this->viewHandler->handle( | ||
View::create($this->cartQuery->getOneByToken($token), Response::HTTP_CREATED) | ||
); | ||
} catch (\InvalidArgumentException $exception) { | ||
throw new BadRequestHttpException($exception->getMessage()); | ||
} | ||
} | ||
|
||
/** | ||
* @param array $item | ||
* | ||
* @return PutOptionBasedConfigurableItemToCartRequest|PutSimpleItemToCartRequest|PutVariantBasedConfigurableItemToCartRequest | ||
*/ | ||
private function provideCommandRequest(array $item) | ||
{ | ||
if (!isset($item['variantCode']) && !isset($item['options'])) { | ||
return PutSimpleItemToCartRequest::fromArray($item); | ||
} | ||
|
||
if (isset($item['variantCode']) && !isset($item['options'])) { | ||
return PutVariantBasedConfigurableItemToCartRequest::fromArray($item); | ||
} | ||
|
||
if (!isset($item['variantCode']) && isset($item['options'])) { | ||
return PutOptionBasedConfigurableItemToCartRequest::fromArray($item); | ||
} | ||
|
||
throw new NotFoundHttpException('Variant not found for given configuration'); | ||
} | ||
|
||
private function isValid(array $validationResults): bool | ||
{ | ||
foreach ($validationResults as $validationResult) { | ||
if (0 !== count($validationResult)) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |
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
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
45 changes: 45 additions & 0 deletions
45
src/Resources/config/validation/PutOptionBasedConfigutableItemToCartRequest.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,45 @@ | ||
<?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\ShopApiPlugin\Request\PutOptionBasedConfigurableItemToCartRequest"> | ||
<property name="token"> | ||
<constraint name="NotNull"> | ||
<option name="message">sylius.shop_api.token.not_null</option> | ||
</constraint> | ||
<constraint name="Sylius\ShopApiPlugin\Validator\Constraints\CartExists" /> | ||
</property> | ||
<property name="quantity"> | ||
<constraint name="Type"> | ||
<option name="type">integer</option> | ||
<option name="message">sylius.shop_api.quantity.type_integer</option> | ||
</constraint> | ||
|
||
<constraint name="GreaterThan"> | ||
<option name="value">0</option> | ||
<option name="message">sylius.shop_api.quantity.greater_than_0</option> | ||
</constraint> | ||
|
||
<constraint name="NotNull"> | ||
<option name="message">sylius.shop_api.quantity.not_null</option> | ||
</constraint> | ||
</property> | ||
<property name="productCode"> | ||
<constraint name="NotNull"> | ||
<option name="message">sylius.shop_api.product.not_null</option> | ||
</constraint> | ||
<constraint name="Sylius\ShopApiPlugin\Validator\Constraints\ProductExists" /> | ||
<constraint name="Sylius\ShopApiPlugin\Validator\Constraints\ConfigurableProduct" /> | ||
</property> | ||
</class> | ||
</constraint-mapping> |
Oops, something went wrong.