Complex classes like AjaxInstallController 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 AjaxInstallController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | class AjaxInstallController extends AbstractController |
||
| 33 | { |
||
| 34 | /** |
||
| 35 | * @var YamlDumper |
||
| 36 | */ |
||
| 37 | private $yamlManager; |
||
| 38 | |||
| 39 | public function __construct(ContainerInterface $container) |
||
| 40 | { |
||
| 41 | parent::__construct($container); |
||
| 42 | $this->yamlManager = new YamlDumper($this->container->get('kernel')->getRootDir() .'/config', 'custom_parameters.yml'); |
||
| 43 | } |
||
| 44 | |||
| 45 | public function ajaxAction(Request $request) |
||
| 46 | { |
||
| 47 | $stage = $request->request->get('stage'); |
||
| 48 | $status = $this->executeStage($stage); |
||
| 49 | |||
| 50 | return new JsonResponse(['status' => $status]); |
||
| 51 | } |
||
| 52 | |||
| 53 | public function commandLineAction($stage) |
||
| 54 | { |
||
| 55 | return $this->executeStage($stage); |
||
| 56 | } |
||
| 57 | |||
| 58 | private function executeStage($stageName) |
||
| 59 | { |
||
| 60 | switch ($stageName) { |
||
| 61 | case "bundles": |
||
| 62 | return $this->createBundles(); |
||
| 63 | case "install_event": |
||
| 64 | return $this->fireEvent(CoreEvents::CORE_INSTALL_PRE_MODULE); |
||
| 65 | case "extensions": |
||
| 66 | return $this->installModule('ZikulaExtensionsModule'); |
||
| 67 | case "settings": |
||
| 68 | return $this->installModule('ZikulaSettingsModule'); |
||
| 69 | case "theme": |
||
| 70 | return $this->installModule('ZikulaThemeModule'); |
||
| 71 | case "admin": |
||
| 72 | return $this->installModule('ZikulaAdminModule'); |
||
| 73 | case "permissions": |
||
| 74 | return $this->installModule('ZikulaPermissionsModule'); |
||
| 75 | case "groups": |
||
| 76 | return $this->installModule('ZikulaGroupsModule'); |
||
| 77 | case "blocks": |
||
| 78 | return $this->installModule('ZikulaBlocksModule'); |
||
| 79 | case "users": |
||
| 80 | return $this->installModule('ZikulaUsersModule'); |
||
| 81 | case "zauth": |
||
| 82 | return $this->installModule('ZikulaZAuthModule'); |
||
| 83 | case "security": |
||
| 84 | return $this->installModule('ZikulaSecurityCenterModule'); |
||
| 85 | case "categories": |
||
| 86 | return $this->installModule('ZikulaCategoriesModule'); |
||
| 87 | case "mailer": |
||
| 88 | return $this->installModule('ZikulaMailerModule'); |
||
| 89 | case "search": |
||
| 90 | return $this->installModule('ZikulaSearchModule'); |
||
| 91 | case "routes": |
||
| 92 | return $this->installModule('ZikulaRoutesModule'); |
||
| 93 | case "menu": |
||
| 94 | return $this->installModule('ZikulaMenuModule'); |
||
| 95 | case "updateadmin": |
||
| 96 | return $this->updateAdmin(); |
||
| 97 | case "loginadmin": |
||
| 98 | $params = $this->decodeParameters($this->yamlManager->getParameters()); |
||
| 99 | |||
| 100 | return $this->loginAdmin($params); |
||
| 101 | case "activatemodules": |
||
| 102 | return $this->reSyncAndActivateModules(); |
||
| 103 | case "categorize": |
||
| 104 | return $this->categorizeModules(); |
||
| 105 | case "createblocks": |
||
| 106 | return $this->createBlocks(); |
||
| 107 | case "finalizeparameters": |
||
| 108 | return $this->finalizeParameters(); |
||
| 109 | case "installassets": |
||
| 110 | return $this->installAssets(); |
||
| 111 | case "protect": |
||
| 112 | return $this->protectFiles(); |
||
| 113 | } |
||
| 114 | |||
| 115 | return true; |
||
| 116 | } |
||
| 117 | |||
| 118 | private function createBundles() |
||
| 131 | |||
| 132 | private function categorizeModules() |
||
| 133 | { |
||
| 134 | reset(ZikulaKernel::$coreModules); |
||
| 135 | $systemModulesCategories = [ |
||
| 136 | 'ZikulaExtensionsModule' => $this->translator->__('System'), |
||
| 137 | 'ZikulaPermissionsModule' => $this->translator->__('Users'), |
||
| 138 | 'ZikulaGroupsModule' => $this->translator->__('Users'), |
||
| 139 | 'ZikulaBlocksModule' => $this->translator->__('Layout'), |
||
| 140 | 'ZikulaUsersModule' => $this->translator->__('Users'), |
||
| 141 | 'ZikulaZAuthModule' => $this->translator->__('Users'), |
||
| 142 | 'ZikulaThemeModule' => $this->translator->__('Layout'), |
||
| 143 | 'ZikulaSecurityCenterModule' => $this->translator->__('Security'), |
||
| 144 | 'ZikulaCategoriesModule' => $this->translator->__('Content'), |
||
| 145 | 'ZikulaMailerModule' => $this->translator->__('System'), |
||
| 146 | 'ZikulaSearchModule' => $this->translator->__('Content'), |
||
| 147 | 'ZikulaAdminModule' => $this->translator->__('System'), |
||
| 148 | 'ZikulaSettingsModule' => $this->translator->__('System'), |
||
| 149 | 'ZikulaRoutesModule' => $this->translator->__('System'), |
||
| 150 | 'ZikulaMenuModule' => $this->translator->__('Content'), |
||
| 151 | ]; |
||
| 152 | |||
| 153 | foreach (ZikulaKernel::$coreModules as $systemModule => $bundleClass) { |
||
| 154 | $this->setModuleCategory($systemModule, $systemModulesCategories[$systemModule]); |
||
| 155 | } |
||
| 156 | |||
| 157 | return true; |
||
| 158 | } |
||
| 159 | |||
| 160 | private function createBlocks() |
||
| 171 | |||
| 172 | /** |
||
| 173 | * This function inserts the admin's user data |
||
| 174 | */ |
||
| 175 | private function updateAdmin() |
||
| 204 | |||
| 205 | private function finalizeParameters() |
||
| 241 | |||
| 242 | private function installAssets() |
||
| 248 | |||
| 249 | private function protectFiles() |
||
| 276 | |||
| 277 | private function createMainMenuBlock() |
||
| 304 | |||
| 305 | private function fireEvent($eventName) |
||
| 315 | } |
||
| 316 |
If you suppress an error, we recommend checking for the error condition explicitly: