Permalink
Browse files

Episode 55

  • Loading branch information...
JeffreyWay committed Jul 5, 2017
1 parent 21412ff commit a8eb9d1d7acd50958684b687ca4ddd6d11d45c5a
@@ -5,6 +5,7 @@
use Exception;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Validation\ValidationException;
class Handler extends ExceptionHandler
{
@@ -44,6 +45,16 @@ public function report(Exception $exception)
*/
public function render($request, Exception $exception)
{
if ($exception instanceof ValidationException) {
if ($request->expectsJson()) {
return response('Sorry, validation failed.', 422);
}
}
if ($exception instanceof ThrottleException) {
return response($exception->getMessage(), 429);
}
return parent::render($request, $exception);
}
@@ -0,0 +1,8 @@
<?php
namespace App\Exceptions;
class ThrottleException extends \Exception
{
}
@@ -2,9 +2,9 @@
namespace App\Http\Controllers;
use App\Http\Requests\CreatePostRequest;
use App\Reply;
use App\Thread;
use Illuminate\Support\Facades\Gate;
class RepliesController extends Controller
{
@@ -30,32 +30,17 @@ public function index($channelId, Thread $thread)
/**
* Persist a new reply.
*
* @param integer $channelId
* @param Thread $thread
* @return \Illuminate\Database\Eloquent\Model|\Illuminate\Http\RedirectResponse
* @param integer $channelId
* @param Thread $thread
* @param CreatePostRequest $form
* @return \Illuminate\Database\Eloquent\Model
*/
public function store($channelId, Thread $thread)
public function store($channelId, Thread $thread, CreatePostRequest $form)
{
if (Gate::denies('create', new Reply)) {
return response(
'You are posting too frequently. Please take a break. :)', 429
);
}
try {
$this->validate(request(), ['body' => 'required|spamfree']);
$reply = $thread->addReply([
'body' => request('body'),
'user_id' => auth()->id()
]);
} catch (\Exception $e) {
return response(
'Sorry, your reply could not be saved at this time.', 422
);
}
return $reply->load('owner');
return $thread->addReply([
'body' => request('body'),
'user_id' => auth()->id()
])->load('owner');
}
/**
@@ -0,0 +1,46 @@
<?php
namespace App\Http\Requests;
use App\Exceptions\ThrottleException;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Gate;
class CreatePostRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return Gate::allows('create', new \App\Reply);
}
/**
* Handle a failed authorization attempt.
*
* @return void
*
* @throws ThrottleException
*/
protected function failedAuthorization()
{
throw new ThrottleException(
'You are replying too frequently. Please take a break.'
);
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'body' => 'required|spamfree'
];
}
}
@@ -102,20 +102,24 @@ function authorized_users_can_update_replies()
/** @test */
function replies_that_contain_spam_may_not_be_created()
{
$this->withExceptionHandling();
$this->signIn();
$thread = create('App\Thread');
$reply = make('App\Reply', [
'body' => 'Yahoo Customer Support'
]);
$this->post($thread->path() . '/replies', $reply->toArray())
$this->json('post', $thread->path() . '/replies', $reply->toArray())
->assertStatus(422);
}
/** @test */
function users_may_only_reply_a_maximum_of_once_per_minute()
{
$this->withExceptionHandling();
$this->signIn();
$thread = create('App\Thread');

0 comments on commit a8eb9d1

Please sign in to comment.