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

DefaultValidator   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Test Coverage

Coverage 76.47%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 7
eloc 36
c 4
b 1
f 0
dl 0
loc 78
ccs 26
cts 34
cp 0.7647
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A normalizedRules() 0 6 1
A performValidation() 0 8 2
A isValidRequest() 0 10 2
A isValid() 0 7 1
A rules() 0 12 1
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules\Validator;
4
5
use GeminiLabs\SiteReviews\Database\DefaultsManager;
6
use GeminiLabs\SiteReviews\Defaults\ValidateReviewDefaults;
7
use GeminiLabs\SiteReviews\Helpers\Arr;
8
use GeminiLabs\SiteReviews\Helpers\Cast;
9
use GeminiLabs\SiteReviews\Modules\Rating;
10
use GeminiLabs\SiteReviews\Modules\Validator;
11
use GeminiLabs\SiteReviews\Request;
12
13
class DefaultValidator extends ValidatorAbstract
14
{
15
    const VALIDATION_RULES = [
16
        'content' => 'required',
17
        'email' => 'required|email',
18
        'name' => 'required',
19
        'rating' => 'required|number|between:1,5',
20
        'terms' => 'accepted',
21
        'title' => 'required',
22
    ];
23
24
    /**
25
     * @return bool
26
     */
27 2
    public function isValid()
28
    {
29 2
        $this->errors = glsr(Validator::class)->validate(
30 2
            $this->request->toArray(),
31 2
            $this->rules()
32
        );
33 2
        return empty($this->errors);
34
    }
35
36
    /**
37
     * This only validates the provided values in the Request
38
     * @return bool
39
     */
40
    public function isValidRequest()
41
    {
42
        $options = glsr(DefaultsManager::class)->pluck('settings.submissions.required.options');
43
        $excludedKeys = array_keys(array_diff_key($options, $this->request->toArray()));
44
        $this->request->excluded = $excludedKeys;
45
        if ($this->isValid()) {
46
            return true;
47
        }
48
        glsr_log()->warning($this->errors);
49
        return false;
50
    }
51
52
    /**
53
     * @return void
54
     */
55 2
    public function performValidation()
56
    {
57 2
        if (!$this->isValid()) {
58 2
            $this->setErrors(__('Please fix the submission errors.', '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

58
            $this->setErrors(/** @scrutinizer ignore-call */ __('Please fix the submission errors.', 'site-reviews'));
Loading history...
59 2
            return;
60
        }
61 1
        $values = glsr(ValidateReviewDefaults::class)->merge($this->request->toArray());
62 1
        $this->request = new Request($values);
63 1
    }
64
65
    /**
66
     * @return array
67
     */
68 2
    protected function normalizedRules()
69
    {
70 2
        $rules = static::VALIDATION_RULES;
71 2
        $maxRating = max(1, Cast::toInt(glsr()->constant('MAX_RATING', Rating::class)));
72 2
        $rules['rating'] = str_replace('between:1,5', 'between:1,'.$maxRating, $rules['rating']);
73 2
        return glsr()->filterArray('validation/rules', $rules, $this->request);
74
    }
75
76
    /**
77
     * @return array
78
     */
79 2
    protected function rules()
80
    {
81 2
        $rules = $this->normalizedRules();
82 2
        $customRules = array_diff_key($rules,
83 2
            glsr(DefaultsManager::class)->pluck('settings.submissions.required.options')
84
        );
85 2
        $requiredRules = array_intersect_key($rules,
86 2
            array_flip(glsr_get_option('submissions.required', []))
87
        );
88 2
        $rules = array_merge($requiredRules, $customRules);
89 2
        $excluded = Arr::convertFromString($this->request->excluded); // these fields were ommited with the hide option
90 2
        return array_diff_key($rules, array_flip($excluded));
91
    }
92
}
93