| @@ 19-77 (lines=59) @@ | ||
| 16 | use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
|
| 17 | use Symfony\Component\Validator\Validator\ValidatorInterface; |
|
| 18 | ||
| 19 | final class AddCouponAction |
|
| 20 | { |
|
| 21 | /** @var ViewHandlerInterface */ |
|
| 22 | private $viewHandler; |
|
| 23 | ||
| 24 | /** @var CommandBus */ |
|
| 25 | private $bus; |
|
| 26 | ||
| 27 | /** @var ValidatorInterface */ |
|
| 28 | private $validator; |
|
| 29 | ||
| 30 | /** @var ValidationErrorViewFactoryInterface */ |
|
| 31 | private $validationErrorViewFactory; |
|
| 32 | ||
| 33 | /** @var CartViewRepositoryInterface */ |
|
| 34 | private $cartQuery; |
|
| 35 | ||
| 36 | public function __construct( |
|
| 37 | ViewHandlerInterface $viewHandler, |
|
| 38 | CommandBus $bus, |
|
| 39 | ValidatorInterface $validator, |
|
| 40 | ValidationErrorViewFactoryInterface $validationErrorViewFactory, |
|
| 41 | CartViewRepositoryInterface $cartQuery |
|
| 42 | ) { |
|
| 43 | $this->viewHandler = $viewHandler; |
|
| 44 | $this->bus = $bus; |
|
| 45 | $this->validator = $validator; |
|
| 46 | $this->validationErrorViewFactory = $validationErrorViewFactory; |
|
| 47 | $this->cartQuery = $cartQuery; |
|
| 48 | } |
|
| 49 | ||
| 50 | /** |
|
| 51 | * @param Request $request |
|
| 52 | * |
|
| 53 | * @return Response |
|
| 54 | */ |
|
| 55 | public function __invoke(Request $request): Response |
|
| 56 | { |
|
| 57 | $addCouponRequest = new AddCouponRequest($request); |
|
| 58 | ||
| 59 | $validationResults = $this->validator->validate($addCouponRequest); |
|
| 60 | ||
| 61 | if (0 === count($validationResults)) { |
|
| 62 | $addCouponCommand = $addCouponRequest->getCommand(); |
|
| 63 | ||
| 64 | $this->bus->handle($addCouponCommand); |
|
| 65 | ||
| 66 | try { |
|
| 67 | return $this->viewHandler->handle( |
|
| 68 | View::create($this->cartQuery->getOneByToken($addCouponCommand->orderToken()), Response::HTTP_OK) |
|
| 69 | ); |
|
| 70 | } catch (\InvalidArgumentException $exception) { |
|
| 71 | throw new BadRequestHttpException($exception->getMessage()); |
|
| 72 | } |
|
| 73 | } |
|
| 74 | ||
| 75 | return $this->viewHandler->handle(View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST)); |
|
| 76 | } |
|
| 77 | } |
|
| 78 | ||
| @@ 16-69 (lines=54) @@ | ||
| 13 | use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
|
| 14 | use Symfony\Component\Validator\Validator\ValidatorInterface; |
|
| 15 | ||
| 16 | final class ChangeItemQuantityAction |
|
| 17 | { |
|
| 18 | /** @var ViewHandlerInterface */ |
|
| 19 | private $viewHandler; |
|
| 20 | ||
| 21 | /** @var CommandBus */ |
|
| 22 | private $bus; |
|
| 23 | ||
| 24 | /** @var ValidatorInterface */ |
|
| 25 | private $validator; |
|
| 26 | ||
| 27 | /** @var ValidationErrorViewFactoryInterface */ |
|
| 28 | private $validationErrorViewFactory; |
|
| 29 | ||
| 30 | /** @var CartViewRepositoryInterface */ |
|
| 31 | private $cartQuery; |
|
| 32 | ||
| 33 | public function __construct( |
|
| 34 | ViewHandlerInterface $viewHandler, |
|
| 35 | CommandBus $bus, |
|
| 36 | ValidatorInterface $validator, |
|
| 37 | ValidationErrorViewFactoryInterface $validationErrorViewFactory, |
|
| 38 | CartViewRepositoryInterface $cartQuery |
|
| 39 | ) { |
|
| 40 | $this->viewHandler = $viewHandler; |
|
| 41 | $this->bus = $bus; |
|
| 42 | $this->validator = $validator; |
|
| 43 | $this->validationErrorViewFactory = $validationErrorViewFactory; |
|
| 44 | $this->cartQuery = $cartQuery; |
|
| 45 | } |
|
| 46 | ||
| 47 | public function __invoke(Request $request): Response |
|
| 48 | { |
|
| 49 | $changeItemQuantityRequest = new ChangeItemQuantityRequest($request); |
|
| 50 | ||
| 51 | $validationResults = $this->validator->validate($changeItemQuantityRequest); |
|
| 52 | ||
| 53 | if (0 !== count($validationResults)) { |
|
| 54 | return $this->viewHandler->handle(View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST)); |
|
| 55 | } |
|
| 56 | ||
| 57 | $changeItemQuantityCommand = $changeItemQuantityRequest->getCommand(); |
|
| 58 | ||
| 59 | $this->bus->handle($changeItemQuantityCommand); |
|
| 60 | ||
| 61 | try { |
|
| 62 | return $this->viewHandler->handle( |
|
| 63 | View::create($this->cartQuery->getOneByToken($changeItemQuantityCommand->orderToken()), Response::HTTP_OK) |
|
| 64 | ); |
|
| 65 | } catch (\InvalidArgumentException $exception) { |
|
| 66 | throw new BadRequestHttpException($exception->getMessage()); |
|
| 67 | } |
|
| 68 | } |
|
| 69 | } |
|
| 70 | ||
| @@ 23-76 (lines=54) @@ | ||
| 20 | use Symfony\Component\Validator\ConstraintViolationInterface; |
|
| 21 | use Symfony\Component\Validator\Validator\ValidatorInterface; |
|
| 22 | ||
| 23 | final class PickupAction |
|
| 24 | { |
|
| 25 | /** @var ViewHandlerInterface */ |
|
| 26 | private $viewHandler; |
|
| 27 | ||
| 28 | /** @var CommandBus */ |
|
| 29 | private $bus; |
|
| 30 | ||
| 31 | /** @var ValidatorInterface */ |
|
| 32 | private $validator; |
|
| 33 | ||
| 34 | /** @var ValidationErrorViewFactoryInterface */ |
|
| 35 | private $validationErrorViewFactory; |
|
| 36 | ||
| 37 | /** @var CartViewRepositoryInterface */ |
|
| 38 | private $cartQuery; |
|
| 39 | ||
| 40 | public function __construct( |
|
| 41 | ViewHandlerInterface $viewHandler, |
|
| 42 | CommandBus $bus, |
|
| 43 | ValidatorInterface $validator, |
|
| 44 | ValidationErrorViewFactoryInterface $validationErrorViewFactory, |
|
| 45 | CartViewRepositoryInterface $cartQuery |
|
| 46 | ) { |
|
| 47 | $this->viewHandler = $viewHandler; |
|
| 48 | $this->bus = $bus; |
|
| 49 | $this->validator = $validator; |
|
| 50 | $this->validationErrorViewFactory = $validationErrorViewFactory; |
|
| 51 | $this->cartQuery = $cartQuery; |
|
| 52 | } |
|
| 53 | ||
| 54 | public function __invoke(Request $request): Response |
|
| 55 | { |
|
| 56 | $pickupRequest = new PickupCartRequest($request); |
|
| 57 | ||
| 58 | $validationResults = $this->validator->validate($pickupRequest); |
|
| 59 | ||
| 60 | if (0 === count($validationResults)) { |
|
| 61 | $pickupCartCommand = $pickupRequest->getCommand(); |
|
| 62 | ||
| 63 | $this->bus->handle($pickupCartCommand); |
|
| 64 | ||
| 65 | try { |
|
| 66 | return $this->viewHandler->handle( |
|
| 67 | View::create($this->cartQuery->getOneByToken($pickupCartCommand->orderToken()), Response::HTTP_CREATED) |
|
| 68 | ); |
|
| 69 | } catch (\InvalidArgumentException $exception) { |
|
| 70 | throw new BadRequestHttpException($exception->getMessage()); |
|
| 71 | } |
|
| 72 | } |
|
| 73 | ||
| 74 | return $this->viewHandler->handle(View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST)); |
|
| 75 | } |
|
| 76 | } |
|
| 77 | ||
| @@ 18-76 (lines=59) @@ | ||
| 15 | use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
|
| 16 | use Symfony\Component\Validator\Validator\ValidatorInterface; |
|
| 17 | ||
| 18 | final class RemoveCouponAction |
|
| 19 | { |
|
| 20 | /** @var ViewHandlerInterface */ |
|
| 21 | private $viewHandler; |
|
| 22 | ||
| 23 | /** @var CommandBus */ |
|
| 24 | private $bus; |
|
| 25 | ||
| 26 | /** @var ValidatorInterface */ |
|
| 27 | private $validator; |
|
| 28 | ||
| 29 | /** @var ValidationErrorViewFactoryInterface */ |
|
| 30 | private $validationErrorViewFactory; |
|
| 31 | ||
| 32 | /** @var CartViewRepositoryInterface */ |
|
| 33 | private $cartQuery; |
|
| 34 | ||
| 35 | public function __construct( |
|
| 36 | ViewHandlerInterface $viewHandler, |
|
| 37 | CommandBus $bus, |
|
| 38 | ValidatorInterface $validator, |
|
| 39 | ValidationErrorViewFactoryInterface $validationErrorViewFactory, |
|
| 40 | CartViewRepositoryInterface $cartQuery |
|
| 41 | ) { |
|
| 42 | $this->viewHandler = $viewHandler; |
|
| 43 | $this->bus = $bus; |
|
| 44 | $this->validator = $validator; |
|
| 45 | $this->validationErrorViewFactory = $validationErrorViewFactory; |
|
| 46 | $this->cartQuery = $cartQuery; |
|
| 47 | } |
|
| 48 | ||
| 49 | /** |
|
| 50 | * @param Request $request |
|
| 51 | * |
|
| 52 | * @return Response |
|
| 53 | */ |
|
| 54 | public function __invoke(Request $request): Response |
|
| 55 | { |
|
| 56 | $removeCouponRequest = new RemoveCouponRequest($request); |
|
| 57 | ||
| 58 | $validationResults = $this->validator->validate($removeCouponRequest); |
|
| 59 | ||
| 60 | if (0 === count($validationResults)) { |
|
| 61 | $removeCouponCommand = $removeCouponRequest->getCommand(); |
|
| 62 | ||
| 63 | $this->bus->handle($removeCouponCommand); |
|
| 64 | ||
| 65 | try { |
|
| 66 | return $this->viewHandler->handle( |
|
| 67 | View::create($this->cartQuery->getOneByToken($removeCouponCommand->orderToken()), Response::HTTP_OK) |
|
| 68 | ); |
|
| 69 | } catch (\InvalidArgumentException $exception) { |
|
| 70 | throw new BadRequestHttpException($exception->getMessage()); |
|
| 71 | } |
|
| 72 | } |
|
| 73 | ||
| 74 | return $this->viewHandler->handle(View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST)); |
|
| 75 | } |
|
| 76 | } |
|
| 77 | ||