Completed
Push — master ( e8ce57...2ef872 )
by Craig
10:53 queued 04:32
created

MailerModuleInstaller::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 8
dl 0
loc 13
rs 10
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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;
15
16
use Doctrine\Persistence\ManagerRegistry;
17
use Symfony\Component\HttpFoundation\RequestStack;
18
use Symfony\Contracts\Translation\TranslatorInterface;
19
use Zikula\Bundle\CoreBundle\Doctrine\Helper\SchemaHelper;
20
use Zikula\Bundle\CoreBundle\DynamicConfigDumper;
21
use Zikula\Bundle\CoreBundle\HttpKernel\ZikulaHttpKernelInterface;
22
use Zikula\ExtensionsModule\AbstractExtension;
23
use Zikula\ExtensionsModule\Api\ApiInterface\VariableApiInterface;
24
use Zikula\ExtensionsModule\Entity\ExtensionVarEntity;
25
use Zikula\ExtensionsModule\Installer\AbstractExtensionInstaller;
26
27
/**
28
 * Installation and upgrade routines for the mailer module.
29
 */
30
class MailerModuleInstaller extends AbstractExtensionInstaller
31
{
32
    /**
33
     * @var ZikulaHttpKernelInterface
34
     */
35
    private $kernel;
36
37
    /**
38
     * @var DynamicConfigDumper
39
     */
40
    private $configDumper;
41
42
    public function __construct(
43
        ZikulaHttpKernelInterface $kernel,
44
        DynamicConfigDumper $configDumper,
45
        AbstractExtension $extension,
46
        ManagerRegistry $managerRegistry,
47
        SchemaHelper $schemaTool,
48
        RequestStack $requestStack,
49
        TranslatorInterface $translator,
50
        VariableApiInterface $variableApi
51
    ) {
52
        $this->kernel = $kernel;
53
        $this->configDumper = $configDumper;
54
        parent::__construct($extension, $managerRegistry, $schemaTool, $requestStack, $translator, $variableApi);
55
    }
56
57
    public function install(): bool
58
    {
59
        $this->setVars($this->getDefaults());
60
61
        // Initialisation successful
62
        return true;
63
    }
64
65
    public function upgrade(string $oldVersion): bool
66
    {
67
        // Upgrade dependent on old version number
68
        switch ($oldVersion) {
69
            case '1.3.1':
70
                $this->setVar('smtpsecuremethod', 'ssl');
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
71
            case '1.3.2':
72
                // clear old modvars
73
                // use manual method because getVars() is not available during system upgrade
74
                $modVarEntities = $this->managerRegistry->getRepository(ExtensionVarEntity::class)->findBy(['modname' => $this->name]);
75
                $modVars = [];
76
                foreach ($modVarEntities as $var) {
77
                    $modVars[$var['name']] = $var['value'];
78
                }
79
                $this->delVars();
80
                $this->setVarWithDefault('charset', $modVars['charset']);
81
                $this->setVarWithDefault('encoding', $modVars['encoding']);
82
                $this->setVarWithDefault('html', $modVars['html']);
83
                $this->setVarWithDefault('wordwrap', $modVars['wordwrap']);
84
                // new modvar for 1.4.0
85
                $this->setVarWithDefault('enableLogging', false);
86
87
                // write the config file
88
                $mailerTypeConversion = [
89
                    1 => 'mail',
90
                    2 => 'sendmail',
91
                    3 => 'mail',
92
                    4 => 'smtp',
93
                    5 => 'mail',
94
                ];
95
                $config = [
96
                    'transport' => $mailerTypeConversion[$modVars['mailertype']],
97
                    'username' => $modVars['smtpusername'],
98
                    'password' => $modVars['smtppassword'],
99
                    'host' => $modVars['smtpserver'],
100
                    'port' => $modVars['smtpport'],
101
                    'encryption' => isset($modVars['smtpsecuremethod']) && in_array($modVars['smtpsecuremethod'], ['ssl', 'tls']) ? $modVars['smtpsecuremethod'] : 'ssl',
102
                    'auth_mode' => !empty($modVars['auth']) ? 'login' : null,
103
                    'spool' => ['type' => 'memory'],
104
                    'delivery_addresses' => [],
105
                    'disable_delivery' => 5 === $modVars['mailertype'],
106
                ];
107
                $this->configDumper->setConfiguration('swiftmailer', $config);
108
            case '1.4.0':
109
                $config = $this->configDumper->getConfiguration('swiftmailer');
110
                // remove spool parameter
111
                unset($config['spool']);
112
                $this->configDumper->setConfiguration('swiftmailer', $config);
113
            case '1.4.1':
114
                // install subscriber hooks
115
            case '1.4.2':
116
                $config = $this->configDumper->getConfiguration('swiftmailer');
117
                // delivery_address has changed to an array named delivery_addresses
118
                $config['delivery_addresses'] = !empty($config['delivery_address']) ? [$config['delivery_address']] : [];
119
                unset($config['delivery_address']);
120
            $this->configDumper->setConfiguration('swiftmailer', $config);
121
            case '1.4.3':
122
                // nothing
123
            case '1.5.0':
124
            case '1.5.1':
125
                // future upgrade routines
126
        }
127
128
        // Update successful
129
        return true;
130
    }
131
132
    public function uninstall(): bool
133
    {
134
        // Delete any module variables
135
        $this->delVars();
136
137
        // Deletion successful
138
        return true;
139
    }
140
141
    /**
142
     * Default module vars.
143
     */
144
    private function getDefaults(): array
145
    {
146
        return [
147
            'charset' => $this->kernel->getCharset(),
148
            'encoding' => '8bit',
149
            'html' => false,
150
            'wordwrap' => 50,
151
            'enableLogging' => false
152
        ];
153
    }
154
155
    /**
156
     * Set the module var but if it is not set, use the default instead.
157
     *
158
     * @param mixed $value
159
     */
160
    private function setVarWithDefault(string $key, $value = null): void
161
    {
162
        if (isset($value)) {
163
            $this->setVar($key, $value);
164
        }
165
        $defaults = $this->getDefaults();
166
        $this->setVar($key, $defaults[$key]);
167
    }
168
}
169