Passed
Push — master ( bd753d...066495 )
by Darko
10:20
created

CaptchaHelper::getResponseFieldName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
dl 0
loc 9
rs 10
c 1
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace App\Support;
4
5
use App\Services\TurnstileService;
6
7
class CaptchaHelper
8
{
9
    /**
10
     * Check if captcha is enabled
11
     */
12
    public static function isEnabled(): bool
13
    {
14
        $provider = config('captcha.provider', 'recaptcha');
15
16
        // Enforce that both providers cannot be enabled at the same time
17
        $recaptchaEnabled = config('captcha.recaptcha.enabled') === true
18
            && ! empty(config('captcha.recaptcha.sitekey'))
19
            && ! empty(config('captcha.recaptcha.secret'));
20
21
        $turnstileEnabled = config('captcha.turnstile.enabled') === true
22
            && ! empty(config('captcha.turnstile.sitekey'))
23
            && ! empty(config('captcha.turnstile.secret'));
24
25
        // If both are configured as enabled, log a warning and use the configured provider
26
        if ($recaptchaEnabled && $turnstileEnabled) {
27
            \Log::warning('Both reCAPTCHA and Turnstile are enabled. Only one can be active at a time. Using provider: '.$provider);
28
        }
29
30
        if ($provider === 'turnstile') {
31
            return $turnstileEnabled;
32
        }
33
34
        // Default to reCAPTCHA
35
        return $recaptchaEnabled || config('captcha.enabled') === true
36
            && ! empty(config('captcha.sitekey'))
37
            && ! empty(config('captcha.secret'));
38
    }
39
40
    /**
41
     * Get the current captcha provider
42
     */
43
    public static function getProvider(): string
44
    {
45
        return config('captcha.provider', 'recaptcha');
46
    }
47
48
    /**
49
     * Display the captcha widget
50
     */
51
    public static function display(array $attributes = []): string
52
    {
53
        if (! self::isEnabled()) {
54
            return '';
55
        }
56
57
        $provider = self::getProvider();
58
59
        if ($provider === 'turnstile') {
60
            return TurnstileService::display($attributes);
61
        }
62
63
        // Default to reCAPTCHA
64
        return app('captcha')->display($attributes);
65
    }
66
67
    /**
68
     * Render the captcha JavaScript
69
     */
70
    public static function renderJs(): string
71
    {
72
        if (! self::isEnabled()) {
73
            return '';
74
        }
75
76
        $provider = self::getProvider();
77
78
        if ($provider === 'turnstile') {
79
            return TurnstileService::renderJs();
80
        }
81
82
        // Default to reCAPTCHA
83
        return app('captcha')->renderJs();
84
    }
85
86
    /**
87
     * Get the captcha response field name
88
     */
89
    public static function getResponseFieldName(): string
90
    {
91
        $provider = self::getProvider();
92
93
        if ($provider === 'turnstile') {
94
            return 'cf-turnstile-response';
95
        }
96
97
        return 'g-recaptcha-response';
98
    }
99
100
    /**
101
     * Get validation rules for captcha
102
     */
103
    public static function getValidationRules(): array
104
    {
105
        if (! self::isEnabled()) {
106
            return [];
107
        }
108
109
        $provider = self::getProvider();
110
        $fieldName = self::getResponseFieldName();
111
112
        if ($provider === 'turnstile') {
113
            return [
114
                $fieldName => [
115
                    'required',
116
                    new \App\Rules\TurnstileRule,
0 ignored issues
show
Bug introduced by
The type App\Rules\TurnstileRule was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
117
                ],
118
            ];
119
        }
120
121
        // Default to reCAPTCHA
122
        return [
123
            $fieldName => [
124
                'required',
125
                'captcha',
126
            ],
127
        ];
128
    }
129
}
130