Permalink
Browse files

Episode 53

  • Loading branch information...
JeffreyWay committed Jun 27, 2017
1 parent b4b4fae commit ffad0f953de3823eac526e912aa67fd8cf4a3e67
@@ -2,7 +2,6 @@
namespace App\Http\Controllers;
use App\Inspections\Spam;
use App\Reply;
use App\Thread;
@@ -37,7 +36,7 @@ public function index($channelId, Thread $thread)
public function store($channelId, Thread $thread)
{
try {
$this->validateReply();
$this->validate(request(), ['body' => 'required|spamfree']);
$reply = $thread->addReply([
'body' => request('body'),
@@ -62,7 +61,7 @@ public function update(Reply $reply)
$this->authorize('update', $reply);
try {
$this->validateReply();
$this->validate(request(), ['body' => 'required|spamfree']);
$reply->update(request(['body']));
} catch (\Exception $e) {
@@ -91,14 +90,4 @@ public function destroy(Reply $reply)
return back();
}
/**
* Validate the incoming reply.
*/
protected function validateReply()
{
$this->validate(request(), ['body' => 'required']);
resolve(Spam::class)->detect(request('body'));
}
}
@@ -4,7 +4,6 @@
use App\Channel;
use App\Filters\ThreadFilters;
use App\Inspections\Spam;
use App\Thread;
use Illuminate\Http\Request;
@@ -50,19 +49,16 @@ public function create()
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param Spam $spam
* @return \Illuminate\Http\Response
*/
public function store(Request $request, Spam $spam)
public function store(Request $request)
{
$this->validate($request, [
'title' => 'required',
'body' => 'required',
'title' => 'required|spamfree',
'body' => 'required|spamfree',
'channel_id' => 'required|exists:channels,id'
]);
$spam->detect(request('body'));
$thread = Thread::create([
'user_id' => auth()->id(),
'channel_id' => request('channel_id'),
@@ -21,6 +21,8 @@ public function boot()
$view->with('channels', $channels);
});
\Validator::extend('spamfree', 'App\Rules\SpamFree@passes');
}
/**
@@ -0,0 +1,25 @@
<?php
namespace App\Rules;
use Exception;
use App\Inspections\Spam;
class SpamFree
{
/**
* Determine if the given attribute passes our spam validation.
*
* @param string $attribute
* @param string $value
* @return bool
*/
public function passes($attribute, $value)
{
try {
return ! resolve(Spam::class)->detect($value);
} catch (Exception $e) {
return false;
}
}
}
@@ -85,6 +85,7 @@
'unique' => 'The :attribute has already been taken.',
'uploaded' => 'The :attribute failed to upload.',
'url' => 'The :attribute format is invalid.',
'spamfree' => 'The :attribute contains spam.',
/*
|--------------------------------------------------------------------------

0 comments on commit ffad0f9

Please sign in to comment.