Skip to content

Commit

Permalink
Merge branch 'feature/password-reset-merge' into feature/password-reset
Browse files Browse the repository at this point in the history
  • Loading branch information
artstorm committed Jul 23, 2015
2 parents c2dcff5 + d7689a2 commit f68f670
Show file tree
Hide file tree
Showing 72 changed files with 2,882 additions and 1,573 deletions.
4 changes: 2 additions & 2 deletions api/.local.env
Expand Up @@ -10,14 +10,14 @@ APP_HOSTNAME=local.api.spira.io
AUTH_MODEL=App\Models\User

DB_CONNECTION=pgsql
DB_HOST=%DB_PORT_5432_TCP_ADDR%
DB_HOST=%DATABASE_PORT_5432_TCP_ADDR%
DB_PORT=5432
DB_DATABASE=spira
DB_USERNAME=spira
DB_PASSWORD=spira

CACHE_DRIVER=redis
REDIS_HOST=%REDIS_PORT_6379_TCP_ADDR%
REDIS_HOST=%CACHE_PORT_6379_TCP_ADDR%
REDIS_PORT=6379

WEBSERVER_HOST=%WEB_PORT_8080_TCP_ADDR%
Expand Down
2 changes: 0 additions & 2 deletions api/.travis.env
Expand Up @@ -24,8 +24,6 @@ SESSION_DRIVER=array
QUEUE_DRIVER=beanstalkd
BEANSTALKD_HOST=127.0.0.1

MAIL_DRIVER=log

MAIL_DRIVER=smtp
MAIL_HOST=localhost
MAIL_PORT=1025
Expand Down
2 changes: 1 addition & 1 deletion api/app/Console/Commands/GenerateKeysCommand.php
Expand Up @@ -25,7 +25,7 @@ class GenerateKeysCommand extends Command
/**
* Filesystem.
*
* @var Illuminate\Filesystem\Filesystem
* @var Filesystem
*/
protected $file;

Expand Down
50 changes: 44 additions & 6 deletions api/app/Exceptions/Handler.php
Expand Up @@ -2,9 +2,12 @@

namespace App\Exceptions;

use App\Http\Controllers\BaseController;
use App\Http\Transformers\IlluminateModelTransformer;
use Exception;
use Laravel\Lumen\Exceptions\Handler as ExceptionHandler;
use Spira\Responder\Contract\TransformableInterface;
use Spira\Responder\Contract\TransformerInterface;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;

class Handler extends ExceptionHandler
{
Expand All @@ -22,13 +25,12 @@ class Handler extends ExceptionHandler
*
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
*
* @param \Exception $e
* @param \Exception $e
*
* @return void
*/
public function report(Exception $e)
{
return parent::report($e);
parent::report($e);
}

/**
Expand All @@ -41,8 +43,44 @@ public function report(Exception $e)
*/
public function render($request, Exception $e)
{
return BaseController::renderException($request, $e, env('APP_DEBUG', false));
$debug = env('APP_DEBUG', false);

// return parent::render($request, $e);
$message = $e->getMessage();
if (!$message) {
$message = 'An error occurred';
}

$debugData = [
'exception' => get_class($e),
'message' => $e->getMessage(),
'code' => $e->getCode(),
'file' => $e->getFile(),
'line' => $e->getLine(),
'trace' => explode("\n", $e->getTraceAsString()),
];

$response = [
'message' => $message,
];

$statusCode = 500;

if ($e instanceof HttpExceptionInterface) {
$statusCode = $e->getStatusCode();

if (method_exists($e, 'getResponse')) {
if ($e instanceof TransformableInterface) {
$response = $e->transform(\App::make(IlluminateModelTransformer::class));
} else {
$response = $e->getResponse();
}
}
}

if ($debug) {
$response['debug'] = $debugData;
}

return response()->json($response, $statusCode, [], JSON_PRETTY_PRINT);
}
}
35 changes: 31 additions & 4 deletions api/app/Exceptions/ValidationException.php
Expand Up @@ -3,23 +3,23 @@
namespace App\Exceptions;

use Illuminate\Support\MessageBag;
use Spira\Responder\Contract\TransformableInterface;
use Spira\Responder\Contract\TransformerInterface;
use Symfony\Component\HttpKernel\Exception\HttpException;

class ValidationException extends HttpException
class ValidationException extends HttpException implements TransformableInterface
{
/**
* The validation error messages.
*
* @var \Illuminate\Support\MessageBag
* @var MessageBag
*/
protected $errors;

/**
* Create a new validation exception instance.
*
* @param \Illuminate\Support\MessageBag
*
* @return void
*/
public function __construct(MessageBag $errors)
{
Expand Down Expand Up @@ -48,4 +48,31 @@ public function getResponse()
'invalid' => $this->errors,
];
}

/**
* Returns response headers.
*
* @return array Response headers
*/
public function getHeaders()
{
return [];
}

/**
* @param TransformerInterface $transformer
* @return mixed
*/
public function transform(TransformerInterface $transformer)
{
return $transformer->transformItem($this->getResponse());
}

/**
* @return MessageBag
*/
public function getErrors()
{
return $this->errors;
}
}
64 changes: 64 additions & 0 deletions api/app/Exceptions/ValidationExceptionCollection.php
@@ -0,0 +1,64 @@
<?php
/**
* Created by PhpStorm.
* User: redjik
* Date: 17.07.15
* Time: 20:12
*/

namespace App\Exceptions;

use HttpException;
use Spira\Responder\Contract\TransformableInterface;
use Spira\Responder\Contract\TransformerInterface;

class ValidationExceptionCollection extends HttpException implements TransformableInterface
{
/**
* @var ValidationException[]
*/
private $exceptions;

public function __construct(array $exceptions)
{
$this->exceptions = $exceptions;
}

/**
* Returns the status code.
*
* @return int An HTTP response status code
*/
public function getStatusCode()
{
return 422;
}

/**
* Return the response instance.
*
* @return array
*/
public function getResponse()
{
$responses = [];
foreach ($this->exceptions as $exception) {
if (!is_null($exception)) {
$responses[] = $exception->getResponse();
} else {
$responses[] = null;
}
}

return $responses;
}

/**
* @param TransformerInterface $transformer
* @return mixed
*/
public function transform(TransformerInterface $transformer)
{
return $transformer->transformCollection($this->getResponse());
}
}
33 changes: 33 additions & 0 deletions api/app/Helpers/RouteHelper.php
@@ -0,0 +1,33 @@
<?php
/**
* Created by PhpStorm.
* User: redjik
* Date: 17.07.15
* Time: 18:19
*/

namespace App\Helpers;

use Spira\Repository\Model\BaseModel;

class RouteHelper
{
public static $badRoutes = [];

/**
* @param BaseModel $model
* @return string|false
*/
public static function getRoute(BaseModel $model)
{
if (!isset(static::$badRoutes[get_class($model)])) {
try {
return route(get_class($model), ['id'=>$model->getQueueableId()]);
} catch (\InvalidArgumentException $e) {
static::$badRoutes[get_class($model)] = true;
}
}

return false;
}
}

0 comments on commit f68f670

Please sign in to comment.