Passed
Push — master ( ab2678...bf154d )
by webdevetc
14:17
created

Basic   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 9
eloc 17
dl 0
loc 58
rs 10
c 4
b 1
f 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 3
A captchaFieldName() 0 3 1
A captcha_field_name() 0 3 1
A view() 0 3 1
A rules() 0 19 3
1
<?php
2
3
/** @noinspection PhpInconsistentReturnPointsInspection */
4
5
namespace WebDevEtc\BlogEtc\Captcha;
6
7
use DomainException;
8
9
/**
10
 * Class Basic.
11
 *
12
 * Basic anti spam captcha
13
 */
14
class Basic extends CaptchaAbstract
15
{
16
    public function __construct()
17
    {
18
        if (!config('blogetc.captcha.basic_question') || !config('blogetc.captcha.basic_answers')) {
19
            throw new DomainException('Invalid question or answers for captcha');
20
        }
21
    }
22
23
    /**
24
     * What view file should we use for the captcha field?
25
     */
26
    public function view(): string
27
    {
28
        return 'blogetc::captcha.basic';
29
    }
30
31
    /**
32
     * What rules should we use for the validation for this field?
33
     *
34
     * Enter the rules here, along with captcha validation.
35
     */
36
    public function rules(): array
37
    {
38
        $checkAnswer = static function ($attribute, $value, $fail) {
39
            $answers = config('blogetc.captcha.basic_answers');
40
41
            $value = strtolower(trim($value));
42
            $answers = strtolower($answers);
43
44
            $answersArray = array_map('trim', explode(',', $answers));
45
46
            if (!$value || !in_array($value, $answersArray, true)) {
47
                return $fail('The captcha field is incorrect.');
48
            }
49
        };
50
51
        return [
52
            'required',
53
            'string',
54
            $checkAnswer,
55
        ];
56
    }
57
58
    /**
59
     * @deprecated - please use captchaFieldName() instead
60
     */
61
    public function captcha_field_name(): string
62
    {
63
        return $this->captchaFieldName();
64
    }
65
66
    /**
67
     * What should the field name be (in the <input type='text' name='????'>).
68
     */
69
    public function captchaFieldName(): string
70
    {
71
        return 'captcha';
72
    }
73
}
74