Passed
Push — master ( 38dded...bb2043 )
by Craig
01:04
created

SettingsModuleInstaller::install()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 52
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 52
rs 9.4929
cc 2
eloc 36
nc 2
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the Zikula package.
5
 *
6
 * Copyright Zikula Foundation - http://zikula.org/
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zikula\SettingsModule;
13
14
use Zikula\Bundle\CoreBundle\HttpKernel\ZikulaKernel;
15
use Zikula\Core\AbstractExtensionInstaller;
16
use Zikula\ExtensionsModule\Api\VariableApi;
17
18
/**
19
 * Installation and upgrade routines for the settings module.
20
 *
21
 * PLEASE NOTE CAREFULLY.  The use of System::get/set/delVar() is deliberate
22
 * we cannot use $this->get/set/delVar() because the keys will be incorrectly
23
 * generated (System instead of ZConfig).
24
 */
25
class SettingsModuleInstaller extends AbstractExtensionInstaller
26
{
27
    /**
28
     * Initialise the settings module.
29
     *
30
     * @return boolean
31
     */
32
    public function install()
33
    {
34
        // Set up an initial value for a module variable. Note that all module
35
        // variables should be initialised with some value in this way rather
36
        // than just left blank, this helps the user-side code and means that
37
        // there doesn't need to be a check to see if the variable is set in
38
        // the rest of the code as it always will be.
39
        $this->setSystemVar('debug', '0');
40
        $this->setSystemVar('startdate', date('m/Y', time()));
41
        $this->setSystemVar('adminmail', '[email protected]');
42
        $this->setSystemVar('Default_Theme', 'ZikulaBootstrapTheme');
43
        $this->setSystemVar('timezone', date_default_timezone_get());
44
        $this->setSystemVar('funtext', '1');
45
        $this->setSystemVar('reportlevel', '0');
46
        $this->setSystemVar('startpage', '');
47
        $this->setSystemVar('Version_Num', ZikulaKernel::VERSION);
48
        $this->setSystemVar('Version_Sub', ZikulaKernel::VERSION_SUB);
49
        $this->setSystemVar('debug_sql', '0');
50
        $this->setSystemVar('multilingual', '1');
51
        $this->setSystemVar('useflags', '0');
52
        $this->setSystemVar('theme_change', '0');
53
        $this->setSystemVar('UseCompression', '0');
54
        $this->setSystemVar('siteoff', 0);
55
        $this->setSystemVar('siteoffreason', '');
56
        $this->setSystemVar('startargs', '');
57
        $this->setSystemVar('language_detect', 0);
58
        // Multilingual support
59
        foreach ($this->container->get('zikula_settings_module.locale_api')->getSupportedLocales() as $lang) {
60
            $this->setSystemVar('sitename_' . $lang, $this->__('Site name'));
61
            $this->setSystemVar('slogan_' . $lang, $this->__('Site description'));
62
            $this->setSystemVar('defaultpagetitle_' . $lang, $this->__('Site name'));
63
            $this->setSystemVar('defaultmetadescription_' . $lang, $this->__('Site description'));
64
        }
65
66
        $this->setSystemVar(SettingsConstant::SYSTEM_VAR_PROFILE_MODULE, '');
67
        $this->setSystemVar(SettingsConstant::SYSTEM_VAR_MESSAGE_MODULE, '');
68
        $this->setSystemVar('languageurl', 0);
69
        $this->setSystemVar('ajaxtimeout', 5000);
70
        //! this is a comma-separated list of special characters to search for in permalinks
71
        $this->setSystemVar('permasearch', $this->__('À,Á,Â,Ã,Å,à,á,â,ã,å,Ò,Ó,Ô,Õ,Ø,ò,ó,ô,õ,ø,È,É,Ê,Ë,è,é,ê,ë,Ç,ç,Ì,Í,Î,Ï,ì,í,î,ï,Ù,Ú,Û,ù,ú,û,ÿ,Ñ,ñ,ß,ä,Ä,ö,Ö,ü,Ü'));
72
        //! this is a comma-separated list of special characters to replace in permalinks
73
        $this->setSystemVar('permareplace', $this->__('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'));
74
75
        $locale = $this->container->getParameter('locale');
76
        $this->setSystemVar('locale', $locale);
77
        $this->setSystemVar('language_i18n', $locale);
78
79
        $this->setSystemVar('idnnames', 1);
80
81
        // Initialisation successful
82
        return true;
83
    }
84
85
    /**
86
     * upgrade the module from an old version
87
     *
88
     * @param  string $oldversion version number string to upgrade from
89
     *
90
     * @return bool|string true on success, last valid version string or false if fails
91
     */
92
    public function upgrade($oldversion)
93
    {
94
        $request = $this->container->get('request_stack')->getMasterRequest();
95
        // Upgrade dependent on old version number
96
        switch ($oldversion) {
97
            case '2.9.7':
98
            case '2.9.8':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
99
                $permasearch = $this->getSystemVar('permasearch');
100
                if (empty($permasearch)) {
101
                    $this->setSystemVar('permasearch', $this->__('À,Á,Â,Ã,Å,à,á,â,ã,å,Ò,Ó,Ô,Õ,Ø,ò,ó,ô,õ,ø,È,É,Ê,Ë,è,é,ê,ë,Ç,ç,Ì,Í,Î,Ï,ì,í,î,ï,Ù,Ú,Û,ù,ú,û,ÿ,Ñ,ñ,ß,ä,Ä,ö,Ö,ü,Ü'));
102
                }
103
                $permareplace = $this->getSystemVar('permareplace');
104
                if (empty($permareplace)) {
105
                    $this->setSystemVar('permareplace', $this->__('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'));
106
                }
107
                $locale = $this->getSystemVar('locale');
108
                if (empty($locale)) {
109
                    $this->setSystemVar('locale', $request->getLocale());
110
                }
111
112
            case '2.9.9':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
113
                // update certain System vars to multilingual. provide default values for all locales using current value.
114
                // must directly manipulate System vars at DB level because using $this->getSystemVar() returns empty values
115
                $varsToChange = ['sitename', 'slogan', 'defaultpagetitle', 'defaultmetadescription'];
116
                $SystemVars = $this->entityManager->getRepository('Zikula\ExtensionsModule\Entity\ExtensionVarEntity')->findBy(['modname' => VariableApi::CONFIG]);
117
                /** @var \Zikula\ExtensionsModule\Entity\ExtensionVarEntity $modVar */
118
                foreach ($SystemVars as $modVar) {
119
                    if (in_array($modVar->getName(), $varsToChange)) {
120
                        foreach ($this->container->get('zikula_settings_module.locale_api')->getSupportedLocales() as $langcode) {
121
                            $newModVar = clone $modVar;
122
                            $newModVar->setName($modVar->getName() . '_' . $langcode);
123
                            $this->entityManager->persist($newModVar);
124
                        }
125
                        $this->entityManager->remove($modVar);
126
                    }
127
                }
128
                $this->entityManager->flush();
129
130
            case '2.9.10':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
131
                $this->setSystemVar('startController', '');
132
                $newStargArgs = str_replace(',', '&', $this->getSystemVar('startargs')); // replace comma with `&`
133
                $this->setSystemVar('startargs', $newStargArgs);
134
            case '2.9.11':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
135
                $this->setSystemVar('useCompression', (bool)$this->getSystemVar('useCompression'));
136
            case '2.9.12': // ship with Core-1.4.4
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
137
                // reconfigure TZ settings
138
                $this->setGuestTimeZone();
139
            case '2.9.13':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
140
                $this->container->get('zikula_extensions_module.api.variable')->del(VariableApi::CONFIG, 'entrypoint');
141
                $this->container->get('zikula_extensions_module.api.variable')->del(VariableApi::CONFIG, 'shorturlsstripentrypoint');
142
                $this->container->get('zikula_extensions_module.api.variable')->del(VariableApi::CONFIG, 'shorturls');
143
                $this->container->get('zikula_extensions_module.api.variable')->del(VariableApi::CONFIG, 'shorturlsdefaultmodule');
144
            case '2.9.14': // ship with Core-1.5.0
145
                // current version
146
        }
147
148
        // Update successful
149
        return true;
150
    }
151
152
    /**
153
     * Delete the settings module.
154
     *
155
     * @return boolean false
156
     */
157
    public function uninstall()
158
    {
159
        // This module cannot be uninstalled.
160
        return false;
161
    }
162
163
    private function setSystemVar($name, $value = '')
164
    {
165
        return $this->container->get('zikula_extensions_module.api.variable')->set(VariableApi::CONFIG, $name, $value);
166
    }
167
168
    /**
169
     * @param string $name
170
     */
171
    private function getSystemVar($name)
172
    {
173
        return $this->container->get('zikula_extensions_module.api.variable')->getSystemVar($name);
174
    }
175
176
    /**
177
     * upgrade helper method
178
     */
179
    private function setGuestTimeZone()
180
    {
181
        $existingOffset = $this->getSystemVar('timezone_offset');
182
        $actualOffset = floatval($existingOffset) * 60; // express in minutes
183
        $timezoneAbbreviations = \DateTimeZone::listAbbreviations();
184
        $timeZone = date_default_timezone_get();
185
        foreach ($timezoneAbbreviations as $abbreviation => $zones) {
186
            foreach ($zones as $zone) {
187
                if ($zone['offset'] == $actualOffset) {
188
                    $timeZone = $zone['timezone_id'];
189
                    break 2;
190
                }
191
            }
192
        }
193
        $this->setSystemVar('timezone', $timeZone);
194
    }
195
}
196