Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
92.31% |
24 / 26 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
FriendlyCaptcha | |
92.31% |
24 / 26 |
|
50.00% |
2 / 4 |
8.03 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
ensureValid | |
50.00% |
1 / 2 |
|
0.00% |
0 / 1 |
2.50 | |||
getCaptchaDetails | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
verifyCaptcha | |
91.67% |
11 / 12 |
|
0.00% |
0 / 1 |
4.01 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace BO\Zmscitizenapi\Models\Captcha; |
6 | |
7 | use BO\Zmscitizenapi\Models\CaptchaInterface; |
8 | use BO\Zmsentities\Schema\Entity; |
9 | use GuzzleHttp\Exception\RequestException; |
10 | |
11 | class FriendlyCaptcha extends Entity implements CaptchaInterface |
12 | { |
13 | public static $schema = "citizenapi/captcha/friendlyCaptcha.json"; |
14 | /** @var string */ |
15 | public string $service; |
16 | /** @var string */ |
17 | public string $siteKey; |
18 | /** @var string */ |
19 | public string $apiUrl; |
20 | /** @var string */ |
21 | public string $secretKey; |
22 | /** @var string */ |
23 | public string $puzzle; |
24 | /** |
25 | * Constructor. |
26 | */ |
27 | public function __construct() |
28 | { |
29 | $this->service = 'FriendlyCaptcha'; |
30 | $this->siteKey = \App::$FRIENDLY_CAPTCHA_SITE_KEY; |
31 | $this->apiUrl = \App::$FRIENDLY_CAPTCHA_ENDPOINT; |
32 | $this->secretKey = \App::$FRIENDLY_CAPTCHA_SECRET_KEY; |
33 | $this->puzzle = \App::$FRIENDLY_CAPTCHA_ENDPOINT_PUZZLE; |
34 | $this->ensureValid(); |
35 | } |
36 | |
37 | private function ensureValid() |
38 | { |
39 | if (!$this->testValid()) { |
40 | throw new \InvalidArgumentException("The provided data is invalid according to the schema."); |
41 | } |
42 | } |
43 | |
44 | /** |
45 | * Gibt die Captcha-Konfigurationsdetails zurück. |
46 | * |
47 | * @return array |
48 | */ |
49 | public function getCaptchaDetails(): array |
50 | { |
51 | return [ |
52 | 'siteKey' => $this->siteKey, |
53 | 'captchaEndpoint' => $this->apiUrl, |
54 | 'puzzle' => $this->puzzle, |
55 | 'captchaEnabled' => \App::$CAPTCHA_ENABLED |
56 | ]; |
57 | } |
58 | |
59 | /** |
60 | * Überprüft die Captcha-Lösung. |
61 | * |
62 | * @param string $solution |
63 | * @return bool |
64 | * @throws \Exception |
65 | */ |
66 | public function verifyCaptcha(string $solution): bool |
67 | { |
68 | try { |
69 | $response = \App::$http->post($this->apiUrl, [ |
70 | 'form_params' => [ |
71 | 'secret' => $this->secretKey, |
72 | 'solution' => $solution |
73 | ] |
74 | ]); |
75 | $responseBody = json_decode((string)$response->getBody(), true); |
76 | if (json_last_error() !== JSON_ERROR_NONE || !isset($responseBody['success'])) { |
77 | return false; |
78 | } |
79 | |
80 | return $responseBody['success'] === true; |
81 | } catch (RequestException $e) { |
82 | return false; |
83 | } |
84 | } |
85 | } |