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