Passed
Push — master ( 6b8ca8...3384db )
by Paul
04:57
created

Blacklist::check()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 0
cts 16
cp 0
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 11
nc 5
nop 1
crap 42
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules;
4
5
use GeminiLabs\SiteReviews\Commands\SubmitReview;
6
use GeminiLabs\SiteReviews\Database\OptionManager;
7
8
class Blacklist
9
{
10
	/**
11
	 * @return bool
12
	 */
13
	public function isBlacklisted( SubmitReview $review )
14
	{
15
		$target = implode( "\n", array_filter([
16
			$review->author,
17
			$review->content,
18
			$review->email,
19
			$review->ipAddress,
20
			$review->title,
21
		]));
22
		return (bool) apply_filters( 'site-reviews/blacklist/is-blacklisted',
23
			$this->check( $target ),
24
			$review
25
		);
26
	}
27
28
	/**
29
	 * @param string $target
30
	 * @return bool
31
	 */
32
	protected function check( $target )
33
	{
34
		$blacklist = trim( glsr( OptionManager::class )->get( 'settings.reviews-form.blacklist.entries' ));
35
		if( empty( $blacklist )) {
36
			return false;
37
		}
38
		$lines = explode( "\n", $blacklist );
39
		foreach( (array) $lines as $line ) {
40
			$line = trim( $line );
41
			if( empty( $line ) || 256 < strlen( $line ))continue;
42
			$pattern = sprintf( '#%s#i', preg_quote( $line, '#' ));
43
			if( preg_match( $pattern, $target )) {
44
				return true;
45
			}
46
		}
47
		return false;
48
	}
49
}
50