From 7797a084c693164e57dce6ce45fd48a1b0f787d9 Mon Sep 17 00:00:00 2001 From: mamazu Date: Mon, 27 Aug 2018 17:50:13 +0200 Subject: [PATCH 1/9] Added more precise response codes --- spec/Provider/LoggedInUserProviderSpec.php | 46 ++++++++++++ spec/Validator/AddressExistsValidatorSpec.php | 8 +-- .../AddressBook/CreateAddressAction.php | 53 ++++++++------ .../AddressBook/RemoveAddressAction.php | 58 ++++++++------- .../AddressBook/SetDefaultAddressAction.php | 56 ++++++++------- .../AddressBook/ShowAddressBookAction.php | 46 +++++++----- .../AddressBook/UpdateAddressAction.php | 71 +++++++++++-------- ...rProvider.php => LoggedInUserProvider.php} | 14 ++-- ....php => LoggedInUserProviderInterface.php} | 2 +- src/Resources/config/services.xml | 3 +- .../config/services/actions/address_book.xml | 6 +- src/Validator/AddressExistsValidator.php | 10 +-- tests/Controller/AddressBookShowApiTest.php | 12 ++++ ...ow_address_book_unauthorized_response.json | 1 + 14 files changed, 239 insertions(+), 147 deletions(-) create mode 100644 spec/Provider/LoggedInUserProviderSpec.php rename src/Provider/{CurrentUserProvider.php => LoggedInUserProvider.php} (58%) rename src/Provider/{CurrentUserProviderInterface.php => LoggedInUserProviderInterface.php} (81%) create mode 100644 tests/Responses/Expected/address_book/show_address_book_unauthorized_response.json diff --git a/spec/Provider/LoggedInUserProviderSpec.php b/spec/Provider/LoggedInUserProviderSpec.php new file mode 100644 index 000000000..80ee4304c --- /dev/null +++ b/spec/Provider/LoggedInUserProviderSpec.php @@ -0,0 +1,46 @@ +beConstructedWith($tokenStorage); + } + + function it_is_reviewer_subject_provider() + { + $this->shouldImplement(LoggedInUserProviderInterface::class); + } + + function it_throws_an_error_if_there_is_no_user_logged_in( + TokenStorageInterface $tokenStorage, + TokenInterface $token + ) { + $token->getUser()->shouldBeCalled()->willReturn(null); + $tokenStorage->getToken()->shouldBeCalled()->willReturn($token); + + $this->shouldThrow(TokenNotFoundException::class)->during('provide'); + } + + function it_returns_the_logged_in_user_if_there_is_one( + TokenStorageInterface $tokenStorage, + TokenInterface $token, + ShopUserInterface $shopUser + ) + { + $token->getUser()->shouldBeCalled()->willReturn($shopUser); + $tokenStorage->getToken()->shouldBeCalled()->willReturn($token); + + $this->provide()->shouldReturn($shopUser); + } +} diff --git a/spec/Validator/AddressExistsValidatorSpec.php b/spec/Validator/AddressExistsValidatorSpec.php index 21fe02e7e..79030027c 100644 --- a/spec/Validator/AddressExistsValidatorSpec.php +++ b/spec/Validator/AddressExistsValidatorSpec.php @@ -10,7 +10,7 @@ use Sylius\Component\Core\Model\CustomerInterface; use Sylius\Component\Core\Model\ShopUserInterface; use Sylius\Component\Core\Repository\AddressRepositoryInterface; -use Sylius\ShopApiPlugin\Provider\CurrentUserProviderInterface; +use Sylius\ShopApiPlugin\Provider\LoggedInUserProviderInterface; use Sylius\ShopApiPlugin\Validator\Constraints\AddressExists; use Symfony\Component\Validator\Context\ExecutionContextInterface; @@ -19,7 +19,7 @@ final class AddressExistsValidatorSpec extends ObjectBehavior function let( ExecutionContextInterface $executionContext, AddressRepositoryInterface $addressRepository, - CurrentUserProviderInterface $currentUserProvider + LoggedInUserProviderInterface $currentUserProvider ) { $this->beConstructedWith($addressRepository, $currentUserProvider); @@ -30,7 +30,7 @@ function it_does_not_add_constraint_if_address_exists_and_its_owned_by_current_u AddressInterface $address, ShopUserInterface $shopUser, CustomerInterface $customerOwner, - CurrentUserProviderInterface $currentUserProvider, + LoggedInUserProviderInterface $currentUserProvider, AddressRepositoryInterface $addressRepository, ExecutionContextInterface $executionContext ) { @@ -61,7 +61,7 @@ function it_adds_constraint_if_address_does_not_exits_exists( function it_adds_constraint_if_current_user_is_not_address_owner( AddressInterface $address, AddressRepositoryInterface $addressRepository, - CurrentUserProviderInterface $currentUserProvider, + LoggedInUserProviderInterface $currentUserProvider, ShopUserInterface $shopUser, CustomerInterface $customerOwner, ExecutionContextInterface $executionContext diff --git a/src/Controller/AddressBook/CreateAddressAction.php b/src/Controller/AddressBook/CreateAddressAction.php index c9e9e1d9d..0b805d328 100644 --- a/src/Controller/AddressBook/CreateAddressAction.php +++ b/src/Controller/AddressBook/CreateAddressAction.php @@ -7,13 +7,14 @@ use FOS\RestBundle\View\View; use FOS\RestBundle\View\ViewHandlerInterface; use League\Tactician\CommandBus; +use Sylius\Component\Core\Model\ShopUserInterface; use Sylius\ShopApiPlugin\Command\CreateAddress; use Sylius\ShopApiPlugin\Factory\ValidationErrorViewFactoryInterface; use Sylius\ShopApiPlugin\Model\Address; -use Sylius\ShopApiPlugin\Provider\CurrentUserProviderInterface; +use Sylius\ShopApiPlugin\Provider\LoggedInUserProviderInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; -use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; +use Symfony\Component\Security\Core\Exception\TokenNotFoundException; use Symfony\Component\Validator\Validator\ValidatorInterface; final class CreateAddressAction @@ -39,36 +40,29 @@ final class CreateAddressAction private $validationErrorViewFactory; /** - * @var TokenStorage - */ - private $tokenStorage; - /** - * @var CurrentUserProviderInterface + * @var LoggedInUserProviderInterface */ private $currentUserProvider; /** - * @param ViewHandlerInterface $viewHandler - * @param CommandBus $bus - * @param ValidatorInterface $validator + * @param ViewHandlerInterface $viewHandler + * @param CommandBus $bus + * @param ValidatorInterface $validator * @param ValidationErrorViewFactoryInterface $validationErrorViewFactory - * @param TokenStorage $tokenStorage - * @param CurrentUserProviderInterface $currentUserProvider + * @param LoggedInUserProviderInterface $currentUserProvider */ public function __construct( ViewHandlerInterface $viewHandler, CommandBus $bus, ValidatorInterface $validator, ValidationErrorViewFactoryInterface $validationErrorViewFactory, - TokenStorage $tokenStorage, - CurrentUserProviderInterface $currentUserProvider + LoggedInUserProviderInterface $currentUserProvider ) { - $this->viewHandler = $viewHandler; - $this->bus = $bus; - $this->validator = $validator; + $this->viewHandler = $viewHandler; + $this->bus = $bus; + $this->validator = $validator; $this->validationErrorViewFactory = $validationErrorViewFactory; - $this->tokenStorage = $tokenStorage; - $this->currentUserProvider = $currentUserProvider; + $this->currentUserProvider = $currentUserProvider; } /** @@ -83,13 +77,26 @@ public function __invoke(Request $request): Response $validationResults = $this->validator->validate($addressModel); if (0 !== count($validationResults)) { - return $this->viewHandler->handle(View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST)); + return $this->viewHandler->handle( + View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST) + ); } - $user = $this->currentUserProvider->provide(); + try { + /** @var ShopUserInterface $user */ + $user = $this->currentUserProvider->provide(); + } catch (TokenNotFoundException $exception) { + return $this->viewHandler->handle(View::create(null, Response::HTTP_UNAUTHORIZED)); + } - $this->bus->handle(new CreateAddress($addressModel, $user->getEmail())); - return $this->viewHandler->handle(View::create(null, Response::HTTP_NO_CONTENT)); + if ($user->getCustomer() !== null) { + $this->bus->handle(new CreateAddress($addressModel, $user->getEmail())); + $view = View::create(null, Response::HTTP_NO_CONTENT); + } else { + $view = View::create(['error' => 'The user has is not a customer'], Response::HTTP_BAD_REQUEST); + } + + return $this->viewHandler->handle($view); } } diff --git a/src/Controller/AddressBook/RemoveAddressAction.php b/src/Controller/AddressBook/RemoveAddressAction.php index 75cce757a..0d6381215 100644 --- a/src/Controller/AddressBook/RemoveAddressAction.php +++ b/src/Controller/AddressBook/RemoveAddressAction.php @@ -7,13 +7,15 @@ use FOS\RestBundle\View\View; use FOS\RestBundle\View\ViewHandlerInterface; use League\Tactician\CommandBus; +use Sylius\Component\Core\Model\ShopUserInterface; use Sylius\ShopApiPlugin\Command\RemoveAddress; use Sylius\ShopApiPlugin\Factory\ValidationErrorViewFactory; -use Sylius\ShopApiPlugin\Provider\CurrentUserProviderInterface; +use Sylius\ShopApiPlugin\Provider\LoggedInUserProviderInterface; use Sylius\ShopApiPlugin\Request\RemoveAddressRequest; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; +use Symfony\Component\Security\Core\Exception\TokenNotFoundException; use Symfony\Component\Validator\Validator\ValidatorInterface; final class RemoveAddressAction @@ -39,37 +41,30 @@ final class RemoveAddressAction private $bus; /** - * @var TokenStorageInterface - */ - private $tokenStorage; - - /** - * @var CurrentUserProviderInterface + * @var LoggedInUserProviderInterface */ private $currentUserProvider; /** - * @param ViewHandlerInterface $viewHandler - * @param ValidatorInterface $validator - * @param ValidationErrorViewFactory $validationErrorViewFactory - * @param CommandBus $bus - * @param TokenStorageInterface $tokenStorage - * @param CurrentUserProviderInterface $currentUserProvider + * @param ViewHandlerInterface $viewHandler + * @param ValidatorInterface $validator + * @param ValidationErrorViewFactory $validationErrorViewFactory + * @param CommandBus $bus + * @param TokenStorageInterface $tokenStorage + * @param LoggedInUserProviderInterface $currentUserProvider */ public function __construct( ViewHandlerInterface $viewHandler, ValidatorInterface $validator, ValidationErrorViewFactory $validationErrorViewFactory, CommandBus $bus, - TokenStorageInterface $tokenStorage, - CurrentUserProviderInterface $currentUserProvider + LoggedInUserProviderInterface $currentUserProvider ) { - $this->viewHandler = $viewHandler; - $this->validator = $validator; + $this->viewHandler = $viewHandler; + $this->validator = $validator; $this->validationErrorViewFactory = $validationErrorViewFactory; - $this->bus = $bus; - $this->tokenStorage = $tokenStorage; - $this->currentUserProvider = $currentUserProvider; + $this->bus = $bus; + $this->currentUserProvider = $currentUserProvider; } public function __invoke(Request $request): Response @@ -79,16 +74,25 @@ public function __invoke(Request $request): Response $validationResults = $this->validator->validate($removeAddressRequest); if (0 !== count($validationResults)) { - return $this->viewHandler->handle(View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST)); + return $this->viewHandler->handle( + View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST) + ); } - $user = $this->currentUserProvider->provide(); + try { + /** @var ShopUserInterface $user */ + $user = $this->currentUserProvider->provide(); + } catch (TokenNotFoundException $exception) { + return $this->viewHandler->handle(View::create(null, Response::HTTP_UNAUTHORIZED)); + } - $this->bus->handle(new RemoveAddress( - $request->attributes->get('id'), - $user->getEmail() - )); + if ($user->getCustomer() !== null) { + $this->bus->handle(new RemoveAddress($request->attributes->get('id'), $user->getEmail())); + $view = View::create(null, Response::HTTP_NO_CONTENT); + } else { + $view = View::create(['error' => 'The user has is not a customer'], Response::HTTP_BAD_REQUEST); + } - return $this->viewHandler->handle(View::create('', Response::HTTP_NO_CONTENT)); + return $this->viewHandler->handle($view); } } diff --git a/src/Controller/AddressBook/SetDefaultAddressAction.php b/src/Controller/AddressBook/SetDefaultAddressAction.php index 51bef7f56..d7c8ef845 100644 --- a/src/Controller/AddressBook/SetDefaultAddressAction.php +++ b/src/Controller/AddressBook/SetDefaultAddressAction.php @@ -7,13 +7,15 @@ use FOS\RestBundle\View\View; use FOS\RestBundle\View\ViewHandlerInterface; use League\Tactician\CommandBus; +use Sylius\Component\Core\Model\ShopUserInterface; use Sylius\ShopApiPlugin\Command\SetDefaultAddress; use Sylius\ShopApiPlugin\Factory\ValidationErrorViewFactoryInterface; -use Sylius\ShopApiPlugin\Provider\CurrentUserProviderInterface; +use Sylius\ShopApiPlugin\Provider\LoggedInUserProviderInterface; use Sylius\ShopApiPlugin\Request\SetDefaultAddressRequest; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; +use Symfony\Component\Security\Core\Exception\TokenNotFoundException; use Symfony\Component\Validator\Validator\ValidatorInterface; final class SetDefaultAddressAction @@ -39,36 +41,30 @@ final class SetDefaultAddressAction private $validationErrorViewFactory; /** - * @var TokenStorageInterface - */ - private $tokenStorage; - /** - * @var CurrentUserProviderInterface + * @var LoggedInUserProviderInterface */ private $currentUserProvider; /** - * @param ViewHandlerInterface $viewHandler - * @param CommandBus $bus - * @param ValidatorInterface $validator + * @param ViewHandlerInterface $viewHandler + * @param CommandBus $bus + * @param ValidatorInterface $validator * @param ValidationErrorViewFactoryInterface $validationErrorViewFactory - * @param TokenStorageInterface $tokenStorage - * @param CurrentUserProviderInterface $currentUserProvider + * @param TokenStorageInterface $tokenStorage + * @param LoggedInUserProviderInterface $currentUserProvider */ public function __construct( ViewHandlerInterface $viewHandler, CommandBus $bus, ValidatorInterface $validator, ValidationErrorViewFactoryInterface $validationErrorViewFactory, - TokenStorageInterface $tokenStorage, - CurrentUserProviderInterface $currentUserProvider + LoggedInUserProviderInterface $currentUserProvider ) { - $this->viewHandler = $viewHandler; - $this->bus = $bus; - $this->validator = $validator; + $this->viewHandler = $viewHandler; + $this->bus = $bus; + $this->validator = $validator; $this->validationErrorViewFactory = $validationErrorViewFactory; - $this->tokenStorage = $tokenStorage; - $this->currentUserProvider = $currentUserProvider; + $this->currentUserProvider = $currentUserProvider; } /** @@ -83,16 +79,26 @@ public function __invoke(Request $request) $validationResults = $this->validator->validate($setDefaultAddressRequest); if (0 !== count($validationResults)) { - return $this->viewHandler->handle(View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST)); + return $this->viewHandler->handle( + View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST) + ); } - $user = $this->currentUserProvider->provide(); + try { + /** @var ShopUserInterface $user */ + $user = $this->currentUserProvider->provide(); + } catch (TokenNotFoundException $exception) { + return $this->viewHandler->handle(View::create(null, Response::HTTP_UNAUTHORIZED)); + } - $this->bus->handle(new SetDefaultAddress( - $request->attributes->get('id'), - $user->getEmail() - )); + if ($user->getCustomer() !== null) { + $this->bus->handle(new SetDefaultAddress($request->attributes->get('id'), $user->getEmail())); + + $view = View::create(null, Response::HTTP_NO_CONTENT); + } else { + $view = View::create(['error' => 'The user has is not a customer'], Response::HTTP_BAD_REQUEST); + } - return $this->viewHandler->handle(View::create(null, Response::HTTP_NO_CONTENT)); + return $this->viewHandler->handle($view); } } diff --git a/src/Controller/AddressBook/ShowAddressBookAction.php b/src/Controller/AddressBook/ShowAddressBookAction.php index 0f03c793f..a48d72c02 100644 --- a/src/Controller/AddressBook/ShowAddressBookAction.php +++ b/src/Controller/AddressBook/ShowAddressBookAction.php @@ -6,13 +6,14 @@ use FOS\RestBundle\View\View; use FOS\RestBundle\View\ViewHandlerInterface; +use Sylius\Component\Core\Model\AddressInterface; use Sylius\Component\Core\Model\Customer; use Sylius\Component\Core\Model\ShopUserInterface; use Sylius\ShopApiPlugin\Factory\AddressBookViewFactoryInterface; +use Sylius\ShopApiPlugin\Provider\LoggedInUserProviderInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; -use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; -use Webmozart\Assert\Assert; +use Symfony\Component\Security\Core\Exception\TokenNotFoundException; final class ShowAddressBookAction { @@ -22,9 +23,9 @@ final class ShowAddressBookAction private $viewHandler; /** - * @var TokenStorageInterface + * @var LoggedInUserProviderInterface */ - private $tokenStorage; + private $currentUserProvider; /** * @var AddressBookViewFactoryInterface @@ -32,36 +33,43 @@ final class ShowAddressBookAction private $addressBookViewFactory; /** - * @param ViewHandlerInterface $viewHandler - * @param TokenStorageInterface $tokenStorage + * @param ViewHandlerInterface $viewHandler + * @param LoggedInUserProviderInterface $currentUserProvider * @param AddressBookViewFactoryInterface $addressBookViewFactory */ public function __construct( ViewHandlerInterface $viewHandler, - TokenStorageInterface $tokenStorage, + LoggedInUserProviderInterface $currentUserProvider, AddressBookViewFactoryInterface $addressBookViewFactory ) { - $this->viewHandler = $viewHandler; - $this->tokenStorage = $tokenStorage; + $this->viewHandler = $viewHandler; + $this->currentUserProvider = $currentUserProvider; $this->addressBookViewFactory = $addressBookViewFactory; } public function __invoke(Request $request): Response { - /** @var ShopUserInterface $user */ - $user = $this->tokenStorage->getToken()->getUser(); - - Assert::isInstanceOf($user, ShopUserInterface::class); + try { + /** @var ShopUserInterface $user */ + $user = $this->currentUserProvider->provide(); + } catch (TokenNotFoundException $exception) { + return $this->viewHandler->handle(View::create(null, Response::HTTP_UNAUTHORIZED)); + } - /** @var Customer $customer */ $customer = $user->getCustomer(); - $addresses = $customer->getAddresses(); + if ($customer instanceof Customer) { + + $addressViews = $customer->getAddresses()->map( + function (AddressInterface $address) use ($customer) { + return $this->addressBookViewFactory->create($address, $customer); + } + ); - $addressViews = []; - foreach ($addresses as $address) { - $addressViews[] = $this->addressBookViewFactory->create($address, $customer); + $view = View::create($addressViews, Response::HTTP_OK); + } else { + $view = View::create(['No customer associated to the user'], Response::HTTP_BAD_REQUEST); } - return $this->viewHandler->handle(View::create($addressViews, Response::HTTP_OK)); + return $this->viewHandler->handle($view); } } diff --git a/src/Controller/AddressBook/UpdateAddressAction.php b/src/Controller/AddressBook/UpdateAddressAction.php index af9c5caf4..e3233215f 100644 --- a/src/Controller/AddressBook/UpdateAddressAction.php +++ b/src/Controller/AddressBook/UpdateAddressAction.php @@ -8,15 +8,17 @@ use FOS\RestBundle\View\ViewHandlerInterface; use League\Tactician\CommandBus; use Sylius\Component\Core\Model\AddressInterface; +use Sylius\Component\Core\Model\ShopUserInterface; use Sylius\Component\Core\Repository\AddressRepositoryInterface; use Sylius\ShopApiPlugin\Command\UpdateAddress; use Sylius\ShopApiPlugin\Factory\AddressBookViewFactoryInterface; use Sylius\ShopApiPlugin\Factory\ValidationErrorViewFactoryInterface; use Sylius\ShopApiPlugin\Model\Address; -use Sylius\ShopApiPlugin\Provider\CurrentUserProviderInterface; +use Sylius\ShopApiPlugin\Provider\LoggedInUserProviderInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; +use Symfony\Component\Security\Core\Exception\TokenNotFoundException; use Symfony\Component\Validator\Validator\ValidatorInterface; final class UpdateAddressAction @@ -52,23 +54,19 @@ final class UpdateAddressAction private $addressRepository; /** - * @var TokenStorageInterface - */ - private $tokenStorage; - /** - * @var CurrentUserProviderInterface + * @var LoggedInUserProviderInterface */ private $currentUserProvider; /** - * @param ViewHandlerInterface $viewHandler - * @param ValidatorInterface $validator - * @param CommandBus $bus + * @param ViewHandlerInterface $viewHandler + * @param ValidatorInterface $validator + * @param CommandBus $bus * @param ValidationErrorViewFactoryInterface $validationErrorViewFactory - * @param AddressBookViewFactoryInterface $addressViewFactory - * @param AddressRepositoryInterface $addressRepository - * @param TokenStorageInterface $tokenStorage - * @param CurrentUserProviderInterface $currentUserProvider + * @param AddressBookViewFactoryInterface $addressViewFactory + * @param AddressRepositoryInterface $addressRepository + * @param TokenStorageInterface $tokenStorage + * @param LoggedInUserProviderInterface $currentUserProvider */ public function __construct( ViewHandlerInterface $viewHandler, @@ -77,17 +75,15 @@ public function __construct( ValidationErrorViewFactoryInterface $validationErrorViewFactory, AddressBookViewFactoryInterface $addressViewFactory, AddressRepositoryInterface $addressRepository, - TokenStorageInterface $tokenStorage, - CurrentUserProviderInterface $currentUserProvider + LoggedInUserProviderInterface $currentUserProvider ) { - $this->viewHandler = $viewHandler; - $this->validator = $validator; - $this->bus = $bus; + $this->viewHandler = $viewHandler; + $this->validator = $validator; + $this->bus = $bus; $this->validationErrorViewFactory = $validationErrorViewFactory; - $this->addressBookViewFactory = $addressViewFactory; - $this->addressRepository = $addressRepository; - $this->tokenStorage = $tokenStorage; - $this->currentUserProvider = $currentUserProvider; + $this->addressBookViewFactory = $addressViewFactory; + $this->addressRepository = $addressRepository; + $this->currentUserProvider = $currentUserProvider; } public function __invoke(Request $request, $id): Response @@ -97,19 +93,32 @@ public function __invoke(Request $request, $id): Response $validationResults = $this->validator->validate($addressModel); if (0 !== count($validationResults)) { - return $this->viewHandler->handle(View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST)); + return $this->viewHandler->handle( + View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST) + ); } - $user = $this->currentUserProvider->provide(); + try { + /** @var ShopUserInterface $user */ + $user = $this->currentUserProvider->provide(); + } catch (TokenNotFoundException $exception) { + return $this->viewHandler->handle(View::create(null, Response::HTTP_UNAUTHORIZED)); + } - $this->bus->handle(new UpdateAddress($addressModel, $user->getEmail(), $id)); + if ($user->getCustomer() !== null) { + $this->bus->handle(new UpdateAddress($addressModel, $user->getEmail(), $id)); - /** @var AddressInterface $updatedAddress */ - $updatedAddress = $this->addressRepository->findOneBy(['id' => $id]); + /** @var AddressInterface $updatedAddress */ + $updatedAddress = $this->addressRepository->findOneBy(['id' => $id]); + + $view = View::create( + $this->addressBookViewFactory->create($updatedAddress, $user->getCustomer()), + Response::HTTP_OK + ); + } else { + $view = View::create(['error' => 'The user has is not a customer'], Response::HTTP_BAD_REQUEST); + } - return $this->viewHandler->handle(View::create( - $this->addressBookViewFactory->create($updatedAddress, $user->getCustomer()), - Response::HTTP_OK) - ); + return $this->viewHandler->handle($view); } } diff --git a/src/Provider/CurrentUserProvider.php b/src/Provider/LoggedInUserProvider.php similarity index 58% rename from src/Provider/CurrentUserProvider.php rename to src/Provider/LoggedInUserProvider.php index b76302a0a..eb914c974 100644 --- a/src/Provider/CurrentUserProvider.php +++ b/src/Provider/LoggedInUserProvider.php @@ -5,17 +5,17 @@ namespace Sylius\ShopApiPlugin\Provider; use Sylius\Component\Core\Model\ShopUserInterface; -use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; -use Webmozart\Assert\Assert; +use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; +use Symfony\Component\Security\Core\Exception\TokenNotFoundException; -final class CurrentUserProvider implements CurrentUserProviderInterface +final class LoggedInUserProvider implements LoggedInUserProviderInterface { /** - * @var TokenStorage + * @var TokenStorageInterface */ private $tokenStorage; - public function __construct(TokenStorage $tokenStorage) + public function __construct(TokenStorageInterface $tokenStorage) { $this->tokenStorage = $tokenStorage; } @@ -28,7 +28,9 @@ public function provide(): ShopUserInterface /** @var ShopUserInterface $user */ $user = $this->tokenStorage->getToken()->getUser(); - Assert::isInstanceOf($user, ShopUserInterface::class); + if(!$user instanceof ShopUserInterface){ + throw new TokenNotFoundException(); + } return $user; } diff --git a/src/Provider/CurrentUserProviderInterface.php b/src/Provider/LoggedInUserProviderInterface.php similarity index 81% rename from src/Provider/CurrentUserProviderInterface.php rename to src/Provider/LoggedInUserProviderInterface.php index 2b553cda3..f16723abf 100644 --- a/src/Provider/CurrentUserProviderInterface.php +++ b/src/Provider/LoggedInUserProviderInterface.php @@ -6,7 +6,7 @@ use Sylius\Component\Core\Model\ShopUserInterface; -interface CurrentUserProviderInterface +interface LoggedInUserProviderInterface { public function provide(): ShopUserInterface; } diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index 650a2b4ce..002c12d31 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -58,7 +58,8 @@ - + diff --git a/src/Resources/config/services/actions/address_book.xml b/src/Resources/config/services/actions/address_book.xml index e15b11d21..861f2150a 100644 --- a/src/Resources/config/services/actions/address_book.xml +++ b/src/Resources/config/services/actions/address_book.xml @@ -11,14 +11,13 @@ - - + - - - diff --git a/src/Validator/AddressExistsValidator.php b/src/Validator/AddressExistsValidator.php index 0f5bbcb25..4e435ad1e 100644 --- a/src/Validator/AddressExistsValidator.php +++ b/src/Validator/AddressExistsValidator.php @@ -6,7 +6,7 @@ use Sylius\Component\Core\Model\AddressInterface; use Sylius\Component\Core\Repository\AddressRepositoryInterface; -use Sylius\ShopApiPlugin\Provider\CurrentUserProviderInterface; +use Sylius\ShopApiPlugin\Provider\LoggedInUserProviderInterface; use Sylius\ShopApiPlugin\Validator\Constraints\AddressExists; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; @@ -18,19 +18,19 @@ final class AddressExistsValidator extends ConstraintValidator */ private $addressRepository; /** - * @var CurrentUserProviderInterface + * @var LoggedInUserProviderInterface */ private $currentUserProvider; /** * AddressExistsValidator constructor. * - * @param AddressRepositoryInterface $addressRepository - * @param CurrentUserProviderInterface $currentUserProvider + * @param AddressRepositoryInterface $addressRepository + * @param LoggedInUserProviderInterface $currentUserProvider */ public function __construct( AddressRepositoryInterface $addressRepository, - CurrentUserProviderInterface $currentUserProvider + LoggedInUserProviderInterface $currentUserProvider ) { $this->addressRepository = $addressRepository; $this->currentUserProvider = $currentUserProvider; diff --git a/tests/Controller/AddressBookShowApiTest.php b/tests/Controller/AddressBookShowApiTest.php index 46cb03f6c..5eff2256f 100644 --- a/tests/Controller/AddressBookShowApiTest.php +++ b/tests/Controller/AddressBookShowApiTest.php @@ -24,4 +24,16 @@ public function it_shows_address_book() $this->assertResponse($response, 'address_book/show_address_book_response', Response::HTTP_OK); } + + /** + * @test + */ + public function it_returns_an_unauthorized_exception_if_there_is_no_logged_in_user() + { + $this->client->request('GET', '/shop-api/address-book', [], [], ['ACCEPT' => 'application/json']); + $response = $this->client->getResponse(); + + $this->assertResponse($response, 'address_book/show_address_book_unauthorized_response', Response::HTTP_UNAUTHORIZED); + } + } diff --git a/tests/Responses/Expected/address_book/show_address_book_unauthorized_response.json b/tests/Responses/Expected/address_book/show_address_book_unauthorized_response.json new file mode 100644 index 000000000..19765bd50 --- /dev/null +++ b/tests/Responses/Expected/address_book/show_address_book_unauthorized_response.json @@ -0,0 +1 @@ +null From 9ca1ebaeb79701002e9f04fd3e0249dc6dc604ff Mon Sep 17 00:00:00 2001 From: mamazu Date: Mon, 27 Aug 2018 18:58:42 +0200 Subject: [PATCH 2/9] Updated the documentaion and unified the responses --- doc/swagger.yml | 20 ++++++++++++++----- .../AddressBook/CreateAddressAction.php | 2 +- .../AddressBook/RemoveAddressAction.php | 2 +- .../AddressBook/SetDefaultAddressAction.php | 2 +- .../AddressBook/ShowAddressBookAction.php | 2 +- .../AddressBook/UpdateAddressAction.php | 2 +- 6 files changed, 20 insertions(+), 10 deletions(-) diff --git a/doc/swagger.yml b/doc/swagger.yml index 7e8609da4..b5a19573c 100644 --- a/doc/swagger.yml +++ b/doc/swagger.yml @@ -896,8 +896,10 @@ paths: description: "Successfully fetched Address Book" schema: $ref: "#/definitions/LoggedInCustomerAddressBook" + 401: + description: "No user is logged in" 500: - description: "User not logged in" + description: "The user is not a customer" security: - bearerAuth: [] post: @@ -918,8 +920,10 @@ paths: description: "Validation failed" schema: $ref: "#/definitions/GeneralError" + 401: + description: "No user is logged in" 500: - description: "No logged in user or the user does not own the address" + description: "The user does not own the address or the user is not a customer" security: - bearerAuth: [] /address-book/{id}: @@ -950,8 +954,10 @@ paths: description: "Validation failed" schema: $ref: "#/definitions/GeneralError" + 401: + description: "No user is logged in" 500: - description: "No logged in user or the user does not own the address" + description: "The user does not own the address or the user is not a customer" security: - bearerAuth: [] delete: @@ -973,8 +979,10 @@ paths: description: "Validation failed" schema: $ref: "#/definitions/GeneralError" + 401: + description: "No user is logged in" 500: - description: "No logged in user or the user does not own the address" + description: "The user does not own the address or the user is not a customer" security: - bearerAuth: [] /address-book/{id}/default: @@ -997,8 +1005,10 @@ paths: description: "Validation failed" schema: $ref: "#/definitions/GeneralError" + 401: + description: "No user is logged in" 500: - description: "No logged in user or the user does not own the address" + description: "The user does not own the address or the user is not a customer" security: - bearerAuth: [] securityDefinitions: diff --git a/src/Controller/AddressBook/CreateAddressAction.php b/src/Controller/AddressBook/CreateAddressAction.php index 0b805d328..4a37e0431 100644 --- a/src/Controller/AddressBook/CreateAddressAction.php +++ b/src/Controller/AddressBook/CreateAddressAction.php @@ -94,7 +94,7 @@ public function __invoke(Request $request): Response $this->bus->handle(new CreateAddress($addressModel, $user->getEmail())); $view = View::create(null, Response::HTTP_NO_CONTENT); } else { - $view = View::create(['error' => 'The user has is not a customer'], Response::HTTP_BAD_REQUEST); + $view = View::create(['message' => 'The user is not a customer'], Response::HTTP_BAD_REQUEST); } return $this->viewHandler->handle($view); diff --git a/src/Controller/AddressBook/RemoveAddressAction.php b/src/Controller/AddressBook/RemoveAddressAction.php index 0d6381215..b775a0a34 100644 --- a/src/Controller/AddressBook/RemoveAddressAction.php +++ b/src/Controller/AddressBook/RemoveAddressAction.php @@ -90,7 +90,7 @@ public function __invoke(Request $request): Response $this->bus->handle(new RemoveAddress($request->attributes->get('id'), $user->getEmail())); $view = View::create(null, Response::HTTP_NO_CONTENT); } else { - $view = View::create(['error' => 'The user has is not a customer'], Response::HTTP_BAD_REQUEST); + $view = View::create(['message' => 'The user is not a customer'], Response::HTTP_BAD_REQUEST); } return $this->viewHandler->handle($view); diff --git a/src/Controller/AddressBook/SetDefaultAddressAction.php b/src/Controller/AddressBook/SetDefaultAddressAction.php index d7c8ef845..3c0212b37 100644 --- a/src/Controller/AddressBook/SetDefaultAddressAction.php +++ b/src/Controller/AddressBook/SetDefaultAddressAction.php @@ -96,7 +96,7 @@ public function __invoke(Request $request) $view = View::create(null, Response::HTTP_NO_CONTENT); } else { - $view = View::create(['error' => 'The user has is not a customer'], Response::HTTP_BAD_REQUEST); + $view = View::create(['message' => 'The user is not a customer'], Response::HTTP_BAD_REQUEST); } return $this->viewHandler->handle($view); diff --git a/src/Controller/AddressBook/ShowAddressBookAction.php b/src/Controller/AddressBook/ShowAddressBookAction.php index a48d72c02..422e7921f 100644 --- a/src/Controller/AddressBook/ShowAddressBookAction.php +++ b/src/Controller/AddressBook/ShowAddressBookAction.php @@ -67,7 +67,7 @@ function (AddressInterface $address) use ($customer) { $view = View::create($addressViews, Response::HTTP_OK); } else { - $view = View::create(['No customer associated to the user'], Response::HTTP_BAD_REQUEST); + $view = View::create(['message' => 'The user is not a customer'], Response::HTTP_BAD_REQUEST); } return $this->viewHandler->handle($view); diff --git a/src/Controller/AddressBook/UpdateAddressAction.php b/src/Controller/AddressBook/UpdateAddressAction.php index e3233215f..d66608956 100644 --- a/src/Controller/AddressBook/UpdateAddressAction.php +++ b/src/Controller/AddressBook/UpdateAddressAction.php @@ -116,7 +116,7 @@ public function __invoke(Request $request, $id): Response Response::HTTP_OK ); } else { - $view = View::create(['error' => 'The user has is not a customer'], Response::HTTP_BAD_REQUEST); + $view = View::create(['message' => 'The user is not a customer'], Response::HTTP_BAD_REQUEST); } return $this->viewHandler->handle($view); From 56aa0a4bed5b4c4e0ad436bc5770cbcd95c45ab1 Mon Sep 17 00:00:00 2001 From: mamazu Date: Mon, 27 Aug 2018 19:18:35 +0200 Subject: [PATCH 3/9] Added return value to create action --- .../AddressBook/CreateAddressAction.php | 41 +++++++++++++++++-- .../config/services/actions/address_book.xml | 2 + .../AddressBookCreateAddressApiTest.php | 2 +- .../address_book/add_address_response.json | 10 +++++ 4 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 tests/Responses/Expected/address_book/add_address_response.json diff --git a/src/Controller/AddressBook/CreateAddressAction.php b/src/Controller/AddressBook/CreateAddressAction.php index 4a37e0431..95315f1d4 100644 --- a/src/Controller/AddressBook/CreateAddressAction.php +++ b/src/Controller/AddressBook/CreateAddressAction.php @@ -7,11 +7,15 @@ use FOS\RestBundle\View\View; use FOS\RestBundle\View\ViewHandlerInterface; use League\Tactician\CommandBus; +use Sylius\Component\Core\Model\Customer; use Sylius\Component\Core\Model\ShopUserInterface; +use Sylius\Component\Core\Repository\AddressRepositoryInterface; use Sylius\ShopApiPlugin\Command\CreateAddress; +use Sylius\ShopApiPlugin\Factory\AddressBookViewFactoryInterface; use Sylius\ShopApiPlugin\Factory\ValidationErrorViewFactoryInterface; use Sylius\ShopApiPlugin\Model\Address; use Sylius\ShopApiPlugin\Provider\LoggedInUserProviderInterface; +use Sylius\ShopApiPlugin\View\AddressView; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Security\Core\Exception\TokenNotFoundException; @@ -44,11 +48,23 @@ final class CreateAddressAction */ private $currentUserProvider; + /** + * @var AddressBookViewFactoryInterface + */ + private $addressViewFactory; + + /** + * @var AddressRepositoryInterface + */ + private $addressRepository; + /** * @param ViewHandlerInterface $viewHandler * @param CommandBus $bus * @param ValidatorInterface $validator * @param ValidationErrorViewFactoryInterface $validationErrorViewFactory + * @param AddressBookViewFactoryInterface $addressViewFactory + * @param AddressRepositoryInterface $addressRepository * @param LoggedInUserProviderInterface $currentUserProvider */ public function __construct( @@ -56,13 +72,17 @@ public function __construct( CommandBus $bus, ValidatorInterface $validator, ValidationErrorViewFactoryInterface $validationErrorViewFactory, + AddressBookViewFactoryInterface $addressViewFactory, + AddressRepositoryInterface $addressRepository, LoggedInUserProviderInterface $currentUserProvider ) { $this->viewHandler = $viewHandler; $this->bus = $bus; $this->validator = $validator; $this->validationErrorViewFactory = $validationErrorViewFactory; - $this->currentUserProvider = $currentUserProvider; + $this->addressViewFactory = $addressViewFactory; + $this->addressRepository = $addressRepository; + $this->currentUserProvider = $currentUserProvider; } /** @@ -90,13 +110,28 @@ public function __invoke(Request $request): Response } - if ($user->getCustomer() !== null) { + if (($customer = $user->getCustomer()) !== null) { $this->bus->handle(new CreateAddress($addressModel, $user->getEmail())); - $view = View::create(null, Response::HTTP_NO_CONTENT); + + $view = View::create($this->getLastInsertedAddress($customer), Response::HTTP_NO_CONTENT); } else { $view = View::create(['message' => 'The user is not a customer'], Response::HTTP_BAD_REQUEST); } return $this->viewHandler->handle($view); } + + /** + * Returns the id that was inserted last in the address book + * + * @param Customer $customer + * + * @return AddressView + */ + private function getLastInsertedAddress(Customer $customer): AddressView + { + $addresses = $this->addressRepository->findByCustomer($customer); + + $this->addressViewFactory->create(end($addresses), $customer); + } } diff --git a/src/Resources/config/services/actions/address_book.xml b/src/Resources/config/services/actions/address_book.xml index 861f2150a..b539aa09c 100644 --- a/src/Resources/config/services/actions/address_book.xml +++ b/src/Resources/config/services/actions/address_book.xml @@ -11,6 +11,8 @@ + + client->request('POST', '/shop-api/address-book', [], [], self::$acceptAndContentTypeHeader, $data); $response = $this->client->getResponse(); - $this->assertResponseCode($response, Response::HTTP_NO_CONTENT); + $this->assertResponse($response, 'address_book/add_address_response', Response::HTTP_NO_CONTENT); /** @var CustomerRepositoryInterface $customerRepository */ $customerRepository = $this->get('sylius.repository.customer'); diff --git a/tests/Responses/Expected/address_book/add_address_response.json b/tests/Responses/Expected/address_book/add_address_response.json new file mode 100644 index 000000000..0acc8fe7a --- /dev/null +++ b/tests/Responses/Expected/address_book/add_address_response.json @@ -0,0 +1,10 @@ +{ + "id": @integer@, + "firstName": "Jurica", + "lastName": "Separovic", + "phoneNumber": "091892212", + "city": "Split", + "street": "Kupreska 12", + "countryCode": "GB", + "postcode": "2433" +} From a5767f4700ff19b307724d81681da803e029e363 Mon Sep 17 00:00:00 2001 From: mamazu Date: Mon, 27 Aug 2018 22:09:39 +0200 Subject: [PATCH 4/9] Changed response codes --- src/Controller/AddressBook/CreateAddressAction.php | 10 +++++----- tests/Controller/AddressBookCreateAddressApiTest.php | 2 +- .../Expected/address_book/add_address_response.json | 3 ++- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/Controller/AddressBook/CreateAddressAction.php b/src/Controller/AddressBook/CreateAddressAction.php index 95315f1d4..2a376ec42 100644 --- a/src/Controller/AddressBook/CreateAddressAction.php +++ b/src/Controller/AddressBook/CreateAddressAction.php @@ -15,7 +15,7 @@ use Sylius\ShopApiPlugin\Factory\ValidationErrorViewFactoryInterface; use Sylius\ShopApiPlugin\Model\Address; use Sylius\ShopApiPlugin\Provider\LoggedInUserProviderInterface; -use Sylius\ShopApiPlugin\View\AddressView; +use Sylius\ShopApiPlugin\View\AddressBookView; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Security\Core\Exception\TokenNotFoundException; @@ -113,7 +113,7 @@ public function __invoke(Request $request): Response if (($customer = $user->getCustomer()) !== null) { $this->bus->handle(new CreateAddress($addressModel, $user->getEmail())); - $view = View::create($this->getLastInsertedAddress($customer), Response::HTTP_NO_CONTENT); + $view = View::create($this->getLastInsertedAddress($customer), Response::HTTP_CREATED); } else { $view = View::create(['message' => 'The user is not a customer'], Response::HTTP_BAD_REQUEST); } @@ -126,12 +126,12 @@ public function __invoke(Request $request): Response * * @param Customer $customer * - * @return AddressView + * @return AddressBookView */ - private function getLastInsertedAddress(Customer $customer): AddressView + private function getLastInsertedAddress(Customer $customer): AddressBookView { $addresses = $this->addressRepository->findByCustomer($customer); - $this->addressViewFactory->create(end($addresses), $customer); + return $this->addressViewFactory->create(end($addresses), $customer); } } diff --git a/tests/Controller/AddressBookCreateAddressApiTest.php b/tests/Controller/AddressBookCreateAddressApiTest.php index 7022d48ac..429334782 100644 --- a/tests/Controller/AddressBookCreateAddressApiTest.php +++ b/tests/Controller/AddressBookCreateAddressApiTest.php @@ -42,7 +42,7 @@ public function it_allows_user_to_add_new_address_to_address_book() $this->client->request('POST', '/shop-api/address-book', [], [], self::$acceptAndContentTypeHeader, $data); $response = $this->client->getResponse(); - $this->assertResponse($response, 'address_book/add_address_response', Response::HTTP_NO_CONTENT); + $this->assertResponse($response, 'address_book/add_address_response', Response::HTTP_CREATED); /** @var CustomerRepositoryInterface $customerRepository */ $customerRepository = $this->get('sylius.repository.customer'); diff --git a/tests/Responses/Expected/address_book/add_address_response.json b/tests/Responses/Expected/address_book/add_address_response.json index 0acc8fe7a..c8b8eac50 100644 --- a/tests/Responses/Expected/address_book/add_address_response.json +++ b/tests/Responses/Expected/address_book/add_address_response.json @@ -6,5 +6,6 @@ "city": "Split", "street": "Kupreska 12", "countryCode": "GB", - "postcode": "2433" + "postcode": "2433", + "default": false } From 2bfb722bd396f063fde6a54a51b347e7a792c223 Mon Sep 17 00:00:00 2001 From: mamazu Date: Mon, 27 Aug 2018 22:39:02 +0200 Subject: [PATCH 5/9] Added documentation --- doc/swagger.yml | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/doc/swagger.yml b/doc/swagger.yml index b5a19573c..73d24484b 100644 --- a/doc/swagger.yml +++ b/doc/swagger.yml @@ -896,10 +896,8 @@ paths: description: "Successfully fetched Address Book" schema: $ref: "#/definitions/LoggedInCustomerAddressBook" - 401: - description: "No user is logged in" 500: - description: "The user is not a customer" + description: "User not logged in" security: - bearerAuth: [] post: @@ -914,8 +912,10 @@ paths: schema: $ref: "#/definitions/LoggedInCustomerAddressBookAddress" responses: - 204: + 201: description: "Successfully created the address" + schema: + - $ref: "#/definitions/LoggedInCustomerAddressBookAddress" 400: description: "Validation failed" schema: @@ -923,7 +923,7 @@ paths: 401: description: "No user is logged in" 500: - description: "The user does not own the address or the user is not a customer" + description: "The user does not own the address" security: - bearerAuth: [] /address-book/{id}: @@ -957,7 +957,7 @@ paths: 401: description: "No user is logged in" 500: - description: "The user does not own the address or the user is not a customer" + description: "The user does not own the address" security: - bearerAuth: [] delete: @@ -982,7 +982,7 @@ paths: 401: description: "No user is logged in" 500: - description: "The user does not own the address or the user is not a customer" + description: "The user does not own the address" security: - bearerAuth: [] /address-book/{id}/default: @@ -1008,7 +1008,7 @@ paths: 401: description: "No user is logged in" 500: - description: "The user does not own the address or the user is not a customer" + description: "The user does not own the address" security: - bearerAuth: [] securityDefinitions: From a4c5fccd1c023ca22e479ebe961b8719390388c5 Mon Sep 17 00:00:00 2001 From: mamazu Date: Fri, 31 Aug 2018 09:49:54 +0200 Subject: [PATCH 6/9] Codestyle --- src/Controller/AddressBook/ShowAddressBookAction.php | 1 - src/Controller/Customer/UpdateCustomerAction.php | 6 ++++-- src/Provider/LoggedInUserProvider.php | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Controller/AddressBook/ShowAddressBookAction.php b/src/Controller/AddressBook/ShowAddressBookAction.php index 422e7921f..a1d7608fe 100644 --- a/src/Controller/AddressBook/ShowAddressBookAction.php +++ b/src/Controller/AddressBook/ShowAddressBookAction.php @@ -58,7 +58,6 @@ public function __invoke(Request $request): Response $customer = $user->getCustomer(); if ($customer instanceof Customer) { - $addressViews = $customer->getAddresses()->map( function (AddressInterface $address) use ($customer) { return $this->addressBookViewFactory->create($address, $customer); diff --git a/src/Controller/Customer/UpdateCustomerAction.php b/src/Controller/Customer/UpdateCustomerAction.php index 985f98f70..6eaaf9ad7 100644 --- a/src/Controller/Customer/UpdateCustomerAction.php +++ b/src/Controller/Customer/UpdateCustomerAction.php @@ -77,9 +77,11 @@ public function __invoke(Request $request): Response $updateCustomerCommand = $updateCustomerRequest->getCommand(); $this->bus->handle($updateCustomerCommand); - return $this->viewHandler->handle(View::create( + return $this->viewHandler->handle( + View::create( $this->customerViewFactory->create($customer), - Response::HTTP_OK) + Response::HTTP_OK + ) ); } } diff --git a/src/Provider/LoggedInUserProvider.php b/src/Provider/LoggedInUserProvider.php index eb914c974..fcf51f3b9 100644 --- a/src/Provider/LoggedInUserProvider.php +++ b/src/Provider/LoggedInUserProvider.php @@ -28,7 +28,7 @@ public function provide(): ShopUserInterface /** @var ShopUserInterface $user */ $user = $this->tokenStorage->getToken()->getUser(); - if(!$user instanceof ShopUserInterface){ + if (!$user instanceof ShopUserInterface) { throw new TokenNotFoundException(); } From 450ad5727bddfd91b71755a0439562346f522310 Mon Sep 17 00:00:00 2001 From: mamazu Date: Fri, 31 Aug 2018 11:13:25 +0200 Subject: [PATCH 7/9] Added codestyle --- src/Controller/AddressBook/CreateAddressAction.php | 7 +++---- src/Controller/AddressBook/RemoveAddressAction.php | 8 ++++---- .../AddressBook/SetDefaultAddressAction.php | 8 ++++---- src/Controller/AddressBook/ShowAddressBookAction.php | 4 ++-- src/Controller/AddressBook/UpdateAddressAction.php | 12 ++++++------ src/Controller/CartController.php | 2 +- src/Controller/Product/ShowLatestProductAction.php | 1 + src/ViewRepository/ProductLatestViewRepository.php | 4 ++-- 8 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/Controller/AddressBook/CreateAddressAction.php b/src/Controller/AddressBook/CreateAddressAction.php index 2a376ec42..92a8ae293 100644 --- a/src/Controller/AddressBook/CreateAddressAction.php +++ b/src/Controller/AddressBook/CreateAddressAction.php @@ -76,9 +76,9 @@ public function __construct( AddressRepositoryInterface $addressRepository, LoggedInUserProviderInterface $currentUserProvider ) { - $this->viewHandler = $viewHandler; - $this->bus = $bus; - $this->validator = $validator; + $this->viewHandler = $viewHandler; + $this->bus = $bus; + $this->validator = $validator; $this->validationErrorViewFactory = $validationErrorViewFactory; $this->addressViewFactory = $addressViewFactory; $this->addressRepository = $addressRepository; @@ -109,7 +109,6 @@ public function __invoke(Request $request): Response return $this->viewHandler->handle(View::create(null, Response::HTTP_UNAUTHORIZED)); } - if (($customer = $user->getCustomer()) !== null) { $this->bus->handle(new CreateAddress($addressModel, $user->getEmail())); diff --git a/src/Controller/AddressBook/RemoveAddressAction.php b/src/Controller/AddressBook/RemoveAddressAction.php index b775a0a34..a8f5bac2e 100644 --- a/src/Controller/AddressBook/RemoveAddressAction.php +++ b/src/Controller/AddressBook/RemoveAddressAction.php @@ -60,11 +60,11 @@ public function __construct( CommandBus $bus, LoggedInUserProviderInterface $currentUserProvider ) { - $this->viewHandler = $viewHandler; - $this->validator = $validator; + $this->viewHandler = $viewHandler; + $this->validator = $validator; $this->validationErrorViewFactory = $validationErrorViewFactory; - $this->bus = $bus; - $this->currentUserProvider = $currentUserProvider; + $this->bus = $bus; + $this->currentUserProvider = $currentUserProvider; } public function __invoke(Request $request): Response diff --git a/src/Controller/AddressBook/SetDefaultAddressAction.php b/src/Controller/AddressBook/SetDefaultAddressAction.php index 3c0212b37..dd9545da4 100644 --- a/src/Controller/AddressBook/SetDefaultAddressAction.php +++ b/src/Controller/AddressBook/SetDefaultAddressAction.php @@ -60,11 +60,11 @@ public function __construct( ValidationErrorViewFactoryInterface $validationErrorViewFactory, LoggedInUserProviderInterface $currentUserProvider ) { - $this->viewHandler = $viewHandler; - $this->bus = $bus; - $this->validator = $validator; + $this->viewHandler = $viewHandler; + $this->bus = $bus; + $this->validator = $validator; $this->validationErrorViewFactory = $validationErrorViewFactory; - $this->currentUserProvider = $currentUserProvider; + $this->currentUserProvider = $currentUserProvider; } /** diff --git a/src/Controller/AddressBook/ShowAddressBookAction.php b/src/Controller/AddressBook/ShowAddressBookAction.php index a1d7608fe..de4f4ae45 100644 --- a/src/Controller/AddressBook/ShowAddressBookAction.php +++ b/src/Controller/AddressBook/ShowAddressBookAction.php @@ -42,8 +42,8 @@ public function __construct( LoggedInUserProviderInterface $currentUserProvider, AddressBookViewFactoryInterface $addressBookViewFactory ) { - $this->viewHandler = $viewHandler; - $this->currentUserProvider = $currentUserProvider; + $this->viewHandler = $viewHandler; + $this->currentUserProvider = $currentUserProvider; $this->addressBookViewFactory = $addressBookViewFactory; } diff --git a/src/Controller/AddressBook/UpdateAddressAction.php b/src/Controller/AddressBook/UpdateAddressAction.php index d66608956..fc7efeff3 100644 --- a/src/Controller/AddressBook/UpdateAddressAction.php +++ b/src/Controller/AddressBook/UpdateAddressAction.php @@ -77,13 +77,13 @@ public function __construct( AddressRepositoryInterface $addressRepository, LoggedInUserProviderInterface $currentUserProvider ) { - $this->viewHandler = $viewHandler; - $this->validator = $validator; - $this->bus = $bus; + $this->viewHandler = $viewHandler; + $this->validator = $validator; + $this->bus = $bus; $this->validationErrorViewFactory = $validationErrorViewFactory; - $this->addressBookViewFactory = $addressViewFactory; - $this->addressRepository = $addressRepository; - $this->currentUserProvider = $currentUserProvider; + $this->addressBookViewFactory = $addressViewFactory; + $this->addressRepository = $addressRepository; + $this->currentUserProvider = $currentUserProvider; } public function __invoke(Request $request, $id): Response diff --git a/src/Controller/CartController.php b/src/Controller/CartController.php index ef0d6333e..44e23290f 100644 --- a/src/Controller/CartController.php +++ b/src/Controller/CartController.php @@ -11,7 +11,6 @@ use Sylius\Component\Core\Model\OrderInterface; use Sylius\Component\Core\Model\ShipmentInterface; use Sylius\Component\Core\Repository\OrderRepositoryInterface; -use Sylius\Component\Order\Repository\OrderItemRepositoryInterface; use Sylius\Component\Registry\ServiceRegistryInterface; use Sylius\Component\Resource\Factory\FactoryInterface; use Sylius\Component\Shipping\Exception\UnresolvedDefaultShippingMethodException; @@ -29,6 +28,7 @@ final class CartController extends Controller * @param Request $request * * @return Response + * * @throws UnresolvedDefaultShippingMethodException */ public function estimateShippingCostAction(Request $request) diff --git a/src/Controller/Product/ShowLatestProductAction.php b/src/Controller/Product/ShowLatestProductAction.php index 362f4332c..bdae78611 100644 --- a/src/Controller/Product/ShowLatestProductAction.php +++ b/src/Controller/Product/ShowLatestProductAction.php @@ -1,4 +1,5 @@ Date: Fri, 31 Aug 2018 13:03:09 +0200 Subject: [PATCH 8/9] Implemented suggestions (renamed variables) --- src/Controller/AddressBook/CreateAddressAction.php | 10 +++++----- src/Controller/AddressBook/RemoveAddressAction.php | 12 +++++------- .../AddressBook/SetDefaultAddressAction.php | 10 +++++----- src/Controller/AddressBook/ShowAddressBookAction.php | 10 +++++----- src/Controller/AddressBook/UpdateAddressAction.php | 10 +++++----- tests/Controller/AddressBookShowApiTest.php | 2 +- .../show_address_book_unauthorized_response.json | 1 - 7 files changed, 26 insertions(+), 29 deletions(-) delete mode 100644 tests/Responses/Expected/address_book/show_address_book_unauthorized_response.json diff --git a/src/Controller/AddressBook/CreateAddressAction.php b/src/Controller/AddressBook/CreateAddressAction.php index 92a8ae293..7e21d5a3d 100644 --- a/src/Controller/AddressBook/CreateAddressAction.php +++ b/src/Controller/AddressBook/CreateAddressAction.php @@ -46,7 +46,7 @@ final class CreateAddressAction /** * @var LoggedInUserProviderInterface */ - private $currentUserProvider; + private $loggedInUserProvider; /** * @var AddressBookViewFactoryInterface @@ -65,7 +65,7 @@ final class CreateAddressAction * @param ValidationErrorViewFactoryInterface $validationErrorViewFactory * @param AddressBookViewFactoryInterface $addressViewFactory * @param AddressRepositoryInterface $addressRepository - * @param LoggedInUserProviderInterface $currentUserProvider + * @param LoggedInUserProviderInterface $loggedInUserProvider */ public function __construct( ViewHandlerInterface $viewHandler, @@ -74,7 +74,7 @@ public function __construct( ValidationErrorViewFactoryInterface $validationErrorViewFactory, AddressBookViewFactoryInterface $addressViewFactory, AddressRepositoryInterface $addressRepository, - LoggedInUserProviderInterface $currentUserProvider + LoggedInUserProviderInterface $loggedInUserProvider ) { $this->viewHandler = $viewHandler; $this->bus = $bus; @@ -82,7 +82,7 @@ public function __construct( $this->validationErrorViewFactory = $validationErrorViewFactory; $this->addressViewFactory = $addressViewFactory; $this->addressRepository = $addressRepository; - $this->currentUserProvider = $currentUserProvider; + $this->loggedInUserProvider = $loggedInUserProvider; } /** @@ -104,7 +104,7 @@ public function __invoke(Request $request): Response try { /** @var ShopUserInterface $user */ - $user = $this->currentUserProvider->provide(); + $user = $this->loggedInUserProvider->provide(); } catch (TokenNotFoundException $exception) { return $this->viewHandler->handle(View::create(null, Response::HTTP_UNAUTHORIZED)); } diff --git a/src/Controller/AddressBook/RemoveAddressAction.php b/src/Controller/AddressBook/RemoveAddressAction.php index a8f5bac2e..52c639891 100644 --- a/src/Controller/AddressBook/RemoveAddressAction.php +++ b/src/Controller/AddressBook/RemoveAddressAction.php @@ -14,7 +14,6 @@ use Sylius\ShopApiPlugin\Request\RemoveAddressRequest; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; -use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; use Symfony\Component\Security\Core\Exception\TokenNotFoundException; use Symfony\Component\Validator\Validator\ValidatorInterface; @@ -43,28 +42,27 @@ final class RemoveAddressAction /** * @var LoggedInUserProviderInterface */ - private $currentUserProvider; + private $loggedInUserProvider; /** * @param ViewHandlerInterface $viewHandler * @param ValidatorInterface $validator * @param ValidationErrorViewFactory $validationErrorViewFactory * @param CommandBus $bus - * @param TokenStorageInterface $tokenStorage - * @param LoggedInUserProviderInterface $currentUserProvider + * @param LoggedInUserProviderInterface $loggedInUserProvider */ public function __construct( ViewHandlerInterface $viewHandler, ValidatorInterface $validator, ValidationErrorViewFactory $validationErrorViewFactory, CommandBus $bus, - LoggedInUserProviderInterface $currentUserProvider + LoggedInUserProviderInterface $loggedInUserProvider ) { $this->viewHandler = $viewHandler; $this->validator = $validator; $this->validationErrorViewFactory = $validationErrorViewFactory; $this->bus = $bus; - $this->currentUserProvider = $currentUserProvider; + $this->loggedInUserProvider = $loggedInUserProvider; } public function __invoke(Request $request): Response @@ -81,7 +79,7 @@ public function __invoke(Request $request): Response try { /** @var ShopUserInterface $user */ - $user = $this->currentUserProvider->provide(); + $user = $this->loggedInUserProvider->provide(); } catch (TokenNotFoundException $exception) { return $this->viewHandler->handle(View::create(null, Response::HTTP_UNAUTHORIZED)); } diff --git a/src/Controller/AddressBook/SetDefaultAddressAction.php b/src/Controller/AddressBook/SetDefaultAddressAction.php index dd9545da4..fc9c89725 100644 --- a/src/Controller/AddressBook/SetDefaultAddressAction.php +++ b/src/Controller/AddressBook/SetDefaultAddressAction.php @@ -43,7 +43,7 @@ final class SetDefaultAddressAction /** * @var LoggedInUserProviderInterface */ - private $currentUserProvider; + private $loggedInUserProvider; /** * @param ViewHandlerInterface $viewHandler @@ -51,20 +51,20 @@ final class SetDefaultAddressAction * @param ValidatorInterface $validator * @param ValidationErrorViewFactoryInterface $validationErrorViewFactory * @param TokenStorageInterface $tokenStorage - * @param LoggedInUserProviderInterface $currentUserProvider + * @param LoggedInUserProviderInterface $loggedInUserProvider */ public function __construct( ViewHandlerInterface $viewHandler, CommandBus $bus, ValidatorInterface $validator, ValidationErrorViewFactoryInterface $validationErrorViewFactory, - LoggedInUserProviderInterface $currentUserProvider + LoggedInUserProviderInterface $loggedInUserProvider ) { $this->viewHandler = $viewHandler; $this->bus = $bus; $this->validator = $validator; $this->validationErrorViewFactory = $validationErrorViewFactory; - $this->currentUserProvider = $currentUserProvider; + $this->loggedInUserProvider = $loggedInUserProvider; } /** @@ -86,7 +86,7 @@ public function __invoke(Request $request) try { /** @var ShopUserInterface $user */ - $user = $this->currentUserProvider->provide(); + $user = $this->loggedInUserProvider->provide(); } catch (TokenNotFoundException $exception) { return $this->viewHandler->handle(View::create(null, Response::HTTP_UNAUTHORIZED)); } diff --git a/src/Controller/AddressBook/ShowAddressBookAction.php b/src/Controller/AddressBook/ShowAddressBookAction.php index de4f4ae45..ae2106908 100644 --- a/src/Controller/AddressBook/ShowAddressBookAction.php +++ b/src/Controller/AddressBook/ShowAddressBookAction.php @@ -25,7 +25,7 @@ final class ShowAddressBookAction /** * @var LoggedInUserProviderInterface */ - private $currentUserProvider; + private $loggedInUserProvider; /** * @var AddressBookViewFactoryInterface @@ -34,16 +34,16 @@ final class ShowAddressBookAction /** * @param ViewHandlerInterface $viewHandler - * @param LoggedInUserProviderInterface $currentUserProvider + * @param LoggedInUserProviderInterface $loggedInUserProvider * @param AddressBookViewFactoryInterface $addressBookViewFactory */ public function __construct( ViewHandlerInterface $viewHandler, - LoggedInUserProviderInterface $currentUserProvider, + LoggedInUserProviderInterface $loggedInUserProvider, AddressBookViewFactoryInterface $addressBookViewFactory ) { $this->viewHandler = $viewHandler; - $this->currentUserProvider = $currentUserProvider; + $this->loggedInUserProvider = $loggedInUserProvider; $this->addressBookViewFactory = $addressBookViewFactory; } @@ -51,7 +51,7 @@ public function __invoke(Request $request): Response { try { /** @var ShopUserInterface $user */ - $user = $this->currentUserProvider->provide(); + $user = $this->loggedInUserProvider->provide(); } catch (TokenNotFoundException $exception) { return $this->viewHandler->handle(View::create(null, Response::HTTP_UNAUTHORIZED)); } diff --git a/src/Controller/AddressBook/UpdateAddressAction.php b/src/Controller/AddressBook/UpdateAddressAction.php index fc7efeff3..bd12a26a6 100644 --- a/src/Controller/AddressBook/UpdateAddressAction.php +++ b/src/Controller/AddressBook/UpdateAddressAction.php @@ -56,7 +56,7 @@ final class UpdateAddressAction /** * @var LoggedInUserProviderInterface */ - private $currentUserProvider; + private $loggedInUserProvider; /** * @param ViewHandlerInterface $viewHandler @@ -66,7 +66,7 @@ final class UpdateAddressAction * @param AddressBookViewFactoryInterface $addressViewFactory * @param AddressRepositoryInterface $addressRepository * @param TokenStorageInterface $tokenStorage - * @param LoggedInUserProviderInterface $currentUserProvider + * @param LoggedInUserProviderInterface $loggedInUserProvider */ public function __construct( ViewHandlerInterface $viewHandler, @@ -75,7 +75,7 @@ public function __construct( ValidationErrorViewFactoryInterface $validationErrorViewFactory, AddressBookViewFactoryInterface $addressViewFactory, AddressRepositoryInterface $addressRepository, - LoggedInUserProviderInterface $currentUserProvider + LoggedInUserProviderInterface $loggedInUserProvider ) { $this->viewHandler = $viewHandler; $this->validator = $validator; @@ -83,7 +83,7 @@ public function __construct( $this->validationErrorViewFactory = $validationErrorViewFactory; $this->addressBookViewFactory = $addressViewFactory; $this->addressRepository = $addressRepository; - $this->currentUserProvider = $currentUserProvider; + $this->loggedInUserProvider = $loggedInUserProvider; } public function __invoke(Request $request, $id): Response @@ -100,7 +100,7 @@ public function __invoke(Request $request, $id): Response try { /** @var ShopUserInterface $user */ - $user = $this->currentUserProvider->provide(); + $user = $this->loggedInUserProvider->provide(); } catch (TokenNotFoundException $exception) { return $this->viewHandler->handle(View::create(null, Response::HTTP_UNAUTHORIZED)); } diff --git a/tests/Controller/AddressBookShowApiTest.php b/tests/Controller/AddressBookShowApiTest.php index 5eff2256f..243aa886b 100644 --- a/tests/Controller/AddressBookShowApiTest.php +++ b/tests/Controller/AddressBookShowApiTest.php @@ -33,7 +33,7 @@ public function it_returns_an_unauthorized_exception_if_there_is_no_logged_in_us $this->client->request('GET', '/shop-api/address-book', [], [], ['ACCEPT' => 'application/json']); $response = $this->client->getResponse(); - $this->assertResponse($response, 'address_book/show_address_book_unauthorized_response', Response::HTTP_UNAUTHORIZED); + $this->assertResponseCode($response, Response::HTTP_UNAUTHORIZED); } } diff --git a/tests/Responses/Expected/address_book/show_address_book_unauthorized_response.json b/tests/Responses/Expected/address_book/show_address_book_unauthorized_response.json deleted file mode 100644 index 19765bd50..000000000 --- a/tests/Responses/Expected/address_book/show_address_book_unauthorized_response.json +++ /dev/null @@ -1 +0,0 @@ -null From c701680ebed8aac066d7316514e59dccac955b11 Mon Sep 17 00:00:00 2001 From: mamazu Date: Fri, 31 Aug 2018 14:18:42 +0200 Subject: [PATCH 9/9] Unaligned parameters --- .../AddressBook/CreateAddressAction.php | 12 +++++------ .../AddressBook/RemoveAddressAction.php | 15 +++++++++----- .../AddressBook/SetDefaultAddressAction.php | 10 +++++----- .../AddressBook/ShowAddressBookAction.php | 9 ++++++--- .../AddressBook/UpdateAddressAction.php | 20 ++++++++++++------- src/Request/RemoveAddressRequest.php | 10 +++++++++- 6 files changed, 49 insertions(+), 27 deletions(-) diff --git a/src/Controller/AddressBook/CreateAddressAction.php b/src/Controller/AddressBook/CreateAddressAction.php index 7e21d5a3d..5d0014065 100644 --- a/src/Controller/AddressBook/CreateAddressAction.php +++ b/src/Controller/AddressBook/CreateAddressAction.php @@ -59,13 +59,13 @@ final class CreateAddressAction private $addressRepository; /** - * @param ViewHandlerInterface $viewHandler - * @param CommandBus $bus - * @param ValidatorInterface $validator + * @param ViewHandlerInterface $viewHandler + * @param CommandBus $bus + * @param ValidatorInterface $validator * @param ValidationErrorViewFactoryInterface $validationErrorViewFactory - * @param AddressBookViewFactoryInterface $addressViewFactory - * @param AddressRepositoryInterface $addressRepository - * @param LoggedInUserProviderInterface $loggedInUserProvider + * @param AddressBookViewFactoryInterface $addressViewFactory + * @param AddressRepositoryInterface $addressRepository + * @param LoggedInUserProviderInterface $loggedInUserProvider */ public function __construct( ViewHandlerInterface $viewHandler, diff --git a/src/Controller/AddressBook/RemoveAddressAction.php b/src/Controller/AddressBook/RemoveAddressAction.php index 52c639891..e153df307 100644 --- a/src/Controller/AddressBook/RemoveAddressAction.php +++ b/src/Controller/AddressBook/RemoveAddressAction.php @@ -45,10 +45,10 @@ final class RemoveAddressAction private $loggedInUserProvider; /** - * @param ViewHandlerInterface $viewHandler - * @param ValidatorInterface $validator - * @param ValidationErrorViewFactory $validationErrorViewFactory - * @param CommandBus $bus + * @param ViewHandlerInterface $viewHandler + * @param ValidatorInterface $validator + * @param ValidationErrorViewFactory $validationErrorViewFactory + * @param CommandBus $bus * @param LoggedInUserProviderInterface $loggedInUserProvider */ public function __construct( @@ -65,6 +65,11 @@ public function __construct( $this->loggedInUserProvider = $loggedInUserProvider; } + /** + * Removes an address from the address book + * + * @param Request $request + */ public function __invoke(Request $request): Response { $removeAddressRequest = new RemoveAddressRequest($request); @@ -85,7 +90,7 @@ public function __invoke(Request $request): Response } if ($user->getCustomer() !== null) { - $this->bus->handle(new RemoveAddress($request->attributes->get('id'), $user->getEmail())); + $this->bus->handle(new RemoveAddress($removeAddressRequest->id(), $user->getEmail())); $view = View::create(null, Response::HTTP_NO_CONTENT); } else { $view = View::create(['message' => 'The user is not a customer'], Response::HTTP_BAD_REQUEST); diff --git a/src/Controller/AddressBook/SetDefaultAddressAction.php b/src/Controller/AddressBook/SetDefaultAddressAction.php index fc9c89725..6b996c40a 100644 --- a/src/Controller/AddressBook/SetDefaultAddressAction.php +++ b/src/Controller/AddressBook/SetDefaultAddressAction.php @@ -46,12 +46,12 @@ final class SetDefaultAddressAction private $loggedInUserProvider; /** - * @param ViewHandlerInterface $viewHandler - * @param CommandBus $bus - * @param ValidatorInterface $validator + * @param ViewHandlerInterface $viewHandler + * @param CommandBus $bus + * @param ValidatorInterface $validator * @param ValidationErrorViewFactoryInterface $validationErrorViewFactory - * @param TokenStorageInterface $tokenStorage - * @param LoggedInUserProviderInterface $loggedInUserProvider + * @param TokenStorageInterface $tokenStorage + * @param LoggedInUserProviderInterface $loggedInUserProvider */ public function __construct( ViewHandlerInterface $viewHandler, diff --git a/src/Controller/AddressBook/ShowAddressBookAction.php b/src/Controller/AddressBook/ShowAddressBookAction.php index ae2106908..87d3b5013 100644 --- a/src/Controller/AddressBook/ShowAddressBookAction.php +++ b/src/Controller/AddressBook/ShowAddressBookAction.php @@ -33,8 +33,8 @@ final class ShowAddressBookAction private $addressBookViewFactory; /** - * @param ViewHandlerInterface $viewHandler - * @param LoggedInUserProviderInterface $loggedInUserProvider + * @param ViewHandlerInterface $viewHandler + * @param LoggedInUserProviderInterface $loggedInUserProvider * @param AddressBookViewFactoryInterface $addressBookViewFactory */ public function __construct( @@ -47,7 +47,10 @@ public function __construct( $this->addressBookViewFactory = $addressBookViewFactory; } - public function __invoke(Request $request): Response + /** + * Returns the list of addresses that is stored in the user's address book + */ + public function __invoke(): Response { try { /** @var ShopUserInterface $user */ diff --git a/src/Controller/AddressBook/UpdateAddressAction.php b/src/Controller/AddressBook/UpdateAddressAction.php index bd12a26a6..3a0a7d7f2 100644 --- a/src/Controller/AddressBook/UpdateAddressAction.php +++ b/src/Controller/AddressBook/UpdateAddressAction.php @@ -59,14 +59,14 @@ final class UpdateAddressAction private $loggedInUserProvider; /** - * @param ViewHandlerInterface $viewHandler - * @param ValidatorInterface $validator - * @param CommandBus $bus + * @param ViewHandlerInterface $viewHandler + * @param ValidatorInterface $validator + * @param CommandBus $bus * @param ValidationErrorViewFactoryInterface $validationErrorViewFactory - * @param AddressBookViewFactoryInterface $addressViewFactory - * @param AddressRepositoryInterface $addressRepository - * @param TokenStorageInterface $tokenStorage - * @param LoggedInUserProviderInterface $loggedInUserProvider + * @param AddressBookViewFactoryInterface $addressViewFactory + * @param AddressRepositoryInterface $addressRepository + * @param TokenStorageInterface $tokenStorage + * @param LoggedInUserProviderInterface $loggedInUserProvider */ public function __construct( ViewHandlerInterface $viewHandler, @@ -86,6 +86,12 @@ public function __construct( $this->loggedInUserProvider = $loggedInUserProvider; } + /** + * Updates an address in the address book + * + * @param Request $request + * @param string|int $id + */ public function __invoke(Request $request, $id): Response { $addressModel = Address::createFromRequest($request); diff --git a/src/Request/RemoveAddressRequest.php b/src/Request/RemoveAddressRequest.php index 6fec62f28..c50381dc8 100644 --- a/src/Request/RemoveAddressRequest.php +++ b/src/Request/RemoveAddressRequest.php @@ -9,7 +9,7 @@ final class RemoveAddressRequest { /** - * @var mixed + * @var int|string */ private $id; @@ -17,4 +17,12 @@ public function __construct(Request $request) { $this->id = $request->attributes->get('id'); } + + /** + * @return int|string + */ + public function id() + { + return $this->id; + } }