-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
More precise error messages #301
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7797a08
Added more precise response codes
mamazu 9ca1eba
Updated the documentaion and unified the responses
mamazu 56aa0a4
Added return value to create action
mamazu a5767f4
Changed response codes
mamazu 2bfb722
Added documentation
mamazu a4c5fcc
Codestyle
mamazu 450ad57
Added codestyle
mamazu 61a1eba
Implemented suggestions (renamed variables)
mamazu c701680
Unaligned parameters
mamazu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace spec\Sylius\ShopApiPlugin\Provider; | ||
|
||
use PhpSpec\ObjectBehavior; | ||
use Sylius\Component\Core\Model\ShopUserInterface; | ||
use Sylius\ShopApiPlugin\Provider\LoggedInUserProviderInterface; | ||
use Symfony\Component\Security\Core\Exception\TokenNotFoundException; | ||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; | ||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
|
||
final class LoggedInUserProviderSpec extends ObjectBehavior | ||
{ | ||
function let(TokenStorageInterface $tokenStorage) | ||
{ | ||
$this->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 | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CS |
||
{ | ||
$token->getUser()->shouldBeCalled()->willReturn($shopUser); | ||
$tokenStorage->getToken()->shouldBeCalled()->willReturn($token); | ||
|
||
$this->provide()->shouldReturn($shopUser); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,13 +7,18 @@ | |
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\CurrentUserProviderInterface; | ||
use Sylius\ShopApiPlugin\Provider\LoggedInUserProviderInterface; | ||
use Sylius\ShopApiPlugin\View\AddressBookView; | ||
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 +44,45 @@ final class CreateAddressAction | |
private $validationErrorViewFactory; | ||
|
||
/** | ||
* @var TokenStorage | ||
* @var LoggedInUserProviderInterface | ||
*/ | ||
private $tokenStorage; | ||
private $loggedInUserProvider; | ||
|
||
/** | ||
* @var AddressBookViewFactoryInterface | ||
*/ | ||
private $addressViewFactory; | ||
|
||
/** | ||
* @var CurrentUserProviderInterface | ||
* @var AddressRepositoryInterface | ||
*/ | ||
private $currentUserProvider; | ||
private $addressRepository; | ||
|
||
/** | ||
* @param ViewHandlerInterface $viewHandler | ||
* @param CommandBus $bus | ||
* @param ValidatorInterface $validator | ||
* @param ValidationErrorViewFactoryInterface $validationErrorViewFactory | ||
* @param TokenStorage $tokenStorage | ||
* @param CurrentUserProviderInterface $currentUserProvider | ||
* @param AddressBookViewFactoryInterface $addressViewFactory | ||
* @param AddressRepositoryInterface $addressRepository | ||
* @param LoggedInUserProviderInterface $loggedInUserProvider | ||
*/ | ||
public function __construct( | ||
ViewHandlerInterface $viewHandler, | ||
CommandBus $bus, | ||
ValidatorInterface $validator, | ||
ValidationErrorViewFactoryInterface $validationErrorViewFactory, | ||
TokenStorage $tokenStorage, | ||
CurrentUserProviderInterface $currentUserProvider | ||
AddressBookViewFactoryInterface $addressViewFactory, | ||
AddressRepositoryInterface $addressRepository, | ||
LoggedInUserProviderInterface $loggedInUserProvider | ||
) { | ||
$this->viewHandler = $viewHandler; | ||
$this->bus = $bus; | ||
$this->validator = $validator; | ||
$this->validationErrorViewFactory = $validationErrorViewFactory; | ||
$this->tokenStorage = $tokenStorage; | ||
$this->currentUserProvider = $currentUserProvider; | ||
$this->addressViewFactory = $addressViewFactory; | ||
$this->addressRepository = $addressRepository; | ||
$this->loggedInUserProvider = $loggedInUserProvider; | ||
} | ||
|
||
/** | ||
|
@@ -83,13 +97,40 @@ 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->loggedInUserProvider->provide(); | ||
} catch (TokenNotFoundException $exception) { | ||
return $this->viewHandler->handle(View::create(null, Response::HTTP_UNAUTHORIZED)); | ||
} | ||
|
||
if (($customer = $user->getCustomer()) !== null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not needed doubled brackets |
||
$this->bus->handle(new CreateAddress($addressModel, $user->getEmail())); | ||
|
||
$view = View::create($this->getLastInsertedAddress($customer), Response::HTTP_CREATED); | ||
} else { | ||
$view = View::create(['message' => 'The user is not a customer'], Response::HTTP_BAD_REQUEST); | ||
} | ||
|
||
$this->bus->handle(new CreateAddress($addressModel, $user->getEmail())); | ||
return $this->viewHandler->handle($view); | ||
} | ||
|
||
/** | ||
* Returns the id that was inserted last in the address book | ||
* | ||
* @param Customer $customer | ||
* | ||
* @return AddressBookView | ||
*/ | ||
private function getLastInsertedAddress(Customer $customer): AddressBookView | ||
{ | ||
$addresses = $this->addressRepository->findByCustomer($customer); | ||
|
||
return $this->viewHandler->handle(View::create(null, Response::HTTP_NO_CONTENT)); | ||
return $this->addressViewFactory->create(end($addresses), $customer); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be 404, I guess
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As you may have noticed I have added authorization symbols to the documentation. And the address book is something that only logged in users can see. If you are not logged in and try to call a route where you need to be logged in I wanted to return a 401 Unauthorized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose it is not the scope of this PR anyway?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The change in the documentation? That is already implemented. But the only parts that are behind a login wall are the address book and some of the customer endpoints.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I was just confused, that we are handling error with
500
code. But this is totally not part of this PR. I'm just missing some part why, but this is rather general question about current codebase :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True, the problem with the API was that assertions throw exceptions which are not very helpful to a user.