Completed
Pull Request — master (#4187)
by Craig
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
            case '1.4.0':
88
            case '1.4.1':
89
            case '1.4.2':
90
            case '1.4.3':
91
            case '1.5.0':
92
            case '1.5.1':
93
                // all swiftmailer config changes removed from previous version upgrades above
94
                $this->configDumper->delConfiguration('swiftmailer');
95
                // future upgrade routines
96
        }
97
98
        // Update successful
99
        return true;
100
    }
101
102
    public function uninstall(): bool
103
    {
104
        // Delete any module variables
105
        $this->delVars();
106
107
        // Deletion successful
108
        return true;
109
    }
110
111
    /**
112
     * Default module vars.
113
     */
114
    private function getDefaults(): array
115
    {
116
        return [
117
            'charset' => $this->kernel->getCharset(),
118
            'encoding' => '8bit',
119
            'html' => false,
120
            'wordwrap' => 50,
121
            'enableLogging' => false
122
        ];
123
    }
124
125
    /**
126
     * Set the module var but if it is not set, use the default instead.
127
     *
128
     * @param mixed $value
129
     */
130
    private function setVarWithDefault(string $key, $value = null): void
131
    {
132
        if (isset($value)) {
133
            $this->setVar($key, $value);
134
        }
135
        $defaults = $this->getDefaults();
136
        $this->setVar($key, $defaults[$key]);
137
    }
138
}
139