Permalink
Browse files

Episode 50

  • Loading branch information...
JeffreyWay committed Jun 8, 2017
1 parent 10dd72a commit c8450fd78b3695810f61b92e77dc21f19efd036c
@@ -2,6 +2,7 @@
namespace App\Http\Controllers;
use App\Inspections\Spam;
use App\Reply;
use App\Thread;
@@ -31,11 +32,13 @@ public function index($channelId, Thread $thread)
*
* @param integer $channelId
* @param Thread $thread
* @param Spam $spam
* @return \Illuminate\Http\RedirectResponse
*/
public function store($channelId, Thread $thread)
public function store($channelId, Thread $thread, Spam $spam)
{
$this->validate(request(), ['body' => 'required']);
$spam->detect(request('body'));
$reply = $thread->addReply([
'body' => request('body'),
@@ -0,0 +1,32 @@
<?php
namespace App\Inspections;
use Exception;
class InvalidKeywords
{
/**
* All registered invalid keywords.
*
* @var array
*/
protected $keywords = [
'yahoo customer support'
];
/**
* Detect spam.
*
* @param string $body
* @throws \Exception
*/
public function detect($body)
{
foreach ($this->keywords as $keyword) {
if (stripos($body, $keyword) !== false) {
throw new Exception('Your reply contains spam.');
}
}
}
}
@@ -0,0 +1,21 @@
<?php
namespace App\Inspections;
use Exception;
class KeyHeldDown
{
/**
* Detect spam.
*
* @param string $body
* @throws \Exception
*/
public function detect($body)
{
if (preg_match('/(.)\\1{4,}/', $body)) {
throw new Exception('Your reply contains spam.');
}
}
}
@@ -0,0 +1,31 @@
<?php
namespace App\Inspections;
class Spam
{
/**
* All registered inspections.
*
* @var array
*/
protected $inspections = [
InvalidKeywords::class,
KeyHeldDown::class
];
/**
* Detect spam.
*
* @param string $body
* @return bool
*/
public function detect($body)
{
foreach ($this->inspections as $inspection) {
app($inspection)->detect($body);
}
return false;
}
}
@@ -10,7 +10,7 @@
<div class="level">
<h4 class="flex">
<a href="{{ $thread->path() }}">
@if ($thread->hasUpdatesFor(auth()->user()))
@if (auth()->check() && $thread->hasUpdatesFor(auth()->user()))
<strong>
{{ $thread->title }}
</strong>
@@ -98,4 +98,19 @@ function authorized_users_can_update_replies()
$this->assertDatabaseHas('replies', ['id' => $reply->id, 'body' => $updatedReply]);
}
/** @test */
function replies_that_contain_spam_may_not_be_created()
{
$this->signIn();
$thread = create('App\Thread');
$reply = make('App\Reply', [
'body' => 'Yahoo Customer Support'
]);
$this->expectException(\Exception::class);
$this->post($thread->path() . '/replies', $reply->toArray());
}
}
@@ -0,0 +1,32 @@
<?php
namespace Tests\Feature;
use App\Inspections\Spam;
use Tests\TestCase;
class SpamTest extends TestCase
{
/** @test */
public function it_checks_for_invalid_keywords()
{
$spam = new Spam();
$this->assertFalse($spam->detect('Innocent reply here'));
$this->expectException('Exception');
$spam->detect('yahoo customer support');
}
/** @test */
function it_checks_for_any_key_being_held_down()
{
$spam = new Spam();
$this->expectException('Exception');
$spam->detect('Hello world aaaaaaaaa');
}
}

0 comments on commit c8450fd

Please sign in to comment.