Completed
Pull Request — master (#4191)
by Craig
05:54 queued 46s
created

ConfigController   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 47
c 0
b 0
f 0
dl 0
loc 97
rs 10
wmc 15

3 Methods

Rating   Name   Duplication   Size   Complexity  
B testAction() 0 37 8
A getDataValues() 0 12 1
A configAction() 0 25 6
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\HttpFoundation\Request;
18
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
19
use Symfony\Component\Mailer\MailerInterface;
20
use Symfony\Component\Mime\Address;
21
use Symfony\Component\Mime\Email;
22
use Symfony\Component\Routing\Annotation\Route;
23
use Zikula\Bundle\CoreBundle\Controller\AbstractController;
24
use Zikula\ExtensionsModule\Api\ApiInterface\VariableApiInterface;
25
use Zikula\MailerModule\Form\Type\MailTransportConfigType;
26
use Zikula\MailerModule\Form\Type\TestType;
27
use Zikula\MailerModule\Helper\MailTransportHelper;
28
use Zikula\PermissionsModule\Annotation\PermissionCheck;
29
use Zikula\ThemeModule\Engine\Annotation\Theme;
30
31
/**
32
 * Class ConfigController
33
 *
34
 * @Route("/config")
35
 * @PermissionCheck("admin")
36
 */
37
class ConfigController extends AbstractController
38
{
39
    /**
40
     * @Route("/config")
41
     * @Theme("admin")
42
     * @Template("@ZikulaMailerModule/Config/config.html.twig")
43
     */
44
    public function configAction(
45
        Request $request,
46
        MailTransportHelper $mailTransportHelper
47
    ): array {
48
        $form = $this->createForm(
49
            MailTransportConfigType::class,
50
            $this->getVars()
51
        );
52
        $form->handleRequest($request);
53
        if ($form->isSubmitted() && $form->isValid()) {
54
            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

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