Skip to content

Commit ebdfdec

Browse files
committed
Log spam
1 parent e7066e0 commit ebdfdec

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

app/Lio/Forum/Threads/ThreadCreator.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use Illuminate\Support\MessageBag;
44
use Lio\Accounts\User;
55
use Lio\Content\SpamDetector;
6+
use Psr\Log\LoggerInterface;
67

78
/**
89
* This class can call the following methods on the listener object:
@@ -22,14 +23,21 @@ class ThreadCreator
2223
*/
2324
private $spamDetector;
2425

26+
/**
27+
* @var \Psr\Log\LoggerInterface
28+
*/
29+
private $logger;
30+
2531
/**
2632
* @param \Lio\Forum\Threads\ThreadRepository $threads
2733
* @param \Lio\Content\SpamDetector $spamDetector
34+
* @param \Psr\Log\LoggerInterface $logger
2835
*/
29-
public function __construct(ThreadRepository $threads, SpamDetector $spamDetector)
36+
public function __construct(ThreadRepository $threads, SpamDetector $spamDetector, LoggerInterface $logger)
3037
{
3138
$this->threads = $threads;
3239
$this->spamDetector = $spamDetector;
40+
$this->logger = $logger;
3341
}
3442

3543
// an additional validator is optional and will be run first, an example of a usage for
@@ -39,6 +47,7 @@ public function create(ThreadCreatorListener $listener, $data, $validator = null
3947
if ($validator && ! $validator->isValid()) {
4048
return $listener->threadCreationError($validator->getErrors());
4149
}
50+
4251
return $this->createValidRecord($listener, $data);
4352
}
4453

@@ -59,6 +68,8 @@ private function getNew($data)
5968
private function validateAndSave($thread, $listener, $data)
6069
{
6170
if ($this->spamDetector->detectsSpam($thread->subject)) {
71+
$this->logSpam($thread->subject, $thread->author);
72+
6273
$this->increaseUserSpamCount($thread->author);
6374

6475
return $listener->threadCreationError(
@@ -67,6 +78,8 @@ private function validateAndSave($thread, $listener, $data)
6778
}
6879

6980
if ($this->spamDetector->detectsSpam($thread->body)) {
81+
$this->logSpam($thread->body, $thread->author);
82+
7083
$this->increaseUserSpamCount($thread->author);
7184

7285
return $listener->threadCreationError(
@@ -103,4 +116,14 @@ private function increaseUserSpamCount(User $user)
103116

104117
$user->save();
105118
}
119+
120+
/**
121+
* @param string $spam
122+
* @param \Lio\Accounts\User $author
123+
*/
124+
private function logSpam($spam, User $author)
125+
{
126+
$this->logger->warning("Warning: attempted spam posted by [#{$author->id}] {$author->name}:");
127+
$this->logger->warning($spam);
128+
}
106129
}

0 commit comments

Comments
 (0)