Passed
Push — master ( 7bacbb...65924a )
by Rias
05:00
created

ContactFormExtensionsService   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 82
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getRecaptcha() 0 12 1
A utf8Value() 0 7 2
A saveSubmission() 0 20 3
A utf8AllTheThings() 0 7 2
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 craft\helpers\StringHelper;
18
use rias\contactformextensions\ContactFormExtensions;
19
use rias\contactformextensions\elements\ContactFormSubmission;
20
use yii\base\Exception;
21
22
/**
23
 * CraftContactFormExtensionsService Service.
24
 *
25
 * All of your plugin’s business logic should go in services, including saving data,
26
 * retrieving data, etc. They provide APIs that your controllers, template variables,
27
 * and other plugins can interact with.
28
 *
29
 * https://craftcms.com/docs/plugins/services
30
 *
31
 * @author    Rias
32
 *
33
 * @since     1.0.0
34
 */
35
class ContactFormExtensionsService extends Component
36
{
37
    // Public Methods
38
    // =========================================================================
39
40
    /**
41
     * This function can literally be anything you want, and you can have as many service
42
     * functions as you want.
43
     *
44
     * From any other plugin file, call it like this:
45
     *
46
     *     CraftContactFormExtensions::$plugin->craftContactFormExtensionsService->exampleService()
47
     *
48
     * @param Submission $submission
49
     *
50
     * @throws \Throwable
51
     * @throws \craft\errors\ElementNotFoundException
52
     * @throws \yii\base\Exception
53
     * @throws \yii\base\ExitException
54
     *
55
     * @return mixed
56
     */
57
    public function saveSubmission(Submission $submission)
58
    {
59
        $contactFormSubmission = new ContactFormSubmission();
60
        $contactFormSubmission->form = $submission->message['formName'] ?? 'contact';
61
        $contactFormSubmission->fromName = $submission->fromName;
62
        $contactFormSubmission->fromEmail = $submission->fromEmail;
63
        $contactFormSubmission->subject = $submission->subject;
64
65
        if (!is_array($submission->message)) {
66
            $submission->message = ['message' => $submission->message];
67
        }
68
69
        $message = $this->utf8AllTheThings($submission->message);
70
        $contactFormSubmission->message = json_encode($message);
71
72
        if (Craft::$app->elements->saveElement($contactFormSubmission)) {
73
            return $contactFormSubmission;
74
        }
75
76
        throw new Exception(json_encode($contactFormSubmission->errors));
77
    }
78
79
    public function getRecaptcha()
80
    {
81
        $siteKey = ContactFormExtensions::$plugin->settings->recaptchaSiteKey;
82
        $secretKey = ContactFormExtensions::$plugin->settings->recaptchaSecretKey;
83
        $options = [
84
            'hideBadge' => ContactFormExtensions::$plugin->settings->recaptchaHideBadge,
85
            'dataBadge' => ContactFormExtensions::$plugin->settings->recaptchaDataBadge,
86
            'timeout'   => ContactFormExtensions::$plugin->settings->recaptchaTimeout,
87
            'debug'     => ContactFormExtensions::$plugin->settings->recaptchaDebug,
88
        ];
89
90
        return new \AlbertCht\InvisibleReCaptcha\InvisibleReCaptcha($siteKey, $secretKey, $options);
91
    }
92
93
    /**
94
     * @param array $things
95
     * @return array
96
     */
97
    public function utf8AllTheThings(array $things): array
98
    {
99
        foreach ($things as $key => $value) {
100
            $things[$key] = $this->utf8Value($value);
101
        }
102
103
        return $things;
104
    }
105
106
    /**
107
     * @param array|string $value
108
     * @return array|string
109
     */
110
    public function utf8Value($value)
111
    {
112
        if (is_array($value)) {
113
            return $this->utf8AllTheThings($value);
114
        }
115
116
        return StringHelper::convertToUtf8($value);
117
    }
118
}
119