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

MailTransportHelper::handleFormData()   B

Complexity

Conditions 7
Paths 119

Size

Total Lines 37
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
cc 7
eloc 28
c 2
b 0
f 2
nc 119
nop 1
dl 0
loc 37
rs 8.412
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\Helper;
15
16
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
17
use Zikula\Bundle\CoreBundle\Helper\LocalDotEnvHelper;
18
19
class MailTransportHelper
20
{
21
    /**
22
     * @var string
23
     */
24
    private $projectDir;
25
26
    public function __construct(string $projectDir)
27
    {
28
        $this->projectDir = $projectDir;
29
    }
30
31
    public function handleFormData($formData): bool
32
    {
33
        $transportStrings = [
34
            'smtp' => 'smtp://$MAILER_ID:$MAILER_KEY@',
35
            'sendmail' => 'sendmail+smtp://default',
36
            'amazon' => 'ses://$MAILER_ID:$MAILER_KEY@default',
37
            'gmail' => 'gmail://$MAILER_ID:$MAILER_KEY@default',
38
            'mailchimp' => 'mandrill://$MAILER_ID:$MAILER_KEY@default',
39
            'mailgun' => 'mailgun://$MAILER_ID:$MAILER_KEY@default',
40
            'postmark' => 'postmark://$MAILER_ID:$MAILER_KEY@default',
41
            'sendgrid' => 'sendgrid://apikey:$MAILER_KEY@default', // unclear if 'apikey' is supposed to be literal, or replaced
42
            'test' => 'null://null',
43
        ];
44
        try {
45
            $dsn = $transportStrings[$formData['transport']];
46
            if ('smtp' === $formData['transport']) {
47
                $dsn .= $formData['host'] ?? 'localhost';
48
                if (!empty($formData['port'])) {
49
                    $dsn .= ':' . $formData['port'];
50
                }
51
            }
52
            if (!empty($formData['customParameters'])) {
53
                $dsn .= $formData['customParameters'];
54
            }
55
            $vars = ['MAILER_DSN' => '!' . $dsn];
56
            if (!empty($formData['mailer_id'])) {
57
                $vars['MAILER_ID'] = $formData['mailer_id'];
58
            }
59
            if (!empty($formData['mailer_key'])) {
60
                $vars['MAILER_KEY'] = $formData['mailer_key'];
61
            }
62
            $helper = new LocalDotEnvHelper($this->projectDir);
63
            $helper->writeLocalEnvVars($vars);
64
65
            return true;
66
        } catch (IOExceptionInterface $exception) {
67
            return false;
68
        }
69
    }
70
}
71