| @@ 19-87 (lines=69) @@ | ||
| 16 | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
|
| 17 | use Symfony\Component\Validator\Validator\ValidatorInterface; |
|
| 18 | ||
| 19 | final class RemoveAddressAction |
|
| 20 | { |
|
| 21 | /** |
|
| 22 | * @var ViewHandlerInterface |
|
| 23 | */ |
|
| 24 | private $viewHandler; |
|
| 25 | ||
| 26 | /** |
|
| 27 | * @var ValidatorInterface |
|
| 28 | */ |
|
| 29 | private $validator; |
|
| 30 | ||
| 31 | /** |
|
| 32 | * @var ValidationErrorViewFactory |
|
| 33 | */ |
|
| 34 | private $validationErrorViewFactory; |
|
| 35 | ||
| 36 | /** |
|
| 37 | * @var CommandBus |
|
| 38 | */ |
|
| 39 | private $bus; |
|
| 40 | ||
| 41 | /** |
|
| 42 | * @var TokenStorageInterface |
|
| 43 | */ |
|
| 44 | private $tokenStorage; |
|
| 45 | ||
| 46 | /** |
|
| 47 | * @param ViewHandlerInterface $viewHandler |
|
| 48 | * @param ValidatorInterface $validator |
|
| 49 | * @param ValidationErrorViewFactory $validationErrorViewFactory |
|
| 50 | * @param CommandBus $bus |
|
| 51 | * @param TokenStorageInterface $tokenStorage |
|
| 52 | */ |
|
| 53 | public function __construct( |
|
| 54 | ViewHandlerInterface $viewHandler, |
|
| 55 | ValidatorInterface $validator, |
|
| 56 | ValidationErrorViewFactory $validationErrorViewFactory, |
|
| 57 | CommandBus $bus, |
|
| 58 | TokenStorageInterface $tokenStorage |
|
| 59 | ) { |
|
| 60 | $this->viewHandler = $viewHandler; |
|
| 61 | $this->validator = $validator; |
|
| 62 | $this->validationErrorViewFactory = $validationErrorViewFactory; |
|
| 63 | $this->bus = $bus; |
|
| 64 | $this->tokenStorage = $tokenStorage; |
|
| 65 | } |
|
| 66 | ||
| 67 | public function __invoke(Request $request): Response |
|
| 68 | { |
|
| 69 | $removeAddressRequest = new RemoveAddressRequest($request); |
|
| 70 | ||
| 71 | $validationResults = $this->validator->validate($removeAddressRequest); |
|
| 72 | ||
| 73 | if (0 !== count($validationResults)) { |
|
| 74 | return $this->viewHandler->handle(View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST)); |
|
| 75 | } |
|
| 76 | ||
| 77 | /** @var ShopUserInterface $shopUser */ |
|
| 78 | $shopUser = $this->tokenStorage->getToken()->getUser(); |
|
| 79 | ||
| 80 | $this->bus->handle(new RemoveAddress( |
|
| 81 | $request->attributes->get('id'), |
|
| 82 | $shopUser->getEmail() |
|
| 83 | )); |
|
| 84 | ||
| 85 | return $this->viewHandler->handle(View::create('', Response::HTTP_NO_CONTENT)); |
|
| 86 | } |
|
| 87 | } |
|
| 88 | ||
| @@ 18-88 (lines=71) @@ | ||
| 15 | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
|
| 16 | use Symfony\Component\Validator\Validator\ValidatorInterface; |
|
| 17 | ||
| 18 | final class SetDefaultAddressAction |
|
| 19 | { |
|
| 20 | /** |
|
| 21 | * @var ViewHandlerInterface |
|
| 22 | */ |
|
| 23 | private $viewHandler; |
|
| 24 | ||
| 25 | /** |
|
| 26 | * @var CommandBus |
|
| 27 | */ |
|
| 28 | private $bus; |
|
| 29 | ||
| 30 | /** |
|
| 31 | * @var ValidatorInterface |
|
| 32 | */ |
|
| 33 | private $validator; |
|
| 34 | ||
| 35 | /** |
|
| 36 | * @var ValidationErrorViewFactoryInterface |
|
| 37 | */ |
|
| 38 | private $validationErrorViewFactory; |
|
| 39 | ||
| 40 | /** |
|
| 41 | * @var TokenStorageInterface |
|
| 42 | */ |
|
| 43 | private $tokenStorage; |
|
| 44 | ||
| 45 | /** |
|
| 46 | * @param ViewHandlerInterface $viewHandler |
|
| 47 | * @param CommandBus $bus |
|
| 48 | * @param ValidatorInterface $validator |
|
| 49 | * @param ValidationErrorViewFactoryInterface $validationErrorViewFactory |
|
| 50 | * @param TokenStorageInterface $tokenStorage |
|
| 51 | */ |
|
| 52 | public function __construct( |
|
| 53 | ViewHandlerInterface $viewHandler, |
|
| 54 | CommandBus $bus, |
|
| 55 | ValidatorInterface $validator, |
|
| 56 | ValidationErrorViewFactoryInterface $validationErrorViewFactory, |
|
| 57 | TokenStorageInterface $tokenStorage |
|
| 58 | ) { |
|
| 59 | $this->viewHandler = $viewHandler; |
|
| 60 | $this->bus = $bus; |
|
| 61 | $this->validator = $validator; |
|
| 62 | $this->validationErrorViewFactory = $validationErrorViewFactory; |
|
| 63 | $this->tokenStorage = $tokenStorage; |
|
| 64 | } |
|
| 65 | ||
| 66 | /** |
|
| 67 | * @param Request $request |
|
| 68 | * |
|
| 69 | * @return Response |
|
| 70 | */ |
|
| 71 | public function __invoke(Request $request) |
|
| 72 | { |
|
| 73 | $setDefaultAddressRequest = new SetDefaultAddressRequest($request); |
|
| 74 | ||
| 75 | $validationResults = $this->validator->validate($setDefaultAddressRequest); |
|
| 76 | ||
| 77 | if (0 !== count($validationResults)) { |
|
| 78 | return $this->viewHandler->handle(View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST)); |
|
| 79 | } |
|
| 80 | ||
| 81 | $this->bus->handle(new SetDefaultAddress( |
|
| 82 | $request->attributes->get('id'), |
|
| 83 | $this->tokenStorage->getToken()->getUser()->getEmail() |
|
| 84 | )); |
|
| 85 | ||
| 86 | return $this->viewHandler->handle(View::create(null, Response::HTTP_NO_CONTENT)); |
|
| 87 | } |
|
| 88 | } |
|
| 89 | ||