MailformController::mailAction()   F
last analyzed

Complexity

Conditions 21
Paths 1940

Size

Total Lines 100
Code Lines 64

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
cc 21
eloc 64
c 5
b 0
f 0
nc 1940
nop 1
dl 0
loc 100
rs 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace  Qbus\Qbtools\ViewHelpers\Widget\Controller;
3
4
use TYPO3\CMS\Core\Utility\GeneralUtility;
5
use TYPO3\CMS\Core\Utility\MailUtility;
6
use TYPO3\CMS\Extbase\Security\Cryptography\HashService;
7
8
/*                                                                        *
9
 * This script is backported from the TYPO3 Flow package "TYPO3.Fluid".   *
10
 *                                                                        *
11
 * It is free software; you can redistribute it and/or modify it under    *
12
 * the terms of the GNU Lesser General Public License, either version 3   *
13
 *  of the License, or (at your option) any later version.                *
14
 *                                                                        *
15
 *                                                                        *
16
 * This script is distributed in the hope that it will be useful, but     *
17
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
18
 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser       *
19
 * General Public License for more details.                               *
20
 *                                                                        *
21
 * You should have received a copy of the GNU Lesser General Public       *
22
 * License along with the script.                                         *
23
 * If not, see http://www.gnu.org/licenses/lgpl.html                      *
24
 *                                                                        *
25
 * The TYPO3 project - inspiring people to share!                         *
26
 *                                                                        */
27
class MailformController extends \TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetController
28
{
29
    /**
30
     * @var HashService
31
     */
32
    protected $hashService;
33
34
    /**
35
     * @param  HashService $hashService
36
     * @return void
37
     */
38
    public function injectHashService(HashService $hashService)
39
    {
40
        $this->hashService = $hashService;
41
    }
42
43
    /**
44
     * @return void
45
     */
46
    public function indexAction()
47
    {
48
        //$GLOBALS['TSFE']->additionalHeaderData[md5('qbtools_jquery')]  = '<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.min.js"></script>';
49
        $this->view->assign('required', $this->widgetConfiguration['required']);
50
51
        //$this->view->assign("qbmailformid", "qbmailform-".$this->controllerContext->getRequest()->getWidgetContext()->getAjaxWidgetIdentifier());
52
        $id = 'qbmailform-' . md5(uniqid(mt_rand(), true));
53
        $this->view->assign('qbmailformid', $id);
54
55
        $this->widgetConfiguration['receiver_overwrite_email'] = $this->getReceiverOverwriteEmail();
56
57
        $this->view->assign('absRefPrefix', $GLOBALS['TSFE']->absRefPrefix);
58
        $this->view->assign('qbmailformConfig', $this->hashService->appendHmac(base64_encode(serialize($this->widgetConfiguration))));
59
        $this->view->setTemplateRootPaths([GeneralUtility::getFileAbsFileName('EXT:qbtools/Resources/Private/Templates/')]);
0 ignored issues
show
Bug introduced by
The method setTemplateRootPaths() does not exist on TYPO3\CMS\Extbase\Mvc\View\ViewInterface. It seems like you code against a sub-type of said class. However, the method does not exist in TYPO3\CMS\Extbase\Mvc\View\AbstractView or TYPO3\CMS\Extbase\Mvc\View\JsonView. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

59
        $this->view->/** @scrutinizer ignore-call */ 
60
                     setTemplateRootPaths([GeneralUtility::getFileAbsFileName('EXT:qbtools/Resources/Private/Templates/')]);
Loading history...
60
    }
61
62
    /**
63
     * @param  array  $msg
64
     * @return string
65
     */
66
    public function mailAction(array $msg = array())
67
    {
68
        $recipient = $this->widgetConfiguration['recipient'];
69
        $sender = $this->widgetConfiguration['sender'];
70
        $required = $this->widgetConfiguration['required'];
71
        $cc = $this->widgetConfiguration['cc'];
72
        $bcc = $this->widgetConfiguration['bcc'];
73
74
        $missing = array();
75
        /* example: $required = [ 'name', 'email,phone' ] => name and (phone or email) are required.*/
76
        foreach ($required as $orFieldList) {
77
            $orFields = explode(',', $orFieldList);
78
            $found = false;
79
            foreach ($orFields as $field) {
80
                if (array_key_exists($field, $msg) && strlen($msg[$field]) != 0) {
81
                    $found = true;
82
                    break;
83
                }
84
            }
85
86
            if (!$found) {
87
                foreach ($orFields as $field) {
88
                    $missing[] = $field;
89
                }
90
            }
91
        }
92
93
        if (count($missing)) {
94
            return json_encode(array(
95
                'status' => 'fields-missing',
96
                'missing' => $missing
97
            ));
98
        }
99
100
        if (!is_array($recipient) || !array_key_exists('email', $recipient)) {
101
            /* TODO: Throw exception instead. */
102
            return json_encode(array(
103
                'status' => 'internal-error',
104
                'error' => '$recipient is not valid'
105
            ));
106
        }
107
108
        if (isset($recipient['name']) && strlen($recipient['name']) > 0) {
109
            $tmp = $recipient;
110
            $recipient = array();
111
            foreach (GeneralUtility::trimExplode(',', $tmp['email']) as $email) {
112
                $recipient[$email] = $tmp['name'];
113
            }
114
        } else {
115
            $recipient = GeneralUtility::trimExplode(',', $recipient['email']);
116
        }
117
118
        $sender = ($sender !== null) ? array($sender['email'] => $sender['name']) : MailUtility::getSystemFrom();
119
120
        $recipientSave = $recipient;
121
        $recipientOverwrite = $this->widgetConfiguration['receiver_overwrite_email'];
122
        if ($recipientOverwrite !== null) {
123
            $recipient = $recipientOverwrite;
124
            $cc = null;
125
            $bcc = null;
126
        }
127
128
        $params = array(
129
            'to' => $recipient,
130
            'from' => $sender,
131
            'msg' => $msg
132
        );
133
134
        $view = GeneralUtility::makeInstance('TYPO3\\CMS\\Fluid\\View\\StandaloneView');
135
        $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName($this->widgetConfiguration['mailTemplate']));
136
        $view->assignMultiple($params);
137
        $text = $view->render();
138
139
        list($subject, $body) = explode("\n", $text, 2);
140
141
        if ($recipientOverwrite !== null) {
142
            $subject .= ' – Recipient: ' . implode(',', $recipientSave);
143
        }
144
145
        $mail = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Mail\\MailMessage');
146
        $mail->setFrom($sender);
147
        $mail->setTo($recipient);
148
        if ($cc) {
149
            $mail->setCc(array($cc['email'] => $cc['name']));
150
        }
151
        if ($bcc) {
152
            $mail->setBcc(array($bcc['email'] => $bcc['name']));
153
        }
154
        $mail->setSubject($subject);
155
        $mail->setBody(trim($body));
156
        if (isset($msg['email']) && strlen($msg['email']) > 0) {
157
            try {
158
                $mail->setReplyTo($msg['email']);
159
            } catch (\Swift_RfcComplianceException $e) {
0 ignored issues
show
Bug introduced by
The type Swift_RfcComplianceException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
160
                // ignore for now
161
            }
162
        }
163
        $mail->send();
164
165
        return json_encode(array('status' => 'ok'));
166
    }
167
168
    /**
169
     * @return string
170
     */
171
    protected function getReceiverOverwriteEmail()
172
    {
173
        if (!isset($GLOBALS['TSFE']->config['config']['tx_qbtools.']['mailform.']['receiver.']['overwrite.']['email'])) {
174
            return null;
175
        }
176
177
        $overwrite = trim($GLOBALS['TSFE']->config['config']['tx_qbtools.']['mailform.']['receiver.']['overwrite.']['email']);
178
        if ($overwrite == '') {
179
            return null;
180
        }
181
182
        return $overwrite;
183
    }
184
}
185