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

MailTransportHelper   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 30
c 1
b 0
f 1
dl 0
loc 47
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handleFormData() 0 35 5
A __construct() 0 3 1
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 = [
56
                'MAILER_ID' => $formData['mailer_id'],
57
                'MAILER_KEY' => $formData['mailer_key'],
58
                'MAILER_DSN' => '!' . $dsn
59
            ];
60
            $helper = new LocalDotEnvHelper($this->projectDir);
61
            $helper->writeLocalEnvVars($vars);
62
63
            return true;
64
        } catch (IOExceptionInterface $exception) {
65
            return false;
66
        }
67
    }
68
}
69