Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like PluginController 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 PluginController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 60 | class PluginController extends AbstractController |
||
| 61 | { |
||
| 62 | /** |
||
| 63 | * @Inject("orm.em") |
||
| 64 | * @var EntityManager |
||
| 65 | */ |
||
| 66 | protected $entityManager; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @Inject("monolog") |
||
| 70 | * @var Logger |
||
| 71 | */ |
||
| 72 | protected $logger; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @Inject(PluginEventHandlerRepository::class) |
||
| 76 | * @var PluginEventHandlerRepository |
||
| 77 | */ |
||
| 78 | protected $pluginEventHandlerRepository; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @Inject(PluginService::class) |
||
| 82 | * @var PluginService |
||
| 83 | */ |
||
| 84 | protected $pluginService; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @Inject("config") |
||
| 88 | * @var array |
||
| 89 | */ |
||
| 90 | protected $appConfig; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @Inject(BaseInfo::class) |
||
| 94 | * @var BaseInfo |
||
| 95 | */ |
||
| 96 | protected $BaseInfo; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @Inject("form.factory") |
||
| 100 | * @var FormFactory |
||
| 101 | */ |
||
| 102 | protected $formFactory; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @Inject(PluginRepository::class) |
||
| 106 | * @var PluginRepository |
||
| 107 | */ |
||
| 108 | protected $pluginRepository; |
||
| 109 | |||
| 110 | |||
| 111 | /** |
||
| 112 | * インストール済プラグイン画面 |
||
| 113 | * |
||
| 114 | * @Route("/{_admin}/store/plugin", name="admin_store_plugin") |
||
| 115 | * @Template("Store/plugin.twig") |
||
| 116 | */ |
||
| 117 | public function index(Application $app, Request $request) |
||
| 118 | { |
||
| 119 | $pluginForms = array(); |
||
| 120 | $configPages = array(); |
||
| 121 | |||
| 122 | $Plugins = $this->pluginRepository->findBy(array(), array('code' => 'ASC')); |
||
| 123 | |||
| 124 | // ファイル設置プラグインの取得. |
||
| 125 | $unregisterdPlugins = $this->getUnregisteredPlugins($Plugins, $app); |
||
| 126 | $unregisterdPluginsConfigPages = array(); |
||
| 127 | foreach ($unregisterdPlugins as $unregisterdPlugin) { |
||
| 128 | try { |
||
| 129 | $code = $unregisterdPlugin['code']; |
||
| 130 | // プラグイン用設定画面があれば表示(プラグイン用のサービスプロバイダーに定義されているか) |
||
| 131 | $unregisterdPluginsConfigPages[$code] = $app->url('plugin_'.$code.'_config'); |
||
| 132 | } catch (RouteNotFoundException $e) { |
||
| 133 | // プラグインで設定画面のルートが定義されていない場合は無視 |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | $officialPlugins = array(); |
||
| 138 | $unofficialPlugins = array(); |
||
| 139 | |||
| 140 | foreach ($Plugins as $Plugin) { |
||
| 141 | $form = $this->formFactory |
||
| 142 | ->createNamedBuilder( |
||
| 143 | 'form'.$Plugin->getId(), |
||
| 144 | PluginManagementType::class, |
||
| 145 | null, |
||
| 146 | array( |
||
| 147 | 'plugin_id' => $Plugin->getId(), |
||
| 148 | ) |
||
| 149 | ) |
||
| 150 | ->getForm(); |
||
| 151 | $pluginForms[$Plugin->getId()] = $form->createView(); |
||
| 152 | |||
| 153 | try { |
||
| 154 | // プラグイン用設定画面があれば表示(プラグイン用のサービスプロバイダーに定義されているか) |
||
| 155 | $configPages[$Plugin->getCode()] = $app->url('plugin_'.$Plugin->getCode().'_config'); |
||
| 156 | } catch (\Exception $e) { |
||
| 157 | // プラグインで設定画面のルートが定義されていない場合は無視 |
||
| 158 | } |
||
| 159 | |||
| 160 | if ($Plugin->getSource() == 0) { |
||
| 161 | // 商品IDが設定されていない場合、非公式プラグイン |
||
| 162 | $unofficialPlugins[] = $Plugin; |
||
| 163 | } else { |
||
| 164 | $officialPlugins[] = $Plugin; |
||
| 165 | } |
||
| 166 | } |
||
| 167 | |||
| 168 | // Todo: Need new authentication mechanism |
||
| 169 | // オーナーズストアからダウンロード可能プラグイン情報を取得 |
||
| 170 | $authKey = $this->BaseInfo->getAuthenticationKey(); |
||
| 171 | // if (!is_null($authKey)) { |
||
| 172 | |||
| 173 | // オーナーズストア通信 |
||
| 174 | $url = $this->appConfig['package_repo_url'].'/search/packages.json'; |
||
| 175 | list($json, $info) = $this->getRequestApi($request, $authKey, $url, $app); |
||
| 176 | |||
| 177 | $officialPluginsDetail = []; |
||
| 178 | if ($json) { |
||
| 179 | // 接続成功時 |
||
| 180 | $data = json_decode($json, true); |
||
| 181 | if (isset($data['success'])) { |
||
| 182 | $success = $data['success']; |
||
| 183 | if ($success == '1') { |
||
| 184 | foreach ($data['item'] as $item) { |
||
| 185 | foreach ($officialPlugins as $key => $plugin) { |
||
| 186 | if ($plugin->getSource() == $item['product_id']) { |
||
| 187 | $officialPluginsDetail[$key] = $item; |
||
| 188 | $officialPluginsDetail[$key]['update_status'] = 0; |
||
| 189 | if ($plugin->getVersion() != $item['version']) { |
||
| 190 | $officialPluginsDetail[$key]['update_status'] = 1; |
||
| 191 | } |
||
| 192 | } |
||
| 193 | } |
||
| 194 | } |
||
| 195 | } |
||
| 196 | } |
||
| 197 | } |
||
| 198 | |||
| 199 | return [ |
||
| 200 | 'plugin_forms' => $pluginForms, |
||
| 201 | 'officialPlugins' => $officialPlugins, |
||
| 202 | 'unofficialPlugins' => $unofficialPlugins, |
||
| 203 | 'configPages' => $configPages, |
||
| 204 | 'unregisterdPlugins' => $unregisterdPlugins, |
||
| 205 | 'unregisterdPluginsConfigPages' => $unregisterdPluginsConfigPages, |
||
| 206 | 'officialPluginsDetail' => $officialPluginsDetail, |
||
| 207 | ]; |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * インストール済プラグインからのアップデート |
||
| 212 | * |
||
| 213 | * @Method("POST") |
||
| 214 | * @Route("/{_admin}/store/plugin/{id}/update", requirements={"id" = "\d+"}, name="admin_store_plugin_update") |
||
| 215 | * @param Application $app |
||
| 216 | * @param Request $request |
||
| 217 | * @param Plugin $Plugin |
||
| 218 | * @return RedirectResponse |
||
| 219 | */ |
||
| 220 | public function update(Application $app, Request $request, Plugin $Plugin) |
||
| 221 | { |
||
| 222 | $form = $this->formFactory |
||
| 223 | ->createNamedBuilder( |
||
| 224 | 'form'.$Plugin->getId(), |
||
| 225 | PluginManagementType::class, |
||
| 226 | null, |
||
| 227 | array( |
||
| 228 | 'plugin_id' => null, // placeHolder |
||
| 229 | ) |
||
| 230 | ) |
||
| 231 | ->getForm(); |
||
| 232 | |||
| 233 | $message = ''; |
||
| 234 | $form->handleRequest($request); |
||
| 235 | if ($form->isSubmitted() && $form->isValid()) { |
||
| 236 | $tmpDir = null; |
||
| 237 | try { |
||
| 238 | $formFile = $form['plugin_archive']->getData(); |
||
| 239 | $tmpDir = $this->pluginService->createTempDir(); |
||
| 240 | $tmpFile = sha1(Str::random(32)).'.'.$formFile->getClientOriginalExtension(); |
||
| 241 | $formFile->move($tmpDir, $tmpFile); |
||
| 242 | $this->pluginService->update($Plugin, $tmpDir.'/'.$tmpFile); |
||
| 243 | $fs = new Filesystem(); |
||
| 244 | $fs->remove($tmpDir); |
||
| 245 | $app->addSuccess('admin.plugin.update.complete', 'admin'); |
||
| 246 | |||
| 247 | return $app->redirect($app->url('admin_store_plugin')); |
||
| 248 | } catch (PluginException $e) { |
||
| 249 | if (!empty($tmpDir) && file_exists($tmpDir)) { |
||
| 250 | $fs = new Filesystem(); |
||
| 251 | $fs->remove($tmpDir); |
||
| 252 | } |
||
| 253 | $message = $e->getMessage(); |
||
| 254 | } |
||
| 255 | } else { |
||
| 256 | $errors = $form->getErrors(true); |
||
| 257 | foreach ($errors as $error) { |
||
| 258 | $message = $error->getMessage(); |
||
| 259 | } |
||
| 260 | } |
||
| 261 | |||
| 262 | $app->addError($message, 'admin'); |
||
| 263 | |||
| 264 | return $app->redirect($app->url('admin_store_plugin')); |
||
| 265 | } |
||
| 266 | |||
| 267 | |||
| 268 | /** |
||
| 269 | * 対象のプラグインを有効にします。 |
||
| 270 | * |
||
| 271 | * @Method("PUT") |
||
| 272 | * @Route("/{_admin}/store/plugin/{id}/enable", requirements={"id" = "\d+"}, name="admin_store_plugin_enable") |
||
| 273 | * @param Application $app |
||
| 274 | * @param Plugin $Plugin |
||
| 275 | * @return RedirectResponse |
||
| 276 | */ |
||
| 277 | View Code Duplication | public function enable(Application $app, Plugin $Plugin) |
|
| 278 | { |
||
| 279 | $this->isTokenValid($app); |
||
| 280 | |||
| 281 | if ($Plugin->isEnable()) { |
||
| 282 | $app->addError('admin.plugin.already.enable', 'admin'); |
||
| 283 | } else { |
||
| 284 | $requires = $this->pluginService->findRequirePluginNeedEnable($Plugin->getCode()); |
||
| 285 | if (!empty($requires)) { |
||
| 286 | $DependPlugin = $this->pluginRepository->findOneBy(['code' => $requires[0]]); |
||
| 287 | $dependName = $requires[0]; |
||
| 288 | if ($DependPlugin) { |
||
| 289 | $dependName = $DependPlugin->getName(); |
||
| 290 | } |
||
| 291 | $message = $app->trans('admin.plugin.enable.depend', ['%name%' => $Plugin->getName(), '%depend_name%' => $dependName]); |
||
| 292 | $app->addError($message, 'admin'); |
||
| 293 | |||
| 294 | return $app->redirect($app->url('admin_store_plugin')); |
||
| 295 | } |
||
| 296 | $this->pluginService->enable($Plugin); |
||
| 297 | $app->addSuccess('admin.plugin.enable.complete', 'admin'); |
||
| 298 | } |
||
| 299 | |||
| 300 | return $app->redirect($app->url('admin_store_plugin')); |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * 対象のプラグインを無効にします。 |
||
| 305 | * |
||
| 306 | * @Method("PUT") |
||
| 307 | * @Route("/{_admin}/store/plugin/{id}/disable", requirements={"id" = "\d+"}, name="admin_store_plugin_disable") |
||
| 308 | * @param Application $app |
||
| 309 | * @param Plugin $Plugin |
||
| 310 | * @return RedirectResponse |
||
| 311 | */ |
||
| 312 | View Code Duplication | public function disable(Application $app, Plugin $Plugin) |
|
| 313 | { |
||
| 314 | $this->isTokenValid($app); |
||
| 315 | |||
| 316 | if ($Plugin->isEnable()) { |
||
| 317 | $dependents = $this->pluginService->findDependentPluginNeedDisable($Plugin->getCode()); |
||
| 318 | if (!empty($dependents)) { |
||
| 319 | $dependName = $dependents[0]; |
||
| 320 | $DependPlugin = $this->pluginRepository->findOneBy(['code' => $dependents[0]]); |
||
| 321 | if ($DependPlugin) { |
||
| 322 | $dependName = $DependPlugin->getName(); |
||
| 323 | } |
||
| 324 | $message = $app->trans('admin.plugin.disable.depend', ['%name%' => $Plugin->getName(), '%depend_name%' => $dependName]); |
||
| 325 | $app->addError($message, 'admin'); |
||
| 326 | |||
| 327 | return $app->redirect($app->url('admin_store_plugin')); |
||
| 328 | } |
||
| 329 | |||
| 330 | $this->pluginService->disable($Plugin); |
||
| 331 | $app->addSuccess('admin.plugin.disable.complete', 'admin'); |
||
| 332 | } else { |
||
| 333 | $app->addError('admin.plugin.already.disable', 'admin'); |
||
| 334 | } |
||
| 335 | |||
| 336 | return $app->redirect($app->url('admin_store_plugin')); |
||
| 337 | } |
||
| 338 | |||
| 339 | /** |
||
| 340 | * 対象のプラグインを削除します。 |
||
| 341 | * |
||
| 342 | * @Method("DELETE") |
||
| 343 | * @Route("/{_admin}/store/plugin/{id}/uninstall", requirements={"id" = "\d+"}, name="admin_store_plugin_uninstall") |
||
| 344 | * @param Application $app |
||
| 345 | * @param Plugin $Plugin |
||
| 346 | * @return RedirectResponse |
||
| 347 | */ |
||
| 348 | public function uninstall(Application $app, Plugin $Plugin) |
||
| 349 | { |
||
| 350 | $this->isTokenValid($app); |
||
| 351 | // Check other plugin depend on it |
||
| 352 | $pluginCode = $Plugin->getCode(); |
||
| 353 | $otherDepend = $this->pluginService->findDependentPlugin($pluginCode); |
||
| 354 | if (!empty($otherDepend)) { |
||
| 355 | $DependPlugin = $this->pluginRepository->findOneBy(['code' => $otherDepend[0]]); |
||
| 356 | $dependName = $otherDepend[0]; |
||
| 357 | if ($DependPlugin) { |
||
| 358 | $dependName = $DependPlugin->getName(); |
||
| 359 | } |
||
| 360 | $message = $app->trans('admin.plugin.uninstall.depend', ['%name%' => $Plugin->getName(), '%depend_name%' => $dependName]); |
||
| 361 | $app->addError($message, 'admin'); |
||
| 362 | |||
| 363 | return $app->redirect($app->url('admin_store_plugin')); |
||
| 364 | } |
||
| 365 | |||
| 366 | $this->pluginService->uninstall($Plugin); |
||
| 367 | $app->addSuccess('admin.plugin.uninstall.complete', 'admin'); |
||
| 368 | |||
| 369 | return $app->redirect($app->url('admin_store_plugin')); |
||
| 370 | } |
||
| 371 | |||
| 372 | /** |
||
| 373 | * @Route("/{_admin}/store/plugin/handler", name="admin_store_plugin_handler") |
||
| 374 | * @Template("Store/plugin_handler.twig") |
||
| 375 | */ |
||
| 376 | public function handler(Application $app) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @Route("/{_admin}/store/plugin/handler_up/{id}", requirements={"id" = "\d+"}, name="admin_store_plugin_handler_up") |
||
| 393 | */ |
||
| 394 | View Code Duplication | public function handler_up(Application $app, PluginEventHandler $Handler) |
|
| 401 | |||
| 402 | /** |
||
| 403 | * @Route("/{_admin}/store/plugin/handler_down/{id}", requirements={"id" = "\d+"}, name="admin_store_plugin_handler_down") |
||
| 404 | */ |
||
| 405 | View Code Duplication | public function handler_down(Application $app, PluginEventHandler $Handler) |
|
| 412 | |||
| 413 | /** |
||
| 414 | * プラグインファイルアップロード画面 |
||
| 415 | * |
||
| 416 | * @Route("/{_admin}/store/plugin/install", name="admin_store_plugin_install") |
||
| 417 | * @Template("Store/plugin_install.twig") |
||
| 418 | * @param Application $app |
||
| 419 | * @param Request $request |
||
| 420 | * @return array|RedirectResponse |
||
| 421 | */ |
||
| 422 | public function install(Application $app, Request $request) |
||
| 466 | |||
| 467 | /** |
||
| 468 | * オーナーズストアプラグインインストール画面 |
||
| 469 | * |
||
| 470 | * @Route("/{_admin}/store/plugin/owners_install", name="admin_store_plugin_owners_install") |
||
| 471 | * @Template("Store/plugin_owners_install.twig") |
||
| 472 | */ |
||
| 473 | public function ownersInstall(Application $app, Request $request) |
||
| 579 | |||
| 580 | /** |
||
| 581 | * オーナーズブラグインインストール、アップデート |
||
| 582 | * |
||
| 583 | * @Route("/{_admin}/store/plugin/upgrade/{action}/{id}/{version}", requirements={"id" = "\d+"}, name="admin_store_plugin_upgrade") |
||
| 584 | * @param Application $app |
||
| 585 | * @param Request $request |
||
| 586 | * @param string $action |
||
| 587 | * @param int $id |
||
| 588 | * @param string $version |
||
| 589 | * @return RedirectResponse |
||
| 590 | */ |
||
| 591 | public function upgrade(Application $app, Request $request, $action, $id, $version) |
||
| 667 | |||
| 668 | /** |
||
| 669 | * 認証キー設定画面 |
||
| 670 | * |
||
| 671 | * @Route("/{_admin}/store/plugin/authentication_setting", name="admin_store_authentication_setting") |
||
| 672 | * @Template("Store/authentication_setting.twig") |
||
| 673 | */ |
||
| 674 | public function authenticationSetting(Application $app, Request $request) |
||
| 704 | |||
| 705 | |||
| 706 | /** |
||
| 707 | * APIリクエスト処理 |
||
| 708 | * |
||
| 709 | * @param Request $request |
||
| 710 | * @param $authKey |
||
| 711 | * @param string $url |
||
| 712 | * @param Application $app |
||
| 713 | * @return array |
||
| 714 | */ |
||
| 715 | private function getRequestApi(Request $request, $authKey, $url, $app) |
||
| 745 | |||
| 746 | /** |
||
| 747 | * レスポンスのチェック |
||
| 748 | * |
||
| 749 | * @param $info |
||
| 750 | * @return string |
||
| 751 | */ |
||
| 752 | View Code Duplication | private function getResponseErrorMessage($info) |
|
| 766 | |||
| 767 | |||
| 768 | /** |
||
| 769 | * フォルダ設置のみのプラグインを取得する. |
||
| 770 | * |
||
| 771 | * @param array $plugins |
||
| 772 | * @param Application $app |
||
| 773 | * @return array |
||
| 774 | */ |
||
| 775 | protected function getUnregisteredPlugins(array $plugins, \Eccube\Application $app) |
||
| 811 | } |
||
| 812 |