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