Passed
Push — master ( 68f720...5e727b )
by Paul
10:53
created

ReviewLimitsValidator   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Test Coverage

Coverage 95.12%

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 14
eloc 33
c 4
b 1
f 1
dl 0
loc 99
ccs 39
cts 41
cp 0.9512
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A filterSqlClauseOperator() 0 3 1
A validateLimit() 0 12 3
A validateByUsername() 0 10 2
A validateByEmail() 0 5 1
A validateByIpAddress() 0 5 1
A isWhitelisted() 0 6 2
A isValid() 0 6 2
A performValidation() 0 4 2
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules\Validator;
4
5
use GeminiLabs\SiteReviews\Helper;
6
7
class ReviewLimitsValidator extends ValidatorAbstract
8
{
9
    /**
10
     * @return string
11
     */
12
    public function filterSqlClauseOperator()
13
    {
14
        return 'AND';
15
    }
16
17
    /**
18
     * @return bool
19
     */
20 1
    public function isValid()
21
    {
22 1
        $method = Helper::buildMethodName(glsr_get_option('submissions.limit'), 'validateBy');
23 1
        return method_exists($this, $method)
24 1
            ? call_user_func([$this, $method])
25 1
            : true;
26
    }
27
28
    /**
29
     * @return void
30
     */
31 1
    public function performValidation()
32
    {
33 1
        if (!$this->isValid()) {
34 1
            $this->setErrors(__('You have already submitted a review.', 'site-reviews'));
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

34
            $this->setErrors(/** @scrutinizer ignore-call */ __('You have already submitted a review.', 'site-reviews'));
Loading history...
35
        }
36 1
    }
37
38
    /**
39
     * @param string $value
40
     * @param string $whitelist
41
     * @return bool
42
     */
43 1
    protected function isWhitelisted($value, $whitelist)
44
    {
45 1
        if (empty($whitelist)) {
46 1
            return false;
47
        }
48 1
        return in_array($value, array_filter(explode("\n", $whitelist), 'trim'));
49
    }
50
51
    /**
52
     * @return bool
53
     */
54 1
    protected function validateByEmail()
55
    {
56 1
        glsr_log()->debug('Email is: '.$this->request->email);
57 1
        return $this->validateLimit('email', $this->request->email, [
58 1
            'email' => $this->request->email,
59
        ]);
60
    }
61
62
    /**
63
     * @return bool
64
     */
65 1
    protected function validateByIpAddress()
66
    {
67 1
        glsr_log()->debug('IP Address is: '.$this->request->ip_address);
68 1
        return $this->validateLimit('ip_address', $this->request->ip_address, [
69 1
            'ip_address' => $this->request->ip_address,
70
        ]);
71
    }
72
73
    /**
74
     * @return bool
75
     */
76 1
    protected function validateByUsername()
77
    {
78 1
        $user = wp_get_current_user();
0 ignored issues
show
Bug introduced by
The function wp_get_current_user was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

78
        $user = /** @scrutinizer ignore-call */ wp_get_current_user();
Loading history...
79 1
        if (!$user->exists()) {
80 1
            return true;
81
        }
82 1
        glsr_log()->debug('Username is: '.$user->user_login);
83 1
        return $this->validateLimit('username', $user->user_login, [
84 1
            'status' => 'all',
85 1
            'author_id' => $user->ID,
86
        ]);
87
    }
88
89
    /**
90
     * @param string $key
91
     * @param string $value
92
     * @return bool
93
     */
94 1
    protected function validateLimit($key, $value, array $queryArgs)
95
    {
96 1
        if (empty($value) 
97 1
            || $this->isWhitelisted($value, glsr_get_option('submissions.limit_whitelist.'.$key))) {
98 1
            return true;
99
        }
100 1
        $queryArgs['assigned_posts'] = $this->request->assigned_posts;
101 1
        add_filter('query/sql/clause/operator', [$this, 'filterSqlClauseOperator']);
0 ignored issues
show
Bug introduced by
The function add_filter was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

101
        /** @scrutinizer ignore-call */ 
102
        add_filter('query/sql/clause/operator', [$this, 'filterSqlClauseOperator']);
Loading history...
102 1
        $reviews = glsr_get_reviews($queryArgs);
103 1
        remove_filter('query/sql/clause/operator', [$this, 'filterSqlClauseOperator']);
0 ignored issues
show
Bug introduced by
The function remove_filter was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

103
        /** @scrutinizer ignore-call */ 
104
        remove_filter('query/sql/clause/operator', [$this, 'filterSqlClauseOperator']);
Loading history...
104 1
        $result = 0 === $reviews->total;
105 1
        return glsr()->filterBool('validate/review-limits', $result, $reviews, $this->request, $key);
106
    }
107
}
108