Passed
Push — typo3_11 ( 59ee00...f038b8 )
by Torben
07:22
created

getVerificationServer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Extension "sf_event_mgt" for TYPO3 CMS.
7
 *
8
 * For the full copyright and license information, please read the
9
 * LICENSE.txt file that was distributed with this source code.
10
 */
11
12
namespace DERHANSEN\SfEventMgt\Service;
13
14
use DERHANSEN\SfEventMgt\Exception\InvalidCaptchaConfigurationException;
15
16
/**
17
 * CaptchaConfiguration which supports hCaptcha and reCaptcha
18
 */
19
class CaptchaConfigurationService
20
{
21
    private const RESPONSE_FIELD = [
22
        'hCaptcha' => 'h-captcha-response',
23
        'reCaptcha' => 'g-recaptcha-response',
24
    ];
25
    private const PUBLIC_KEY_FIELD = [
26
        'hCaptcha' => 'publicKey',
27
        'reCaptcha' => 'siteKey',
28
    ];
29
    private const PRIVATE_KEY_FIELD = [
30
        'hCaptcha' => 'privateKey',
31
        'reCaptcha' => 'secretKey',
32
    ];
33
34
    protected bool $enabled = false;
35
    protected string $type = '';
36
    protected string $apiScript = '';
37
    protected string $verificationServer = '';
38
    protected string $publicKey = '';
39
    protected string $privateKey = '';
40
    protected string $responseField = '';
41
42
    public function __construct(array $captchaSettings = [])
43
    {
44
        $this->enabled = (bool)($captchaSettings['enabled'] ?? false);
45
        $this->type = $captchaSettings['type'] ?? '';
46
        $this->apiScript = $captchaSettings[$this->type]['apiScript'] ?? '';
47
        $this->verificationServer = $captchaSettings[$this->type]['verificationServer'] ?? '';
48
        $this->publicKey = $captchaSettings[$this->type][self::PUBLIC_KEY_FIELD[$this->type]] ?? '';
49
        $this->privateKey = $captchaSettings[$this->type][self::PRIVATE_KEY_FIELD[$this->type]] ?? '';
50
        $this->responseField = self::RESPONSE_FIELD[$this->type] ?? '';
51
52
        $this->validateSettings();
53
    }
54
55
    public function getEnabled(): bool
56
    {
57
        return $this->enabled;
58
    }
59
60
    public function getType(): string
61
    {
62
        return $this->type;
63
    }
64
65
    public function getApiScript(): string
66
    {
67
        return $this->apiScript;
68
    }
69
70
    public function getVerificationServer(): string
71
    {
72
        return $this->verificationServer;
73
    }
74
75
    public function getPublicKey(): string
76
    {
77
        return $this->publicKey;
78
    }
79
80
    public function getPrivateKey(): string
81
    {
82
        return $this->privateKey;
83
    }
84
85
    public function getResponseField(): string
86
    {
87
        return $this->responseField;
88
    }
89
90
    /**
91
     * Checks, if all properties contain valid values
92
     */
93
    private function validateSettings(): void
94
    {
95
        // If captcha is not enabled, there is no need to verify the settings
96
        if (!$this->enabled) {
97
            return;
98
        }
99
100
        if ($this->type === '' || !in_array($this->type, ['hCaptcha', 'reCaptcha'])) {
101
            throw new InvalidCaptchaConfigurationException(
102
                'Invalid captcha type settings. Valid values are "hCaptcha" and "reCaptcha',
103
                1631962901
104
            );
105
        }
106
        if ($this->apiScript === '' || !filter_var($this->apiScript, FILTER_VALIDATE_URL)) {
107
            throw new InvalidCaptchaConfigurationException(
108
                'Invalid apiScript setting.',
109
                1631962907
110
            );
111
        }
112
        if ($this->verificationServer === '' || !filter_var($this->verificationServer, FILTER_VALIDATE_URL)) {
113
            throw new InvalidCaptchaConfigurationException(
114
                'Invalid verificationServer setting.',
115
                1631962990
116
            );
117
        }
118
        if ($this->publicKey === '') {
119
            throw new InvalidCaptchaConfigurationException(
120
                'Invalid publicKey/siteKey setting.',
121
                1631964323
122
            );
123
        }
124
        if ($this->privateKey === '') {
125
            throw new InvalidCaptchaConfigurationException(
126
                'Invalid privateKey/secretKey setting.',
127
                1631964328
128
            );
129
        }
130
    }
131
}
132