| Total Complexity | 43 |
| Total Lines | 308 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like TempDatabase 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 TempDatabase, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class TempDatabase |
||
| 17 | { |
||
| 18 | use Injectable; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Connection name |
||
| 22 | * |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $name = null; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Workaround to avoid infinite loops. |
||
| 29 | * |
||
| 30 | * @var Exception |
||
| 31 | */ |
||
| 32 | private $skippedException = null; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Optionally remove the test DB when the PHP process exits |
||
| 36 | * |
||
| 37 | * @var boolean |
||
| 38 | */ |
||
| 39 | private static $teardown_on_exit = true; |
||
|
|
|||
| 40 | |||
| 41 | /** |
||
| 42 | * Create a new temp database |
||
| 43 | * |
||
| 44 | * @param string $name DB Connection name to use |
||
| 45 | */ |
||
| 46 | public function __construct($name = 'default') |
||
| 47 | { |
||
| 48 | $this->name = $name; |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Check if the given name matches the temp_db pattern |
||
| 53 | * |
||
| 54 | * @param string $name |
||
| 55 | * @return bool |
||
| 56 | */ |
||
| 57 | protected function isDBTemp($name) |
||
| 58 | { |
||
| 59 | $prefix = Environment::getEnv('SS_DATABASE_PREFIX') ?: 'ss_'; |
||
| 60 | $result = preg_match( |
||
| 61 | sprintf('/^%stmpdb_[0-9]+_[0-9]+$/i', preg_quote($prefix, '/')), |
||
| 62 | $name ?: '' |
||
| 63 | ); |
||
| 64 | return $result === 1; |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @return Database |
||
| 69 | */ |
||
| 70 | protected function getConn() |
||
| 71 | { |
||
| 72 | return DB::get_conn($this->name); |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Returns true if we are currently using a temporary database |
||
| 77 | * |
||
| 78 | * @return bool |
||
| 79 | */ |
||
| 80 | public function isUsed() |
||
| 81 | { |
||
| 82 | $selected = $this->getConn()->getSelectedDatabase(); |
||
| 83 | return $this->isDBTemp($selected); |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @return bool |
||
| 88 | */ |
||
| 89 | public function supportsTransactions() |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Start a transaction for easy rollback after tests |
||
| 96 | */ |
||
| 97 | public function startTransaction() |
||
| 98 | { |
||
| 99 | if (static::getConn()->supportsTransactions()) { |
||
| 100 | static::getConn()->transactionStart(); |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Rollback a transaction (or trash all data if the DB doesn't support databases |
||
| 106 | * |
||
| 107 | * @return bool True if successfully rolled back, false otherwise. On error the DB is |
||
| 108 | * killed and must be re-created. Note that calling rollbackTransaction() when there |
||
| 109 | * is no transaction is counted as a failure, user code should either kill or flush the DB |
||
| 110 | * as necessary |
||
| 111 | */ |
||
| 112 | public function rollbackTransaction() |
||
| 113 | { |
||
| 114 | // Ensure a rollback can be performed |
||
| 115 | $success = static::getConn()->supportsTransactions() |
||
| 116 | && static::getConn()->transactionDepth(); |
||
| 117 | if (!$success) { |
||
| 118 | return false; |
||
| 119 | } |
||
| 120 | try { |
||
| 121 | // Explicit false = gnostic error from transactionRollback |
||
| 122 | if (static::getConn()->transactionRollback() === false) { |
||
| 123 | return false; |
||
| 124 | } |
||
| 125 | return true; |
||
| 126 | } catch (DatabaseException $ex) { |
||
| 127 | return false; |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Destroy the current temp database |
||
| 133 | */ |
||
| 134 | public function kill() |
||
| 135 | { |
||
| 136 | // Nothing to kill |
||
| 137 | if (!$this->isUsed()) { |
||
| 138 | return; |
||
| 139 | } |
||
| 140 | |||
| 141 | // Rollback any transactions (note: Success ignored) |
||
| 142 | $this->rollbackTransaction(); |
||
| 143 | |||
| 144 | // Check the database actually exists |
||
| 145 | $dbConn = $this->getConn(); |
||
| 146 | $dbName = $dbConn->getSelectedDatabase(); |
||
| 147 | if (!$dbConn->databaseExists($dbName)) { |
||
| 148 | return; |
||
| 149 | } |
||
| 150 | |||
| 151 | // Some DataExtensions keep a static cache of information that needs to |
||
| 152 | // be reset whenever the database is killed |
||
| 153 | foreach (ClassInfo::subclassesFor(DataExtension::class) as $class) { |
||
| 154 | $toCall = [$class, 'on_db_reset']; |
||
| 155 | if (is_callable($toCall)) { |
||
| 156 | call_user_func($toCall); |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | $dbConn->dropSelectedDatabase(); |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Remove all content from the temporary database. |
||
| 165 | */ |
||
| 166 | public function clearAllData() |
||
| 184 | } |
||
| 185 | } |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Create temp DB without creating extra objects |
||
| 190 | * |
||
| 191 | * @return string DB name |
||
| 192 | */ |
||
| 193 | public function build() |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Rebuild all database tables |
||
| 229 | * |
||
| 230 | * @param array $extraDataObjects |
||
| 231 | */ |
||
| 232 | protected function rebuildTables($extraDataObjects = []) |
||
| 233 | { |
||
| 234 | DataObject::reset(); |
||
| 235 | |||
| 236 | // clear singletons, they're caching old extension info which is used in DatabaseAdmin->doBuild() |
||
| 237 | Injector::inst()->unregisterObjects(DataObject::class); |
||
| 238 | |||
| 239 | $dataClasses = ClassInfo::subclassesFor(DataObject::class); |
||
| 240 | array_shift($dataClasses); |
||
| 241 | |||
| 242 | $oldCheckAndRepairOnBuild = Config::inst()->get(DBSchemaManager::class, 'check_and_repair_on_build'); |
||
| 243 | Config::modify()->set(DBSchemaManager::class, 'check_and_repair_on_build', false); |
||
| 244 | |||
| 245 | $schema = $this->getConn()->getSchemaManager(); |
||
| 246 | $schema->quiet(); |
||
| 247 | $schema->schemaUpdate( |
||
| 248 | function () use ($dataClasses, $extraDataObjects) { |
||
| 249 | foreach ($dataClasses as $dataClass) { |
||
| 250 | // Check if class exists before trying to instantiate - this sidesteps any manifest weirdness |
||
| 251 | if (class_exists($dataClass)) { |
||
| 252 | $SNG = singleton($dataClass); |
||
| 253 | if (!($SNG instanceof TestOnly)) { |
||
| 254 | $SNG->requireTable(); |
||
| 255 | } |
||
| 256 | } |
||
| 257 | } |
||
| 258 | |||
| 259 | // If we have additional dataobjects which need schema, do so here: |
||
| 260 | if ($extraDataObjects) { |
||
| 261 | foreach ($extraDataObjects as $dataClass) { |
||
| 262 | $SNG = singleton($dataClass); |
||
| 263 | if (singleton($dataClass) instanceof DataObject) { |
||
| 264 | $SNG->requireTable(); |
||
| 265 | } |
||
| 266 | } |
||
| 267 | } |
||
| 268 | } |
||
| 269 | ); |
||
| 270 | |||
| 271 | Config::modify()->set(DBSchemaManager::class, 'check_and_repair_on_build', $oldCheckAndRepairOnBuild); |
||
| 272 | |||
| 273 | ClassInfo::reset_db_cache(); |
||
| 274 | DataObject::singleton()->flushCache(); |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Clear all temp DBs on this connection |
||
| 279 | * |
||
| 280 | * Note: This will output results to stdout unless suppressOutput |
||
| 281 | * is set on the current db schema |
||
| 282 | */ |
||
| 283 | public function deleteAll() |
||
| 291 | } |
||
| 292 | } |
||
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Reset the testing database's schema. |
||
| 297 | * |
||
| 298 | * @param array $extraDataObjects List of extra dataobjects to build |
||
| 299 | */ |
||
| 300 | public function resetDBSchema(array $extraDataObjects = []) |
||
| 324 | } |
||
| 325 | } |
||
| 327 |