| Total Complexity | 44 | 
| Total Lines | 343 | 
| Duplicated Lines | 0 % | 
| Changes | 1 | ||
| Bugs | 0 | Features | 0 | 
Complex classes like BatchUpdateModule 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 BatchUpdateModule, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 49 | class BatchUpdateModule extends AbstractModule implements ModuleConfigInterface  | 
            ||
| 50 | { | 
            ||
| 51 | use ModuleConfigTrait;  | 
            ||
| 52 | |||
| 53 | /** @var string */  | 
            ||
| 54 | protected $layout = 'layouts/administration';  | 
            ||
| 55 | |||
| 56 | /** @var TreeService */  | 
            ||
| 57 | private $tree_service;  | 
            ||
| 58 | |||
| 59 | /**  | 
            ||
| 60 | * BatchUpdateModule constructor.  | 
            ||
| 61 | *  | 
            ||
| 62 | * @param TreeService $tree_service  | 
            ||
| 63 | */  | 
            ||
| 64 | public function __construct(TreeService $tree_service)  | 
            ||
| 65 |     { | 
            ||
| 66 | $this->tree_service = $tree_service;  | 
            ||
| 67 | }  | 
            ||
| 68 | |||
| 69 | /**  | 
            ||
| 70 | * How should this module be identified in the control panel, etc.?  | 
            ||
| 71 | *  | 
            ||
| 72 | * @return string  | 
            ||
| 73 | */  | 
            ||
| 74 | public function title(): string  | 
            ||
| 75 |     { | 
            ||
| 76 | /* I18N: Name of a module */  | 
            ||
| 77 |         return I18N::translate('Batch update'); | 
            ||
| 78 | }  | 
            ||
| 79 | |||
| 80 | /**  | 
            ||
| 81 | * A sentence describing what this module does.  | 
            ||
| 82 | *  | 
            ||
| 83 | * @return string  | 
            ||
| 84 | */  | 
            ||
| 85 | public function description(): string  | 
            ||
| 86 |     { | 
            ||
| 87 | /* I18N: Description of the “Batch update” module */  | 
            ||
| 88 |         return I18N::translate('Apply automatic corrections to your genealogy data.'); | 
            ||
| 89 | }  | 
            ||
| 90 | |||
| 91 | /**  | 
            ||
| 92 | * Main entry point  | 
            ||
| 93 | *  | 
            ||
| 94 | * @param ServerRequestInterface $request  | 
            ||
| 95 | *  | 
            ||
| 96 | * @return ResponseInterface  | 
            ||
| 97 | */  | 
            ||
| 98 | public function getAdminAction(ServerRequestInterface $request): ResponseInterface  | 
            ||
| 99 |     { | 
            ||
| 100 | $tree = $request->getQueryParams()['tree'] ?? '';  | 
            ||
| 101 |         $user    = $request->getAttribute('user'); | 
            ||
| 102 | $plugin = $request->getQueryParams()['plugin'] ?? '';  | 
            ||
| 103 | $xref = $request->getQueryParams()['xref'] ?? '';  | 
            ||
| 104 | $plugins = $this->getPluginList();  | 
            ||
| 105 | $plugin = $plugins[$plugin] ?? null;  | 
            ||
| 106 | |||
| 107 | // This module can't run without a tree  | 
            ||
| 108 | $tree = $this->tree_service->findByName($tree) ?? $this->tree_service->all()->first();  | 
            ||
| 109 |         if (!$tree instanceof Tree) { | 
            ||
| 110 | return redirect(route(ControlPanel::class));  | 
            ||
| 111 | }  | 
            ||
| 112 | |||
| 113 | $curr_xref = '';  | 
            ||
| 114 | $prev_xref = '';  | 
            ||
| 115 | $next_xref = '';  | 
            ||
| 116 | |||
| 117 | // Don't do any processing until a plugin is chosen.  | 
            ||
| 118 |         if ($plugin !== null) { | 
            ||
| 119 | $plugin->getOptions($request);  | 
            ||
| 120 | |||
| 121 | $all_data = $this->allData($plugin, $tree);  | 
            ||
| 122 | |||
| 123 | // Make sure that our requested record really does need updating.  | 
            ||
| 124 | // It may have been updated in another session, or may not have  | 
            ||
| 125 | // been specified at all.  | 
            ||
| 126 |             if (array_key_exists($xref, $all_data) && $plugin->doesRecordNeedUpdate($this->getRecord($all_data[$xref], $tree))) { | 
            ||
| 127 | $curr_xref = $xref;  | 
            ||
| 128 | }  | 
            ||
| 129 | |||
| 130 | // The requested record doesn't need updating - find one that does  | 
            ||
| 131 |             if ($curr_xref === '') { | 
            ||
| 132 | $curr_xref = $this->findNextXref($plugin, $xref, $all_data, $tree);  | 
            ||
| 133 | }  | 
            ||
| 134 |             if ($curr_xref === '') { | 
            ||
| 135 | $curr_xref = $this->findPrevXref($plugin, $xref, $all_data, $tree);  | 
            ||
| 136 | }  | 
            ||
| 137 | |||
| 138 | // If we've found a record to update, get details and look for the next/prev  | 
            ||
| 139 |             if ($curr_xref !== '') { | 
            ||
| 140 | $prev_xref = $this->findPrevXref($plugin, $xref, $all_data, $tree);  | 
            ||
| 141 | $next_xref = $this->findNextXref($plugin, $xref, $all_data, $tree);  | 
            ||
| 142 | }  | 
            ||
| 143 | }  | 
            ||
| 144 | |||
| 145 |         return $this->viewResponse('modules/batch_update/admin', [ | 
            ||
| 146 |             'auto_accept' => (bool) $user->getPreference('auto_accept'), | 
            ||
| 147 | 'plugins' => $plugins,  | 
            ||
| 148 | 'curr_xref' => $curr_xref,  | 
            ||
| 149 | 'next_xref' => $next_xref,  | 
            ||
| 150 | 'plugin' => $plugin,  | 
            ||
| 151 | 'module' => $this->name(),  | 
            ||
| 152 | 'record' => GedcomRecord::getInstance($curr_xref, $tree),  | 
            ||
| 153 | 'prev_xref' => $prev_xref,  | 
            ||
| 154 |             'title'       => I18N::translate('Batch update'), | 
            ||
| 155 | 'tree' => $tree,  | 
            ||
| 156 | 'trees' => Tree::getNameList(),  | 
            ||
| 157 | ]);  | 
            ||
| 158 | }  | 
            ||
| 159 | |||
| 160 | /**  | 
            ||
| 161 | * Scan the plugin folder for a list of plugins  | 
            ||
| 162 | *  | 
            ||
| 163 | * @return BatchUpdateBasePlugin[]  | 
            ||
| 164 | */  | 
            ||
| 165 | private function getPluginList(): array  | 
            ||
| 166 |     { | 
            ||
| 167 | $plugins = [];  | 
            ||
| 168 | $files = glob(__DIR__ . '/BatchUpdate/BatchUpdate*Plugin.php', GLOB_NOSORT);  | 
            ||
| 169 | |||
| 170 |         foreach ($files as $file) { | 
            ||
| 171 | $base_class = basename($file, '.php');  | 
            ||
| 172 | |||
| 173 |             if ($base_class !== 'BatchUpdateBasePlugin') { | 
            ||
| 174 | $class = __NAMESPACE__ . '\\BatchUpdate\\' . basename($file, '.php');  | 
            ||
| 175 | $plugins[$class] = new $class();  | 
            ||
| 176 | }  | 
            ||
| 177 | }  | 
            ||
| 178 | |||
| 179 | return $plugins;  | 
            ||
| 180 | }  | 
            ||
| 181 | |||
| 182 | /**  | 
            ||
| 183 | * Fetch all records that might need updating.  | 
            ||
| 184 | *  | 
            ||
| 185 | * @param BatchUpdateBasePlugin $plugin  | 
            ||
| 186 | * @param Tree $tree  | 
            ||
| 187 | *  | 
            ||
| 188 | * @return object[]  | 
            ||
| 189 | */  | 
            ||
| 190 | private function allData(BatchUpdateBasePlugin $plugin, Tree $tree): array  | 
            ||
| 191 |     { | 
            ||
| 192 | $tmp = [];  | 
            ||
| 193 | |||
| 194 |         foreach ($plugin->getRecordTypesToUpdate() as $type) { | 
            ||
| 195 |             switch ($type) { | 
            ||
| 196 | case 'INDI':  | 
            ||
| 197 |                     $rows = DB::table('individuals') | 
            ||
| 198 |                         ->where('i_file', '=', $tree->id()) | 
            ||
| 199 |                         ->select(['i_id AS xref', new Expression("'INDI' AS type"), 'i_gedcom AS gedcom']) | 
            ||
| 200 | ->get();  | 
            ||
| 201 | |||
| 202 | $tmp = array_merge($tmp, $rows->all());  | 
            ||
| 203 | break;  | 
            ||
| 204 | |||
| 205 | case 'FAM':  | 
            ||
| 206 |                     $rows = DB::table('families') | 
            ||
| 207 |                         ->where('f_file', '=', $tree->id()) | 
            ||
| 208 |                         ->select(['f_id AS xref', new Expression("'FAM' AS type"), 'f_gedcom AS gedcom']) | 
            ||
| 209 | ->get();  | 
            ||
| 210 | |||
| 211 | $tmp = array_merge($tmp, $rows->all());  | 
            ||
| 212 | break;  | 
            ||
| 213 | |||
| 214 | case 'SOUR':  | 
            ||
| 215 |                     $rows = DB::table('sources') | 
            ||
| 216 |                         ->where('s_file', '=', $tree->id()) | 
            ||
| 217 |                         ->select(['s_id AS xref', new Expression("'SOUR' AS type"), 's_gedcom AS gedcom']) | 
            ||
| 218 | ->get();  | 
            ||
| 219 | |||
| 220 | $tmp = array_merge($tmp, $rows->all());  | 
            ||
| 221 | break;  | 
            ||
| 222 | |||
| 223 | case 'OBJE':  | 
            ||
| 224 |                     $rows = DB::table('media') | 
            ||
| 225 |                         ->where('m_file', '=', $tree->id()) | 
            ||
| 226 |                         ->select(['m_id AS xref', new Expression("'OBJE' AS type"), 'm_gedcom AS gedcom']) | 
            ||
| 227 | ->get();  | 
            ||
| 228 | |||
| 229 | $tmp = array_merge($tmp, $rows->all());  | 
            ||
| 230 | break;  | 
            ||
| 231 | |||
| 232 | default:  | 
            ||
| 233 |                     $rows = DB::table('other') | 
            ||
| 234 |                         ->where('o_file', '=', $tree->id()) | 
            ||
| 235 |                         ->where('o_type', '=', $type) | 
            ||
| 236 | ->select(['o_id AS xref', 'o_type AS type', 'o_gedcom AS gedcom'])  | 
            ||
| 237 | ->get();  | 
            ||
| 238 | |||
| 239 | $tmp = array_merge($tmp, $rows->all());  | 
            ||
| 240 | break;  | 
            ||
| 241 | }  | 
            ||
| 242 | }  | 
            ||
| 243 | |||
| 244 | $data = [];  | 
            ||
| 245 | |||
| 246 |         foreach ($tmp as $value) { | 
            ||
| 247 | $data[$value->xref] = $value;  | 
            ||
| 248 | }  | 
            ||
| 249 | |||
| 250 | ksort($tmp);  | 
            ||
| 251 | |||
| 252 | return $data;  | 
            ||
| 253 | }  | 
            ||
| 254 | |||
| 255 | /**  | 
            ||
| 256 | * @param stdClass $record  | 
            ||
| 257 | * @param Tree $tree  | 
            ||
| 258 | *  | 
            ||
| 259 | * @return GedcomRecord  | 
            ||
| 260 | */  | 
            ||
| 261 | public function getRecord(stdClass $record, Tree $tree): GedcomRecord  | 
            ||
| 262 |     { | 
            ||
| 263 |         switch ($record->type) { | 
            ||
| 264 | case 'INDI':  | 
            ||
| 265 | return Individual::getInstance($record->xref, $tree, $record->gedcom);  | 
            ||
| 266 | |||
| 267 | case 'FAM':  | 
            ||
| 268 | return Family::getInstance($record->xref, $tree, $record->gedcom);  | 
            ||
| 269 | |||
| 270 | case 'SOUR':  | 
            ||
| 271 | return Source::getInstance($record->xref, $tree, $record->gedcom);  | 
            ||
| 272 | |||
| 273 | case 'REPO':  | 
            ||
| 274 | return Repository::getInstance($record->xref, $tree, $record->gedcom);  | 
            ||
| 275 | |||
| 276 | case 'OBJE':  | 
            ||
| 277 | return Media::getInstance($record->xref, $tree, $record->gedcom);  | 
            ||
| 278 | |||
| 279 | case 'NOTE':  | 
            ||
| 280 | return Note::getInstance($record->xref, $tree, $record->gedcom);  | 
            ||
| 281 | |||
| 282 | default:  | 
            ||
| 283 | return GedcomRecord::getInstance($record->xref, $tree, $record->gedcom);  | 
            ||
| 284 | }  | 
            ||
| 285 | }  | 
            ||
| 286 | |||
| 287 | /**  | 
            ||
| 288 | * Find the next record that needs to be updated.  | 
            ||
| 289 | *  | 
            ||
| 290 | * @param BatchUpdateBasePlugin $plugin  | 
            ||
| 291 | * @param string $xref  | 
            ||
| 292 | * @param array $all_data  | 
            ||
| 293 | * @param Tree $tree  | 
            ||
| 294 | *  | 
            ||
| 295 | * @return string  | 
            ||
| 296 | */  | 
            ||
| 297 | private function findNextXref(BatchUpdateBasePlugin $plugin, string $xref, array $all_data, Tree $tree): string  | 
            ||
| 298 |     { | 
            ||
| 299 |         foreach (array_keys($all_data) as $key) { | 
            ||
| 300 |             if ($key > $xref) { | 
            ||
| 301 | $record = $this->getRecord($all_data[$key], $tree);  | 
            ||
| 302 |                 if ($plugin->doesRecordNeedUpdate($record)) { | 
            ||
| 303 | return $key;  | 
            ||
| 304 | }  | 
            ||
| 305 | }  | 
            ||
| 306 | }  | 
            ||
| 307 | |||
| 308 | return '';  | 
            ||
| 309 | }  | 
            ||
| 310 | |||
| 311 | /**  | 
            ||
| 312 | * Find the previous record that needs to be updated.  | 
            ||
| 313 | *  | 
            ||
| 314 | * @param BatchUpdateBasePlugin $plugin  | 
            ||
| 315 | * @param string $xref  | 
            ||
| 316 | * @param array $all_data  | 
            ||
| 317 | * @param Tree $tree  | 
            ||
| 318 | *  | 
            ||
| 319 | * @return string  | 
            ||
| 320 | */  | 
            ||
| 321 | private function findPrevXref(BatchUpdateBasePlugin $plugin, string $xref, array $all_data, Tree $tree): string  | 
            ||
| 333 | }  | 
            ||
| 334 | |||
| 335 | /**  | 
            ||
| 336 | * Perform an update  | 
            ||
| 337 | *  | 
            ||
| 338 | * @param ServerRequestInterface $request  | 
            ||
| 339 | *  | 
            ||
| 340 | * @return ResponseInterface  | 
            ||
| 341 | */  | 
            ||
| 342 | public function postAdminAction(ServerRequestInterface $request): ResponseInterface  | 
            ||
| 392 | }  | 
            ||
| 393 | }  | 
            ||
| 394 |