Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
GoogleRecaptchaAbstractHelper
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
2 / 2
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 renderCaptcha
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3/**
4 * Helper class for the Google Recaptcha captchas.
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public License,
7 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
8 * obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * @package   phpMyFAQ
11 * @author    Thorsten Rinne <thorsten@phpmyfaq.de>
12 * @copyright 2023-2026 phpMyFAQ Team
13 * @license   https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
14 * @link      https://www.phpmyfaq.de
15 * @since     2023-02-07
16 */
17
18declare(strict_types=1);
19
20namespace phpMyFAQ\Captcha\Helper;
21
22use phpMyFAQ\Captcha\CaptchaInterface;
23use phpMyFAQ\Configuration;
24use phpMyFAQ\Helper\AbstractHelper;
25
26class GoogleRecaptchaAbstractHelper extends AbstractHelper implements CaptchaHelperInterface
27{
28    /**
29     * Constructor.
30     */
31    public function __construct(
32        protected Configuration $configuration,
33    ) {
34    }
35
36    public function renderCaptcha(
37        CaptchaInterface $captcha,
38        string $action = '',
39        string $label = '',
40        bool $auth = false,
41    ): string {
42        $html = '';
43
44        if (true === $this->configuration->get(item: 'spam.enableCaptchaCode') && !$auth) {
45            $html .= '<div class="row mb-2">';
46            $html .= sprintf('<label class="col-sm-3 col-form-label">%s</label>', $label);
47            $html .= '    <div class="col-sm-9">';
48            $html .= '        <script src="https://www.google.com/recaptcha/api.js" async defer></script>';
49            $html .= sprintf(
50                '<div class="g-recaptcha" data-sitekey="%s"></div>',
51                (string) $this->configuration->get(item: 'security.googleReCaptchaV2SiteKey'),
52            );
53            $html .= '    </div>';
54            $html .= '</div>';
55        }
56
57        return $html;
58    }
59}