Passed
Push — master ( b01395...c75806 )
by Roberto
05:23 queued 13s
created

ReCaptchaBuilderV2::getTagAttributes()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.128

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 4
nop 0
dl 0
loc 22
rs 9.9332
c 0
b 0
f 0
ccs 8
cts 10
cp 0.8
crap 4.128
1
<?php
2
/**
3
 * Copyright (c) 2017 - present
4
 * LaravelGoogleRecaptcha - ReCaptchaBuilderV2.php
5
 * author: Roberto Belotti - [email protected]
6
 * web : robertobelotti.com, github.com/biscolab
7
 * Initial version created on: 12/9/2018
8
 * MIT license: https://github.com/biscolab/laravel-recaptcha/blob/master/LICENSE
9
 */
10
11
namespace Biscolab\ReCaptcha;
12
13
use Biscolab\ReCaptcha\Exceptions\InvalidConfigurationException;
14
use Illuminate\Support\Arr;
15
16
/**
17
 * Class ReCaptchaBuilderV2
18
 * @package Biscolab\ReCaptcha
19
 */
20
class ReCaptchaBuilderV2 extends ReCaptchaBuilder
21
{
22
23
    /**
24
     * ReCaptchaBuilderV2 constructor.
25
     *
26
     * @param string $api_site_key
27
     * @param string $api_secret_key
28
     */
29 28
    public function __construct(string $api_site_key, string $api_secret_key)
30
    {
31
32 28
        parent::__construct($api_site_key, $api_secret_key, 'v2');
33 28
    }
34
35
    /**
36
     * Write ReCAPTCHA HTML tag in your FORM
37
     * Insert before </form> tag
38
     * @return string
39
     */
40 3
    public function htmlFormSnippet(): string
41
    {
42
43 3
        $data_attributes = [];
44 3
        $config_data_attributes = $this->getTagAttributes();
45
46 3
        foreach ($config_data_attributes as $k => $v) {
47 3
            if ($v) {
48 3
                $data_attributes[] = 'data-' . $k . '="' . $v . '"';
49
            }
50
        }
51
52 3
        $html = '<div class="g-recaptcha" ' . implode(" ", $data_attributes) . ' id="recaptcha-element"></div>';
53
54 3
        return $html;
55
    }
56
57
    /**
58
     * @return array
59
     * @throws InvalidConfigurationException
60
     */
61 8
    public function getTagAttributes(): array
62
    {
63
64
        $tag_attributes = [
65 8
            'sitekey' => $this->api_site_key
66
        ];
67
68 8
        $tag_attributes = array_merge($tag_attributes, config('recaptcha.tag_attributes', []));
69
70 8
        if (Arr::get($tag_attributes, 'callback') === ReCaptchaBuilder::DEFAULT_ONLOAD_JS_FUNCTION) {
71 1
            throw new InvalidConfigurationException('Property "callback" ("data-callback") must be different from "' . ReCaptchaBuilder::DEFAULT_ONLOAD_JS_FUNCTION . '"');
72
        }
73
74 7
        if (Arr::get($tag_attributes, 'expired-callback') === ReCaptchaBuilder::DEFAULT_ONLOAD_JS_FUNCTION) {
75
            throw new InvalidConfigurationException('Property "expired-callback" ("data-expired-callback") must be different from "' . ReCaptchaBuilder::DEFAULT_ONLOAD_JS_FUNCTION . '"');
76
        }
77
78 7
        if (Arr::get($tag_attributes, 'error-callback') === ReCaptchaBuilder::DEFAULT_ONLOAD_JS_FUNCTION) {
79
            throw new InvalidConfigurationException('Property "error-callback" ("data-error-callback") must be different from "' . ReCaptchaBuilder::DEFAULT_ONLOAD_JS_FUNCTION . '"');
80
        }
81
82 7
        return $tag_attributes;
83
    }
84
85
    /**
86
     * @return string
87
     */
88 3
    public function getOnLoadCallback(): string
89
    {
90
91 3
        $attributes = $this->getTagAttributes();
92
93 2
        return "<script>var biscolabOnloadCallback = function() {grecaptcha.render('recaptcha-element', " . json_encode($attributes) . ");};</script>";
94
    }
95
96
}