Passed
Push — develop ( 391f8f...187ad6 )
by Paul
13:53
created

BlacklistValidator   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
eloc 30
c 0
b 0
f 0
dl 0
loc 53
ccs 35
cts 35
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A performValidation() 0 10 3
A isValid() 0 11 1
A validateBlacklist() 0 17 6
A blacklist() 0 5 2
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules\Validator;
4
5
use GeminiLabs\SiteReviews\Database\OptionManager;
6
7
class BlacklistValidator extends ValidatorAbstract
8
{
9 1
    public function isValid(): bool
10
    {
11 1
        $target = implode("\n", array_filter([
12 1
            $this->request->name,
13 1
            $this->request->content,
14 1
            $this->request->email,
15 1
            $this->request->ip_address,
16 1
            $this->request->title,
17 1
        ]));
18 1
        $isValid = $this->validateBlacklist($target);
19 1
        return glsr()->filterBool('validate/blacklist', $isValid, $target, $this->request);
20
    }
21
22 1
    public function performValidation(): void
23
    {
24 1
        if (!$this->isValid()) {
25 1
            if ('reject' !== glsr_get_option('forms.blacklist.action')) {
26 1
                glsr()->sessionSet('form_blacklisted', true);
27 1
                return;
28
            }
29 1
            $this->fail(
30 1
                __('Your review cannot be submitted at this time.', 'site-reviews'),
31 1
                'Blacklisted submission detected.'
32 1
            );
33
        }
34
    }
35
36 1
    protected function blacklist(): string
37
    {
38 1
        return 'comments' === glsr_get_option('forms.blacklist.integration')
39 1
            ? trim(glsr(OptionManager::class)->wp('disallowed_keys'))
40 1
            : trim(glsr_get_option('forms.blacklist.entries'));
41
    }
42
43 1
    protected function validateBlacklist(string $target): bool
44
    {
45 1
        if (empty($blacklist = $this->blacklist())) {
46 1
            return true;
47
        }
48 1
        $lines = explode("\n", $blacklist);
49 1
        foreach ((array) $lines as $line) {
50 1
            $line = trim($line);
51 1
            if (empty($line) || 256 < strlen($line)) {
52 1
                continue;
53
            }
54 1
            $pattern = sprintf('#%s#iu', preg_quote($line, '#'));
55 1
            if (preg_match($pattern, $target)) {
56 1
                return false;
57
            }
58
        }
59 1
        return true;
60
    }
61
}
62