Completed
Pull Request — master (#4191)
by Craig
05:42
created

ConfigController::testAction()   B

Complexity

Conditions 8
Paths 43

Size

Total Lines 37
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 24
c 0
b 0
f 0
nc 43
nop 3
dl 0
loc 37
rs 8.4444
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula Foundation - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\MailerModule\Controller;
15
16
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
17
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
20
use Symfony\Component\Mailer\MailerInterface;
21
use Symfony\Component\Mime\Address;
22
use Symfony\Component\Mime\Email;
23
use Symfony\Component\Routing\Annotation\Route;
24
use Zikula\Bundle\CoreBundle\Controller\AbstractController;
25
use Zikula\Bundle\CoreBundle\Helper\LocalDotEnvHelper;
26
use Zikula\Bundle\CoreBundle\HttpKernel\ZikulaHttpKernelInterface;
27
use Zikula\ExtensionsModule\Api\ApiInterface\VariableApiInterface;
28
use Zikula\MailerModule\Form\Type\MailTransportConfigType;
29
use Zikula\MailerModule\Form\Type\TestType;
30
use Zikula\MailerModule\Helper\MailTransportHelper;
31
use Zikula\PermissionsModule\Annotation\PermissionCheck;
32
use Zikula\ThemeModule\Engine\Annotation\Theme;
33
34
/**
35
 * Class ConfigController
36
 *
37
 * @Route("/config")
38
 * @PermissionCheck("admin")
39
 */
40
class ConfigController extends AbstractController
41
{
42
    /**
43
     * @Route("/config")
44
     * @Theme("admin")
45
     * @Template("@ZikulaMailerModule/Config/config.html.twig")
46
     */
47
    public function configAction(
48
        Request $request,
49
        MailTransportHelper $mailTransportHelper
50
    ): array {
51
        $form = $this->createForm(
52
            MailTransportConfigType::class,
53
            $this->getVars()
54
        );
55
        $form->handleRequest($request);
56
        if ($form->isSubmitted() && $form->isValid()) {
57
            if ($form->get('save')->isClicked()) {
0 ignored issues
show
Bug introduced by
The method isClicked() does not exist on Symfony\Component\Form\FormInterface. It seems like you code against a sub-type of Symfony\Component\Form\FormInterface such as Symfony\Component\Form\SubmitButton. ( Ignorable by Annotation )

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

57
            if ($form->get('save')->/** @scrutinizer ignore-call */ isClicked()) {
Loading history...
58
                $formData = $form->getData();
59
                $this->setVars($formData);
60
                if (true === $mailTransportHelper->handleFormData($formData)) {
61
                    $this->addFlash('status', 'Done! Configuration updated.');
62
                } else {
63
                    $this->addFlash('error', $this->trans('Cannot write to %file%.', ['%file%' => '\.env.local']));
64
                }
65
            } elseif ($form->get('cancel')->isClicked()) {
66
                $this->addFlash('status', 'Operation cancelled.');
67
            }
68
        }
69
70
        return [
71
            'form' => $form->createView()
72
        ];
73
    }
74
75
    /**
76
     * @Route("/test")
77
     * @Theme("admin")
78
     * @Template("@ZikulaMailerModule/Config/test.html.twig")
79
     *
80
     * This function displays a form to send a test mail.
81
     */
82
    public function testAction(
83
        Request $request,
84
        VariableApiInterface $variableApi,
85
        MailerInterface $mailer
86
    ): array {
87
        $form = $this->createForm(TestType::class, $this->getDataValues($variableApi));
88
        $form->handleRequest($request);
89
        if ($form->isSubmitted() && $form->isValid()) {
90
            if ($form->get('test')->isClicked()) {
91
                $formData = $form->getData();
92
                $html = in_array($formData['messageType'], ['html', 'multipart']) ? true : false;
93
                try {
94
                    $siteName = $variableApi->getSystemVar('sitename', $variableApi->getSystemVar('sitename_en'));
95
                    $adminMail = $variableApi->getSystemVar('adminmail');
96
97
                    $email = (new Email())
98
                        ->from(new Address($adminMail, $siteName))
99
                        ->to(new Address($formData['toAddress'], $formData['toName']))
100
                        ->subject($formData['subject'])
101
                        ->text($formData['bodyText'])
102
                    ;
103
                    if ($html) {
104
                        $email->html($formData['bodyHtml']);
105
                    }
106
                    $mailer->send($email);
107
                    $this->addFlash('status', 'Done! Message sent.');
108
                } catch (TransportExceptionInterface $exception) {
109
                    $this->addFlash('error', $exception->getMessage());
110
                }
111
            }
112
            if ($form->get('cancel')->isClicked()) {
113
                $this->addFlash('status', 'Operation cancelled.');
114
            }
115
        }
116
117
        return [
118
            'form' => $form->createView(),
119
        ];
120
    }
121
122
    /**
123
     * Returns required data from module variables and mailer configuration.
124
     */
125
    private function getDataValues(
126
        VariableApiInterface $variableApi
127
    ): array {
128
        $modVars = $variableApi->getAll('ZikulaMailerModule');
129
130
        $modVars['sitename'] = $variableApi->getSystemVar('sitename', $variableApi->getSystemVar('sitename_en'));
131
        $modVars['adminmail'] = $variableApi->getSystemVar('adminmail');
132
133
        $modVars['fromName'] = $modVars['sitename'];
134
        $modVars['fromAddress'] = $modVars['adminmail'];
135
136
        return $modVars;
137
    }
138
}
139