Passed
Push — master ( f6be53...47fb7a )
by Rias
03:42
created

ContactFormExtensionsService::getRecaptcha()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Craft Contact Form Extensions plugin for Craft CMS 3.x.
4
 *
5
 * Adds extensions to the Craft CMS contact form plugin.
6
 *
7
 * @link      https://rias.be
8
 *
9
 * @copyright Copyright (c) 2018 Rias
10
 */
11
12
namespace rias\contactformextensions\services;
13
14
use Craft;
15
use craft\base\Component;
16
use craft\contactform\models\Submission;
17
use rias\contactformextensions\ContactFormExtensions;
18
use rias\contactformextensions\elements\ContactFormSubmission;
19
use yii\base\Exception;
20
21
/**
22
 * CraftContactFormExtensionsService Service.
23
 *
24
 * All of your plugin’s business logic should go in services, including saving data,
25
 * retrieving data, etc. They provide APIs that your controllers, template variables,
26
 * and other plugins can interact with.
27
 *
28
 * https://craftcms.com/docs/plugins/services
29
 *
30
 * @author    Rias
31
 *
32
 * @since     1.0.0
33
 */
34
class ContactFormExtensionsService extends Component
35
{
36
    // Public Methods
37
    // =========================================================================
38
39
    /**
40
     * This function can literally be anything you want, and you can have as many service
41
     * functions as you want.
42
     *
43
     * From any other plugin file, call it like this:
44
     *
45
     *     CraftContactFormExtensions::$plugin->craftContactFormExtensionsService->exampleService()
46
     *
47
     * @param Submission $submission
48
     *
49
     * @throws \Throwable
50
     * @throws \craft\errors\ElementNotFoundException
51
     * @throws \yii\base\Exception
52
     * @throws \yii\base\ExitException
53
     *
54
     * @return mixed
55
     */
56
    public function saveSubmission(Submission $submission)
57
    {
58
        $contactFormSubmission = new ContactFormSubmission();
59
        $contactFormSubmission->form = $submission->message['formName'] ?? 'contact';
60
        $contactFormSubmission->fromName = $submission->fromName;
61
        $contactFormSubmission->fromEmail = $submission->fromEmail;
62
        $contactFormSubmission->subject = $submission->subject;
63
        $contactFormSubmission->message = json_encode($submission->message);
64
65
        if (Craft::$app->elements->saveElement($contactFormSubmission)) {
66
            return $contactFormSubmission;
67
        }
68
69
        throw new Exception(json_encode($contactFormSubmission->errors));
70
    }
71
72
    public function getRecaptcha()
73
    {
74
        $siteKey = ContactFormExtensions::$plugin->settings->recaptchaSiteKey;
75
        $secretKey = ContactFormExtensions::$plugin->settings->recaptchaSecretKey;
76
        $options = [
77
            'hideBadge' => ContactFormExtensions::$plugin->settings->recaptchaHideBadge,
78
            'dataBadge' => ContactFormExtensions::$plugin->settings->recaptchaDataBadge,
79
            'timeout' => ContactFormExtensions::$plugin->settings->recaptchaTimeout,
80
            'debug' => ContactFormExtensions::$plugin->settings->recaptchaDebug,
81
        ];
82
83
        return new \AlbertCht\InvisibleReCaptcha\InvisibleReCaptcha($siteKey, $secretKey, $options);
84
    }
85
}
86