• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

contributte / reCAPTCHA / 5447406801

pending completion
5447406801

push

github

f3l1x
Phpstan: fixes

14 of 14 new or added lines in 3 files covered. (100.0%)

115 of 132 relevant lines covered (87.12%)

0.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.1
/src/ReCaptchaProvider.php
1
<?php declare(strict_types = 1);
2

3
namespace Contributte\ReCaptcha;
4

5
use Nette\Forms\Controls\BaseControl;
6
use Nette\SmartObject;
7

8
/**
9
 * @method onValidateControl(ReCaptchaProvider $provider, BaseControl $control)
10
 * @method onValidate(ReCaptchaProvider $provider, mixed $response)
11
 */
12
class ReCaptchaProvider
13
{
14

15
        use SmartObject;
16

17
        // ReCaptcha FTW!
18
        public const FORM_PARAMETER = 'g-recaptcha-response';
19
        public const VERIFICATION_URL = 'https://www.google.com/recaptcha/api/siteverify';
20

21
        /** @var callable[] */
22
        public array $onValidate = [];
23

24
        /** @var callable[] */
25
        public array $onValidateControl = [];
26

27
        private string $siteKey;
28

29
        private string $secretKey;
30

31
        public function __construct(string $siteKey, string $secretKey)
1✔
32
        {
33
                $this->siteKey = $siteKey;
1✔
34
                $this->secretKey = $secretKey;
1✔
35
        }
1✔
36

37
        public function getSiteKey(): string
38
        {
39
                return $this->siteKey;
1✔
40
        }
41

42
        public function validate(string $response): ?ReCaptchaResponse
1✔
43
        {
44
                // Fire events!
45
                $this->onValidate($this, $response);
1✔
46

47
                // Load response
48
                $response = $this->makeRequest($response);
1✔
49

50
                // Response is empty or failed
51
                if ($response === null || $response === '') {
1✔
52
                        return null;
×
53
                }
54

55
                // Decode server answer (with key assoc reserved)
56
                /** @var mixed[] $answer */
57
                $answer = json_decode($response, true);
1✔
58

59
                // Return response
60
                return $answer['success'] === true ? new ReCaptchaResponse(true) : new ReCaptchaResponse(false, $answer['error-codes'] ?? null);
1✔
61
        }
62

63
        public function validateControl(BaseControl $control): bool
1✔
64
        {
65
                // Fire events!
66
                $this->onValidateControl($this, $control);
1✔
67

68
                // Get response
69
                /** @var scalar $value */
70
                $value = $control->getValue();
1✔
71
                $response = $this->validate(strval($value));
1✔
72

73
                if ($response !== null) {
1✔
74
                        return $response->isSuccess();
1✔
75
                }
76

77
                return false;
×
78
        }
79

80
        protected function makeRequest(?string $response, ?string $remoteIp = null): string|null
1✔
81
        {
82
                if ($response === null || $response === '') {
1✔
83
                        return null;
×
84
                }
85

86
                $params = [
1✔
87
                        'secret' => $this->secretKey,
1✔
88
                        'response' => $response,
1✔
89
                ];
90

91
                if ($remoteIp !== null) {
1✔
92
                        $params['remoteip'] = $remoteIp;
×
93
                }
94

95
                $content = file_get_contents($this->buildUrl($params));
1✔
96

97
                return $content === false ? null : $content;
1✔
98
        }
99

100
        /**
101
         * @param mixed[] $parameters
102
         */
103
        protected function buildUrl(array $parameters = []): string
1✔
104
        {
105
                return self::VERIFICATION_URL . '?' . http_build_query($parameters);
1✔
106
        }
107

108
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc