Complex classes like DatabaseInstaller 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 DatabaseInstaller, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class DatabaseInstaller |
||
28 | { |
||
29 | |||
30 | use InstanceConfigTrait; |
||
31 | |||
32 | /** |
||
33 | * Error messages list. |
||
34 | * |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $_errors = []; |
||
38 | |||
39 | /** |
||
40 | * Whether the install() method was invoked or not. |
||
41 | * |
||
42 | * @var bool |
||
43 | */ |
||
44 | protected $_installed = false; |
||
45 | |||
46 | /** |
||
47 | * Default configuration for this class. |
||
48 | * |
||
49 | * - settingsPath: Full path to the "settings.php" file where store connection |
||
50 | * information used by QuickAppsCMS. This should NEVER be changed, use with |
||
51 | * caution. |
||
52 | * |
||
53 | * - schemaPath: Path to directory containing all tables information to be |
||
54 | * imported (fixtures). |
||
55 | * |
||
56 | * - maxExecutionTime: Time in seconds for PHP's "max_execution_time" directive. |
||
57 | * Defaults to 480 (8 minutes). |
||
58 | * |
||
59 | * @var array |
||
60 | */ |
||
61 | protected $_defaultConfig = [ |
||
62 | 'settingsPath' => null, |
||
63 | 'schemaPath' => null, |
||
64 | 'maxExecutionTime' => 480, |
||
65 | ]; |
||
66 | |||
67 | /** |
||
68 | * Default database connection config. |
||
69 | * |
||
70 | * @var array |
||
71 | */ |
||
72 | protected $_defaultConnection = [ |
||
73 | 'className' => 'Cake\Database\Connection', |
||
74 | 'driver' => '', |
||
75 | 'database' => '', |
||
76 | 'username' => '', |
||
77 | 'password' => '', |
||
78 | 'host' => '', |
||
79 | 'prefix' => '', |
||
80 | 'encoding' => 'utf8', |
||
81 | 'timezone' => 'UTC', |
||
82 | 'log' => false, |
||
83 | 'cacheMetadata' => true, |
||
84 | ]; |
||
85 | |||
86 | /** |
||
87 | * Constructor. |
||
88 | * |
||
89 | * @param array $config Configuration options |
||
90 | */ |
||
91 | public function __construct($config = []) |
||
92 | { |
||
93 | $this->_defaultConfig['settingsPath'] = ROOT . '/config/settings.php'; |
||
94 | $this->_defaultConfig['schemaPath'] = dirname(dirname(__DIR__)) . '/config/fixture/'; |
||
95 | $this->config($config); |
||
96 | |||
97 | if (function_exists('ini_set')) { |
||
98 | ini_set('max_execution_time', (int)$this->config('maxExecutionTime')); |
||
99 | } elseif (function_exists('set_time_limit')) { |
||
100 | set_time_limit((int)$this->config('maxExecutionTime')); |
||
101 | } |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Starts the process. |
||
106 | * |
||
107 | * @param array $dbConfig Database connection information |
||
108 | * @return bool True on success, false otherwise |
||
109 | */ |
||
110 | public function install($dbConfig = []) |
||
111 | { |
||
112 | $this->_installed = true; |
||
113 | |||
114 | if (!$this->prepareConfig($dbConfig)) { |
||
115 | return false; |
||
116 | } |
||
117 | |||
118 | $conn = $this->getConn(); |
||
119 | if ($conn === false) { |
||
120 | return false; |
||
121 | } |
||
122 | |||
123 | if (!$this->isDbEmpty($conn)) { |
||
124 | return false; |
||
125 | } |
||
126 | |||
127 | if (!$this->importTables($conn)) { |
||
128 | return false; |
||
129 | } |
||
130 | |||
131 | $this->writeSetting(); |
||
132 | |||
133 | return true; |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * Registers an error message. |
||
138 | * |
||
139 | * @param string $message The error message |
||
140 | * @return void |
||
141 | */ |
||
142 | public function error($message) |
||
146 | |||
147 | /** |
||
148 | * Get all error messages. |
||
149 | * |
||
150 | * @return array |
||
151 | */ |
||
152 | public function errors() |
||
160 | |||
161 | /** |
||
162 | * Prepares database configuration attributes. |
||
163 | * |
||
164 | * If the file "ROOT/config/settings.php.tmp" exists, and has declared a |
||
165 | * connection named "default" it will be used. |
||
166 | * |
||
167 | * @param array $dbConfig Database connection info coming from POST |
||
168 | * @return bool True on success, false otherwise |
||
169 | */ |
||
170 | public function prepareConfig($dbConfig = []) |
||
204 | |||
205 | /** |
||
206 | * Generates a new connection to DB. |
||
207 | * |
||
208 | * @return \Cake\Database\Connection|bool A connection object, or false on |
||
209 | * failure. On failure error messages are automatically set |
||
210 | */ |
||
211 | public function getConn() |
||
232 | |||
233 | /** |
||
234 | * Imports tables schema and populates them. |
||
235 | * |
||
236 | * @param \Cake\Database\Connection $conn Database connection to use |
||
237 | * @return bool True on success, false otherwise. On failure error messages |
||
238 | * are automatically set |
||
239 | */ |
||
240 | public function importTables($conn) |
||
263 | |||
264 | /** |
||
265 | * Checks whether connected database is empty or not. |
||
266 | * |
||
267 | * @param \Cake\Database\Connection $conn Database connection to use |
||
268 | * @return bool True if database if empty and tables can be imported, false if |
||
269 | * there are some existing tables |
||
270 | */ |
||
271 | public function isDbEmpty($conn) |
||
285 | |||
286 | /** |
||
287 | * Creates site's "settings.php" file. |
||
288 | * |
||
289 | * @return bool True on success |
||
290 | */ |
||
291 | public function writeSetting() |
||
312 | |||
313 | /** |
||
314 | * Generates a random string suitable for security's salt. |
||
315 | * |
||
316 | * @return string |
||
317 | */ |
||
318 | public function salt() |
||
324 | |||
325 | /** |
||
326 | * Process the given fixture class, creates its schema and imports its records. |
||
327 | * |
||
328 | * @param string $path Full path to schema class file |
||
329 | * @param \Cake\Database\Connection $connection Database connection to use |
||
330 | * @return bool True on success |
||
331 | */ |
||
332 | protected function _processFixture($path, Connection $connection) |
||
365 | |||
366 | /** |
||
367 | * Gets an schema instance for the given fixture class. |
||
368 | * |
||
369 | * @param string $fixtureClassName The fixture to be "converted" |
||
370 | * @return \Cake\Database\Schema\Table Schema instance |
||
371 | */ |
||
372 | protected function _prepareSchema($fixtureClassName) |
||
398 | |||
399 | /** |
||
400 | * Extracts some properties from the given fixture instance to properly |
||
401 | * build a new table schema instance (constrains, indexes, etc). |
||
402 | * |
||
403 | * @param object $fixture Fixture instance from which extract schema |
||
404 | * properties |
||
405 | * @return array Where with keys 0 => $fields, 1 => $constraints, 2 => |
||
406 | * $indexes and 3 => $options |
||
407 | */ |
||
408 | protected function _prepareSchemaProperties($fixture) |
||
437 | |||
438 | /** |
||
439 | * Imports all records of the given fixture. |
||
440 | * |
||
441 | * @param string $fixtureClassName Fixture class name |
||
442 | * @param \Cake\Database\Schema\Table $schema Table schema for which records |
||
443 | * will be imported |
||
444 | * @param \Cake\Database\Connection $connection Database connection to use |
||
445 | * @return bool True on success |
||
446 | */ |
||
447 | protected function _importRecords($fixtureClassName, TableSchema $schema, Connection $connection) |
||
483 | |||
484 | /** |
||
485 | * Converts the given array of records into data used to generate a query. |
||
486 | * |
||
487 | * @param array $records Records to be imported |
||
488 | * @param \Cake\Database\Schema\Table $schema Table schema for which records will |
||
489 | * be imported |
||
490 | * @return array |
||
491 | */ |
||
492 | protected function _getRecords(array $records, TableSchema $schema) |
||
510 | } |
||
511 |