More readable REST code
recently there was a new style of validating&throwing Exceptions introduced in our REST Controller (mostly by @LeoVie as far as I know?).
I had a chat with @NerdyProjects, we both think that it makes the code way more unreadable
style, which we believe to be difficult to read
public function joinRegionAction($regionId)
{
$this->validateRegionOrThrowException($regionId);
$region = $this->regionGateway->getBezirk($regionId);
return $this->handleView($view);
}
private function validateRegionOrThrowException($regionId)
{
$this->throwExceptionIfRegionDoesNotExist($regionId);
$this->throwExceptionIfJoiningRegionIsNotAllowed($regionId);
}
private function throwExceptionIfRegionDoesNotExist($regionId)
{
if (!$this->regionGateway->getBezirk($regionId)) {
throw new HttpException(404);
}
}
private function throwExceptionIfJoiningRegionIsNotAllowed($regionId)
{
if (!$this->regionPermissions->mayJoinRegion($regionId)) {
throw new HttpException(403);
}
}
cleaner solution
public function joinRegionAction($regionId)
{
if (!$this->regionGateway->getBezirk($regionId)) {
throw new HttpException(404);
}
if (!$this->regionPermissions->mayJoinRegion($regionId)) {
throw new HttpException(403);
}
$region = $this->regionGateway->getBezirk($regionId);
return $this->handleView($view);
}
If it is still unclear, why we think that, I can write a more detailed explanation! ;)