Completed
Push — master ( 12a783...69e9ac )
by Axel
13:50 queued 08:29
created

SettingsModuleInstaller::__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\SettingsModule;
15
16
use DateTimeZone;
17
use Doctrine\Persistence\ManagerRegistry;
18
use Symfony\Component\HttpFoundation\RequestStack;
19
use Symfony\Contracts\Translation\TranslatorInterface;
20
use Zikula\Bundle\CoreBundle\Doctrine\Helper\SchemaHelper;
21
use Zikula\Bundle\CoreBundle\HttpKernel\ZikulaKernel;
22
use Zikula\ExtensionsModule\AbstractExtension;
23
use Zikula\ExtensionsModule\Api\ApiInterface\VariableApiInterface;
24
use Zikula\ExtensionsModule\Api\VariableApi;
25
use Zikula\ExtensionsModule\Entity\ExtensionVarEntity;
26
use Zikula\ExtensionsModule\Installer\AbstractExtensionInstaller;
27
use Zikula\SettingsModule\Api\ApiInterface\LocaleApiInterface;
28
29
/**
30
 * Installation and upgrade routines for the Settings module.
31
 */
32
class SettingsModuleInstaller extends AbstractExtensionInstaller
33
{
34
    /**
35
     * @var LocaleApiInterface
36
     */
37
    private $localeApi;
38
39
    /**
40
     * @var string
41
     */
42
    private $locale;
43
44
    public function __construct(
45
        LocaleApiInterface $localeApi,
46
        $locale,
47
        AbstractExtension $extension,
48
        ManagerRegistry $managerRegistry,
49
        SchemaHelper $schemaTool,
50
        RequestStack $requestStack,
51
        TranslatorInterface $translator,
52
        VariableApiInterface $variableApi
53
    ) {
54
        $this->localeApi = $localeApi;
55
        $this->locale = $locale;
56
        parent::__construct($extension, $managerRegistry, $schemaTool, $requestStack, $translator, $variableApi);
57
    }
58
59
    public function install(): bool
60
    {
61
        $this->setSystemVar('startdate', date('m/Y'));
62
        $this->setSystemVar('adminmail', '[email protected]');
63
        $this->setSystemVar('Default_Theme', 'ZikulaBootstrapTheme');
64
        $this->setSystemVar('timezone', date_default_timezone_get());
65
        $this->setSystemVar('Version_Num', ZikulaKernel::VERSION);
66
        $this->setSystemVar('multilingual', '1');
67
        $this->setSystemVar('theme_change', '0');
68
        $this->setSystemVar('UseCompression', '0');
69
        $this->setSystemVar('siteoff', 0);
70
        $this->setSystemVar('siteoffreason');
71
        $this->setSystemVar('language_detect', 0);
72
73
        // Multilingual support
74
        foreach ($this->localeApi->getSupportedLocales() as $lang) {
75
            $this->setSystemVar('sitename_' . $lang, $this->trans('Site name'));
76
            $this->setSystemVar('slogan_' . $lang, $this->trans('Site description'));
77
            $this->setSystemVar('defaultpagetitle_' . $lang, $this->trans('Site name'));
78
            $this->setSystemVar('defaultmetadescription_' . $lang, $this->trans('Site description'));
79
            $this->setSystemVar('startController_' . $lang, $this->getDefaultValue('startController'));
80
        }
81
82
        $this->setSystemVar(SettingsConstant::SYSTEM_VAR_PROFILE_MODULE);
83
        $this->setSystemVar(SettingsConstant::SYSTEM_VAR_MESSAGE_MODULE);
84
        $this->setSystemVar('languageurl', 0);
85
        $this->setSystemVar('ajaxtimeout', 5000);
86
        //! this is a comma-separated list of special characters to search for in permalinks
87
        $this->setSystemVar('permasearch', $this->getDefaultValue('permasearch'));
88
        //! this is a comma-separated list of special characters to replace in permalinks
89
        $this->setSystemVar('permareplace', $this->getDefaultValue('permareplace'));
90
91
        $this->setSystemVar('locale', $this->locale);
92
93
        // Initialisation successful
94
        return true;
95
    }
96
97
    public function upgrade(string $oldVersion): bool
98
    {
99
        $request = $this->requestStack->getMasterRequest();
100
        // Upgrade dependent on old version number
101
        switch ($oldVersion) {
102
            case '2.9.7':
103
            case '2.9.8':
104
                $permasearch = $this->getSystemVar('permasearch');
105
                if (empty($permasearch)) {
106
                    $this->setSystemVar('permasearch', $this->getDefaultValue('permasearch'));
107
                }
108
                $permareplace = $this->getSystemVar('permareplace');
109
                if (empty($permareplace)) {
110
                    $this->setSystemVar('permareplace', $this->getDefaultValue('permareplace'));
111
                }
112
                $locale = $this->getSystemVar('locale');
113
                if (empty($locale)) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
114
                    $this->setSystemVar('locale', $request->getLocale());
115
                }
116
117
            case '2.9.9':
118
                // update certain System vars to multilingual. provide default values for all locales using current value.
119
                // must directly manipulate System vars at DB level because using $this->getSystemVar() returns empty values
120
                $varsToChange = [
121
                    'sitename',
122
                    'slogan',
123
                    'defaultpagetitle',
124
                    'defaultmetadescription'
125
                ];
126
                $systemVars = $this->managerRegistry->getRepository(ExtensionVarEntity::class)->findBy(['modname' => VariableApi::CONFIG]);
127
                /** @var ExtensionVarEntity $modVar */
128
                foreach ($systemVars as $modVar) {
129
                    if (in_array($modVar->getName(), $varsToChange, true)) {
130
                        foreach ($this->localeApi->getSupportedLocales() as $langcode) {
131
                            $newModVar = clone $modVar;
132
                            $newModVar->setName($modVar->getName() . '_' . $langcode);
133
                            $this->entityManager->persist($newModVar);
134
                        }
135
                        $this->entityManager->remove($modVar);
136
                    }
137
                }
138
                $this->entityManager->flush();
139
            case '2.9.10':
140
            case '2.9.11':
141
                $this->setSystemVar('UseCompression', (bool)$this->getSystemVar('UseCompression'));
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
142
            case '2.9.12': // ship with Core-1.4.4
143
                // reconfigure TZ settings
144
                $this->setGuestTimeZone();
145
            case '2.9.13':
146
                $this->getVariableApi()->del(VariableApi::CONFIG, 'entrypoint');
147
                $this->getVariableApi()->del(VariableApi::CONFIG, 'shorturlsstripentrypoint');
148
                $this->getVariableApi()->del(VariableApi::CONFIG, 'shorturls');
149
                $this->getVariableApi()->del(VariableApi::CONFIG, 'shorturlsdefaultmodule');
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
150
            case '2.9.14': // ship with Core-1.5.0 + Core-2.x
151
                $this->getVariableApi()->del(VariableApi::CONFIG, 'Version_Sub');
152
                $this->setSystemVar('startController'); // reset to blank because of new format FQCN::method
153
            case '2.9.15':
154
                $varsToRemove = [
155
                    'funtext',
156
                    'reportlevel',
157
                    'idnnames',
158
                    'debug',
159
                    'debug_sql',
160
                    'useflags',
161
                    'language_i18n',
162
                    'startController',
163
                    'startargs'
164
                ];
165
                foreach ($varsToRemove as $varName) {
166
                    $this->getVariableApi()->del(VariableApi::CONFIG, $varName);
167
                }
168
                foreach ($this->localeApi->getSupportedLocales() as $lang) {
169
                    $this->setSystemVar('startController_' . $lang, $this->getDefaultValue('startController'));
170
                }
171
            case '2.9.16': // ship with Core-3.0.0
172
                // current version
173
        }
174
175
        // Update successful
176
        return true;
177
    }
178
179
    public function uninstall(): bool
180
    {
181
        // This module cannot be uninstalled.
182
        return false;
183
    }
184
185
    /**
186
     * @return string|array|null
187
     */
188
    private function getDefaultValue(string $name)
189
    {
190
        if ('permasearch' === $name) {
191
            return $this->trans('À,Á,Â,Ã,Å,à,á,â,ã,å,Ò,Ó,Ô,Õ,Ø,ò,ó,ô,õ,ø,È,É,Ê,Ë,è,é,ê,ë,Ç,ç,Ì,Í,Î,Ï,ì,í,î,ï,Ù,Ú,Û,ù,ú,û,ÿ,Ñ,ñ,ß,ä,Ä,ö,Ö,ü,Ü');
192
        }
193
        if ('permareplace' === $name) {
194
            return $this->trans('A,A,A,A,A,a,a,a,a,a,O,O,O,O,O,o,o,o,o,o,E,E,E,E,e,e,e,e,C,c,I,I,I,I,i,i,i,i,U,U,U,u,u,u,y,N,n,ss,ae,Ae,oe,Oe,ue,Ue');
195
        }
196
        if ('startController' === $name) {
197
            return [
198
                'controller' => '',
199
                'query' => '',
200
                'request' => '',
201
                'attributes' => ''
202
            ];
203
        }
204
205
        return null;
206
    }
207
208
    private function setSystemVar(string $name, $value = ''): void
209
    {
210
        $this->getVariableApi()->set(VariableApi::CONFIG, $name, $value);
211
    }
212
213
    private function getSystemVar(string $name)
214
    {
215
        return $this->getVariableApi()->getSystemVar($name);
216
    }
217
218
    private function setGuestTimeZone(): void
219
    {
220
        $existingOffset = $this->getSystemVar('timezone_offset');
221
        $actualOffset = (float) $existingOffset * 60; // express in minutes
222
        $timezoneAbbreviations = DateTimeZone::listAbbreviations();
223
        $timeZone = date_default_timezone_get();
224
        foreach ($timezoneAbbreviations as $abbreviation => $zones) {
225
            foreach ($zones as $zone) {
226
                if ($zone['offset'] === $actualOffset) {
227
                    $timeZone = $zone['timezone_id'];
228
                    break 2;
229
                }
230
            }
231
        }
232
        $this->setSystemVar('timezone', $timeZone);
233
    }
234
}
235