| Total Complexity | 81 |
| Total Lines | 414 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like DatabaseAdmin 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 DatabaseAdmin, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class DatabaseAdmin extends Controller |
||
| 24 | { |
||
| 25 | |||
| 26 | /// SECURITY /// |
||
| 27 | private static $allowed_actions = array( |
||
|
|
|||
| 28 | 'index', |
||
| 29 | 'build', |
||
| 30 | 'cleanup', |
||
| 31 | 'import' |
||
| 32 | ); |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Obsolete classname values that should be remapped in dev/build |
||
| 36 | */ |
||
| 37 | private static $classname_value_remapping = [ |
||
| 38 | 'File' => 'SilverStripe\\Assets\\File', |
||
| 39 | 'Image' => 'SilverStripe\\Assets\\Image', |
||
| 40 | 'Folder' => 'SilverStripe\\Assets\\Folder', |
||
| 41 | 'Group' => 'SilverStripe\\Security\\Group', |
||
| 42 | 'LoginAttempt' => 'SilverStripe\\Security\\LoginAttempt', |
||
| 43 | 'Member' => 'SilverStripe\\Security\\Member', |
||
| 44 | 'MemberPassword' => 'SilverStripe\\Security\\MemberPassword', |
||
| 45 | 'Permission' => 'SilverStripe\\Security\\Permission', |
||
| 46 | 'PermissionRole' => 'SilverStripe\\Security\\PermissionRole', |
||
| 47 | 'PermissionRoleCode' => 'SilverStripe\\Security\\PermissionRoleCode', |
||
| 48 | 'RememberLoginHash' => 'SilverStripe\\Security\\RememberLoginHash', |
||
| 49 | ]; |
||
| 50 | |||
| 51 | protected function init() |
||
| 52 | { |
||
| 53 | parent::init(); |
||
| 54 | |||
| 55 | // We allow access to this controller regardless of live-status or ADMIN permission only |
||
| 56 | // if on CLI or with the database not ready. The latter makes it less errorprone to do an |
||
| 57 | // initial schema build without requiring a default-admin login. |
||
| 58 | // Access to this controller is always allowed in "dev-mode", or of the user is ADMIN. |
||
| 59 | $allowAllCLI = DevelopmentAdmin::config()->get('allow_all_cli'); |
||
| 60 | $canAccess = ( |
||
| 61 | Director::isDev() |
||
| 62 | || !Security::database_is_ready() |
||
| 63 | // We need to ensure that DevelopmentAdminTest can simulate permission failures when running |
||
| 64 | // "dev/tests" from CLI. |
||
| 65 | || (Director::is_cli() && $allowAllCLI) |
||
| 66 | || Permission::check("ADMIN") |
||
| 67 | ); |
||
| 68 | if (!$canAccess) { |
||
| 69 | Security::permissionFailure( |
||
| 70 | $this, |
||
| 71 | "This page is secured and you need administrator rights to access it. " . |
||
| 72 | "Enter your credentials below and we will send you right along." |
||
| 73 | ); |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Get the data classes, grouped by their root class |
||
| 79 | * |
||
| 80 | * @return array Array of data classes, grouped by their root class |
||
| 81 | */ |
||
| 82 | public function groupedDataClasses() |
||
| 83 | { |
||
| 84 | // Get all root data objects |
||
| 85 | $allClasses = get_declared_classes(); |
||
| 86 | $rootClasses = []; |
||
| 87 | foreach ($allClasses as $class) { |
||
| 88 | if (get_parent_class($class) == DataObject::class) { |
||
| 89 | $rootClasses[$class] = array(); |
||
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 93 | // Assign every other data object one of those |
||
| 94 | foreach ($allClasses as $class) { |
||
| 95 | if (!isset($rootClasses[$class]) && is_subclass_of($class, DataObject::class)) { |
||
| 96 | foreach ($rootClasses as $rootClass => $dummy) { |
||
| 97 | if (is_subclass_of($class, $rootClass)) { |
||
| 98 | $rootClasses[$rootClass][] = $class; |
||
| 99 | break; |
||
| 100 | } |
||
| 101 | } |
||
| 102 | } |
||
| 103 | } |
||
| 104 | return $rootClasses; |
||
| 105 | } |
||
| 106 | |||
| 107 | |||
| 108 | /** |
||
| 109 | * When we're called as /dev/build, that's actually the index. Do the same |
||
| 110 | * as /dev/build/build. |
||
| 111 | */ |
||
| 112 | public function index() |
||
| 113 | { |
||
| 114 | return $this->build(); |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Updates the database schema, creating tables & fields as necessary. |
||
| 119 | */ |
||
| 120 | public function build() |
||
| 121 | { |
||
| 122 | // The default time limit of 30 seconds is normally not enough |
||
| 123 | Environment::increaseTimeLimitTo(600); |
||
| 124 | |||
| 125 | // If this code is being run outside of a dev/build or without a ?flush query string param, |
||
| 126 | // the class manifest hasn't been flushed, so do it here |
||
| 127 | $request = $this->getRequest(); |
||
| 128 | if (!array_key_exists('flush', $request->getVars()) && strpos($request->getURL(), 'dev/build') !== 0) { |
||
| 129 | ClassLoader::inst()->getManifest()->regenerate(false); |
||
| 130 | } |
||
| 131 | |||
| 132 | $url = $this->getReturnURL(); |
||
| 133 | if ($url) { |
||
| 134 | echo "<p>Setting up the database; you will be returned to your site shortly....</p>"; |
||
| 135 | $this->doBuild(true); |
||
| 136 | echo "<p>Done!</p>"; |
||
| 137 | $this->redirect($url); |
||
| 138 | } else { |
||
| 139 | $quiet = $this->request->requestVar('quiet') !== null; |
||
| 140 | $fromInstaller = $this->request->requestVar('from_installer') !== null; |
||
| 141 | $populate = $this->request->requestVar('dont_populate') === null; |
||
| 142 | $this->doBuild($quiet || $fromInstaller, $populate); |
||
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Gets the url to return to after build |
||
| 148 | * |
||
| 149 | * @return string|null |
||
| 150 | */ |
||
| 151 | protected function getReturnURL() |
||
| 152 | { |
||
| 153 | $url = $this->request->getVar('returnURL'); |
||
| 154 | |||
| 155 | // Check that this url is a site url |
||
| 156 | if (empty($url) || !Director::is_site_url($url)) { |
||
| 157 | return null; |
||
| 158 | } |
||
| 159 | |||
| 160 | // Convert to absolute URL |
||
| 161 | return Director::absoluteURL($url, true); |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Build the default data, calling requireDefaultRecords on all |
||
| 166 | * DataObject classes |
||
| 167 | */ |
||
| 168 | public function buildDefaults() |
||
| 169 | { |
||
| 170 | $dataClasses = ClassInfo::subclassesFor(DataObject::class); |
||
| 171 | array_shift($dataClasses); |
||
| 172 | |||
| 173 | if (!Director::is_cli()) { |
||
| 174 | echo "<ul>"; |
||
| 175 | } |
||
| 176 | |||
| 177 | foreach ($dataClasses as $dataClass) { |
||
| 178 | singleton($dataClass)->requireDefaultRecords(); |
||
| 179 | if (Director::is_cli()) { |
||
| 180 | echo "Defaults loaded for $dataClass\n"; |
||
| 181 | } else { |
||
| 182 | echo "<li>Defaults loaded for $dataClass</li>\n"; |
||
| 183 | } |
||
| 184 | } |
||
| 185 | |||
| 186 | if (!Director::is_cli()) { |
||
| 187 | echo "</ul>"; |
||
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Returns the timestamp of the time that the database was last built |
||
| 193 | * |
||
| 194 | * @return string Returns the timestamp of the time that the database was |
||
| 195 | * last built |
||
| 196 | */ |
||
| 197 | public static function lastBuilt() |
||
| 198 | { |
||
| 199 | $file = TEMP_PATH |
||
| 200 | . DIRECTORY_SEPARATOR |
||
| 201 | . 'database-last-generated-' |
||
| 202 | . str_replace(array('\\','/',':'), '.', Director::baseFolder()); |
||
| 203 | |||
| 204 | if (file_exists($file)) { |
||
| 205 | return filemtime($file); |
||
| 206 | } |
||
| 207 | return null; |
||
| 208 | } |
||
| 209 | |||
| 210 | |||
| 211 | /** |
||
| 212 | * Updates the database schema, creating tables & fields as necessary. |
||
| 213 | * |
||
| 214 | * @param boolean $quiet Don't show messages |
||
| 215 | * @param boolean $populate Populate the database, as well as setting up its schema |
||
| 216 | * @param bool $testMode |
||
| 217 | */ |
||
| 218 | public function doBuild($quiet = false, $populate = true, $testMode = false) |
||
| 389 | } |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Remove invalid records from tables - that is, records that don't have |
||
| 393 | * corresponding records in their parent class tables. |
||
| 394 | */ |
||
| 395 | public function cleanup() |
||
| 437 | } |
||
| 438 | } |
||
| 439 | } |
||
| 440 | } |
||
| 441 | } |
||
| 444 |