Test Failed
Push — master ( 31c635...ef6440 )
by Paul
07:17 queued 01:32
created

BlacklistValidator   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 66
ccs 0
cts 48
cp 0
rs 10
c 0
b 0
f 0
wmc 12

4 Methods

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