Skip to content

Commit

Permalink
Merge pull request #3 from sfelix-martins/analysis-qvMwNy
Browse files Browse the repository at this point in the history
Apply fixes from StyleCI
  • Loading branch information
sfelix-martins authored Sep 16, 2017
2 parents 135de16 + c47145f commit 7e94dc4
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 25 deletions.
28 changes: 12 additions & 16 deletions src/JsonHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,33 @@
namespace SMartins\JsonHandler;

use Exception;
use Illuminate\Validation\ValidationException;
use Illuminate\Auth\Access\AuthorizationException;
use SMartins\JsonHandler\Responses\JsonApiResponse;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

trait JsonHandler
{
use ValidationHandler, ModelNotFoundHandler, AuthorizationHandler, NotFoundHttpHandler;

/**
* Config file name
* Config file name.
* @var string
*/
public $configFile = 'json-exception-handler';

/**
* JsonApiResponse instance used on another traits to set response
* JsonApiResponse instance used on another traits to set response.
* @var SMartins\JsonHandler\Responses\JsonApiResponse;
*/
public $jsonApiResponse;

/**
* Receive exception instance to be used on methods
* Receive exception instance to be used on methods.
* @var Exception
*/
private $exception;

/**
* Set the default response on $response attribute. Get default value from
* methods
* methods.
*/
public function setDefaultResponse()
{
Expand All @@ -50,7 +46,7 @@ public function setDefaultResponse()
}

/**
* Get default message from exception
* Get default message from exception.
*
* @return string Exception message
*/
Expand All @@ -60,22 +56,22 @@ public function getMessage()
}

/**
* Mount the description with exception class, line and file
* Mount the description with exception class, line and file.
*
* @return string
*/
public function getDescription()
{
return class_basename($this->exception).
' line '. $this->exception->getLine().
' in '. basename($this->exception->getFile());
' line '.$this->exception->getLine().
' in '.basename($this->exception->getFile());
}

/**
* Get default http code. Check if exception has getStatusCode() methods.
* If not get from config file.
*
* @return integer
* @return int
*/
public function getStatusCode()
{
Expand All @@ -92,13 +88,13 @@ public function getStatusCode()
* Get error code. If code is empty from config file based on type.
*
* @param string $type Code type from config file
* @return integer
* @return int
*/
public function getCode($type = 'default')
{
$code = $this->exception->getCode();
if (empty($this->exception->getCode())) {
$code = config($this->configFile.'.codes.'. $type);
$code = config($this->configFile.'.codes.'.$type);
}

return $code;
Expand Down Expand Up @@ -132,7 +128,7 @@ public function jsonResponse(Exception $exception)
* Check if method to treat exception exists.
*
* @param Exception $exception The exception to be checked
* @return boolean If method is callable
* @return bool If method is callable
*/
public function exceptionIsTreated()
{
Expand Down
4 changes: 2 additions & 2 deletions src/JsonHandlerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class JsonHandlerServiceProvider extends ServiceProvider
public function boot()
{
$this->publishes([
$this->configPath() => config_path('json-exception-handler.php')
$this->configPath() => config_path('json-exception-handler.php'),
]);
}

Expand All @@ -20,6 +20,6 @@ public function register()

public function configPath()
{
return __DIR__ . '/config/json-exception-handler.php';
return __DIR__.'/config/json-exception-handler.php';
}
}
5 changes: 3 additions & 2 deletions src/ModelNotFoundHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
trait ModelNotFoundHandler
{
/**
* Set the response if Exception is ModelNotFound
* Set the response if Exception is ModelNotFound.
*
* @param ModelNotFoundException $exception
*/
Expand All @@ -20,7 +20,7 @@ public function modelNotFoundException(ModelNotFoundException $exception)
'status' => 404,
'code' => $this->getCode('model_not_found'),
'source' => ['pointer' => 'data/id'],
'title' => $entitie. ' not found.',
'title' => $entitie.' not found.',
'detail' => $exception->getMessage(),
]];

Expand All @@ -37,6 +37,7 @@ public function modelNotFoundException(ModelNotFoundException $exception)
public function extractEntitieName($model)
{
$entitieName = explode('\\', $model);

return end($entitieName);
}
}
4 changes: 2 additions & 2 deletions src/NotFoundHttpHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
trait NotFoundHttpHandler
{
/**
* Set response parameters to NotFoundHttpException
* Set response parameters to NotFoundHttpException.
*
* @param NotFoundHttpException $exception
*/
Expand All @@ -28,7 +28,7 @@ public function notFoundHttpException(NotFoundHttpException $exception)

/**
* Get message based on file. If file is RouteCollection return specific
* message
* message.
*
* @param NotFoundHttpException $exception
* @return string
Expand Down
4 changes: 2 additions & 2 deletions src/ValidationHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function validationException(ValidationException $exception)

/**
* Get formatted errors on standard code, field, message to each field with
* error
* error.
*
* @param ValidationException $exception
* @return array
Expand All @@ -32,7 +32,7 @@ public function formattedErrors(ValidationException $exception)
public function jsonApiFormatErrorMessages(ValidationException $exception)
{
$validationMessages = $this->getTreatedMessages($exception);
$validationFails = $this->getTreatedFails($exception);
$validationFails = $this->getTreatedFails($exception);

$errors = [];
foreach ($validationMessages as $field => $messages) {
Expand Down
2 changes: 1 addition & 1 deletion src/config/json-exception-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
],
'email' => [
'default' => 142,
'email' => 1421
'email' => 1421,
],
'password' => [
'default' => 143,
Expand Down

0 comments on commit 7e94dc4

Please sign in to comment.