Total Complexity | 46 |
Total Lines | 202 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Install_InitSchema_Model often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Install_InitSchema_Model, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class Install_InitSchema_Model |
||
12 | { |
||
13 | protected $sql_directory = 'install/install_schema/'; |
||
14 | protected $migration_schema = 'install/migrate_schema/'; |
||
15 | |||
16 | /** |
||
17 | * Function starts applying schema changes. |
||
18 | */ |
||
19 | public function initialize() |
||
20 | { |
||
21 | $this->db = \App\Db::getInstance(); |
||
|
|||
22 | $this->initializeDatabase($this->sql_directory, ['scheme', 'data']); |
||
23 | if ($_SESSION['installation_success'] ?? false) { |
||
24 | $this->setLanguage(); |
||
25 | $this->createConfigFiles(); |
||
26 | $this->setDefaultUsersAccess(); |
||
27 | $this->db->createCommand() |
||
28 | ->update('vtiger_currency_info', [ |
||
29 | 'currency_name' => $_SESSION['config_file_info']['currency_name'], |
||
30 | 'currency_code' => $_SESSION['config_file_info']['currency_code'], |
||
31 | 'currency_symbol' => $_SESSION['config_file_info']['currency_symbol'], |
||
32 | ])->execute(); |
||
33 | $this->db->createCommand() |
||
34 | ->update('vtiger_version', [ |
||
35 | 'current_version' => \App\Version::get(), |
||
36 | 'old_version' => \App\Version::get(), |
||
37 | ])->execute(); |
||
38 | // recalculate all sharing rules for users |
||
39 | require_once 'include/utils/CommonUtils.php'; |
||
40 | require_once 'include/fields/DateTimeField.php'; |
||
41 | require_once 'include/fields/DateTimeRange.php'; |
||
42 | require_once 'include/fields/CurrencyField.php'; |
||
43 | require_once 'include/CRMEntity.php'; |
||
44 | require_once 'modules/Vtiger/CRMEntity.php'; |
||
45 | require_once 'include/runtime/Cache.php'; |
||
46 | require_once 'modules/Vtiger/helpers/Util.php'; |
||
47 | require_once 'modules/PickList/DependentPickListUtils.php'; |
||
48 | require_once 'modules/Users/Users.php'; |
||
49 | require_once 'include/Webservices/Utils.php'; |
||
50 | \App\UserPrivilegesFile::recalculateAll(); |
||
51 | \App\Cache::clear(); |
||
52 | \App\Cache::clearOpcache(); |
||
53 | \App\Module::createModuleMetaFile(); |
||
54 | } |
||
55 | } |
||
56 | |||
57 | public function initializeDatabase($location, $filesName = []) |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Function creates default user's Role, Profiles. |
||
101 | */ |
||
102 | public function setDefaultUsersAccess() |
||
128 | } |
||
129 | |||
130 | public function splitQueries($query) |
||
131 | { |
||
132 | $buffer = []; |
||
133 | $queries = []; |
||
134 | $in_string = false; |
||
135 | |||
136 | // Trim any whitespace. |
||
137 | $query = trim($query); |
||
138 | // Remove comment lines. |
||
139 | $query = preg_replace("/\n\\#[^\n]*/", '', "\n" . $query); |
||
140 | // Remove PostgreSQL comment lines. |
||
141 | $query = preg_replace("/\n\\--[^\n]*/", '', "\n" . $query); |
||
142 | // Find function |
||
143 | $funct = explode('CREATE || REPLACE FUNCTION', $query); |
||
144 | // Save sql before function and parse it |
||
145 | $query = $funct[0]; |
||
146 | |||
147 | // Parse the schema file to break up queries. |
||
148 | for ($i = 0; $i < \strlen($query) - 1; ++$i) { |
||
149 | if (';' == $query[$i] && !$in_string) { |
||
150 | $queries[] = substr($query, 0, $i); |
||
151 | $query = substr($query, $i + 1); |
||
152 | $i = 0; |
||
153 | } |
||
154 | if ($in_string && ($query[$i] == $in_string) && '\\' != $buffer[1]) { |
||
155 | $in_string = false; |
||
156 | } elseif (!$in_string && ('"' == $query[$i] || "'" == $query[$i]) && (!isset($buffer[0]) || '\\' != $buffer[0])) { |
||
157 | $in_string = $query[$i]; |
||
158 | } |
||
159 | if (isset($buffer[1])) { |
||
160 | $buffer[0] = $buffer[1]; |
||
161 | } |
||
162 | $buffer[1] = $query[$i]; |
||
163 | } |
||
164 | // If the is anything left over, add it to the queries. |
||
165 | if (!empty($query)) { |
||
166 | $queries[] = $query; |
||
167 | } |
||
168 | // Add function part as is |
||
169 | $countFunct = \count($funct); |
||
170 | for ($f = 1; $f < $countFunct; ++$f) { |
||
171 | $queries[] = 'CREATE || REPLACE FUNCTION ' . $funct[$f]; |
||
172 | } |
||
173 | return $queries; |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * Set default language. |
||
178 | * |
||
179 | * @return void |
||
180 | */ |
||
181 | private function setLanguage() |
||
186 | } |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * Create config files. |
||
191 | */ |
||
192 | private function createConfigFiles() |
||
216 |