3
3
use Illuminate \Support \MessageBag ;
4
4
use Lio \Accounts \User ;
5
5
use Lio \Content \SpamDetector ;
6
+ use Psr \Log \LoggerInterface ;
6
7
7
8
/**
8
9
* This class can call the following methods on the listener object:
@@ -22,14 +23,21 @@ class ThreadCreator
22
23
*/
23
24
private $ spamDetector ;
24
25
26
+ /**
27
+ * @var \Psr\Log\LoggerInterface
28
+ */
29
+ private $ logger ;
30
+
25
31
/**
26
32
* @param \Lio\Forum\Threads\ThreadRepository $threads
27
33
* @param \Lio\Content\SpamDetector $spamDetector
34
+ * @param \Psr\Log\LoggerInterface $logger
28
35
*/
29
- public function __construct (ThreadRepository $ threads , SpamDetector $ spamDetector )
36
+ public function __construct (ThreadRepository $ threads , SpamDetector $ spamDetector, LoggerInterface $ logger )
30
37
{
31
38
$ this ->threads = $ threads ;
32
39
$ this ->spamDetector = $ spamDetector ;
40
+ $ this ->logger = $ logger ;
33
41
}
34
42
35
43
// 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
39
47
if ($ validator && ! $ validator ->isValid ()) {
40
48
return $ listener ->threadCreationError ($ validator ->getErrors ());
41
49
}
50
+
42
51
return $ this ->createValidRecord ($ listener , $ data );
43
52
}
44
53
@@ -59,6 +68,8 @@ private function getNew($data)
59
68
private function validateAndSave ($ thread , $ listener , $ data )
60
69
{
61
70
if ($ this ->spamDetector ->detectsSpam ($ thread ->subject )) {
71
+ $ this ->logSpam ($ thread ->subject , $ thread ->author );
72
+
62
73
$ this ->increaseUserSpamCount ($ thread ->author );
63
74
64
75
return $ listener ->threadCreationError (
@@ -67,6 +78,8 @@ private function validateAndSave($thread, $listener, $data)
67
78
}
68
79
69
80
if ($ this ->spamDetector ->detectsSpam ($ thread ->body )) {
81
+ $ this ->logSpam ($ thread ->body , $ thread ->author );
82
+
70
83
$ this ->increaseUserSpamCount ($ thread ->author );
71
84
72
85
return $ listener ->threadCreationError (
@@ -103,4 +116,14 @@ private function increaseUserSpamCount(User $user)
103
116
104
117
$ user ->save ();
105
118
}
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
+ }
106
129
}
0 commit comments