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 |
||
| 40 | class PluginController extends AbstractController |
||
|
|
|||
| 41 | { |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string 証明書ファイル |
||
| 45 | */ |
||
| 46 | private $certFileName = 'cacert.pem'; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * インストール済プラグイン画面 |
||
| 50 | * |
||
| 51 | * @param Application $app |
||
| 52 | * @param Request $request |
||
| 53 | */ |
||
| 54 | public function index(Application $app, Request $request) |
||
| 55 | { |
||
| 56 | |||
| 57 | $pluginForms = array(); |
||
| 58 | $configPages = array(); |
||
| 59 | |||
| 60 | $Plugins = $app['eccube.repository.plugin']->findBy(array(), array('name' => 'ASC')); |
||
| 61 | |||
| 62 | // ファイル設置プラグインの取得. |
||
| 63 | $unregisterdPlugins = $this->getUnregisteredPlugins($Plugins, $app); |
||
| 64 | $unregisterdPluginsConfigPages = array(); |
||
| 65 | foreach ($unregisterdPlugins as $unregisterdPlugin) { |
||
| 66 | try { |
||
| 67 | $code = $unregisterdPlugin['code']; |
||
| 68 | // プラグイン用設定画面があれば表示(プラグイン用のサービスプロバイダーに定義されているか) |
||
| 69 | $unregisterdPluginsConfigPages[$code] = $app->url('plugin_'.$code.'_config'); |
||
| 70 | } catch (RouteNotFoundException $e) { |
||
| 71 | // プラグインで設定画面のルートが定義されていない場合は無視 |
||
| 72 | } |
||
| 73 | } |
||
| 74 | |||
| 75 | $officialPlugins = array(); |
||
| 76 | $unofficialPlugins = array(); |
||
| 77 | |||
| 78 | foreach ($Plugins as $Plugin) { |
||
| 79 | |||
| 80 | $form = $app['form.factory'] |
||
| 81 | ->createNamedBuilder('form'.$Plugin->getId(), 'plugin_management', null, array( |
||
| 82 | 'plugin_id' => $Plugin->getId(), |
||
| 83 | )) |
||
| 84 | ->getForm(); |
||
| 85 | |||
| 86 | $pluginForms[$Plugin->getId()] = $form->createView(); |
||
| 87 | |||
| 88 | try { |
||
| 89 | // プラグイン用設定画面があれば表示(プラグイン用のサービスプロバイダーに定義されているか) |
||
| 90 | $configPages[$Plugin->getCode()] = $app->url('plugin_'.$Plugin->getCode().'_config'); |
||
| 91 | } catch (\Exception $e) { |
||
| 92 | // プラグインで設定画面のルートが定義されていない場合は無視 |
||
| 93 | } |
||
| 94 | |||
| 95 | if ($Plugin->getSource() == 0) { |
||
| 96 | // 商品IDが設定されていない場合、非公式プラグイン |
||
| 97 | $unofficialPlugins[] = $Plugin; |
||
| 98 | } else { |
||
| 99 | $officialPlugins[] = $Plugin; |
||
| 100 | } |
||
| 101 | |||
| 102 | } |
||
| 103 | |||
| 104 | // オーナーズストアからダウンロード可能プラグイン情報を取得 |
||
| 105 | $BaseInfo = $app['eccube.repository.base_info']->get(); |
||
| 106 | |||
| 107 | $authKey = $BaseInfo->getAuthenticationKey(); |
||
| 108 | |||
| 109 | if (!is_null($authKey)) { |
||
| 110 | |||
| 111 | // オーナーズストア通信 |
||
| 112 | $url = $app['config']['owners_store_url'].'?method=list'; |
||
| 113 | list($json, $info) = $this->getRequestApi($request, $authKey, $url, $app); |
||
| 114 | |||
| 115 | if ($json) { |
||
| 116 | |||
| 117 | // 接続成功時 |
||
| 118 | |||
| 119 | $data = json_decode($json, true); |
||
| 120 | |||
| 121 | if (isset($data['success'])) { |
||
| 122 | $success = $data['success']; |
||
| 123 | if ($success == '1') { |
||
| 124 | |||
| 125 | // 既にインストールされているかどうか確認 |
||
| 126 | foreach ($data['item'] as $item) { |
||
| 127 | foreach ($officialPlugins as $plugin) { |
||
| 128 | if ($plugin->getSource() == $item['product_id']) { |
||
| 129 | // 商品IDが同一の情報を設定 |
||
| 130 | $plugin->setNewVersion($item['version']); |
||
| 131 | $plugin->setLastUpdateDate($item['last_update_date']); |
||
| 132 | $plugin->setProductUrl($item['product_url']); |
||
| 133 | |||
| 134 | if ($plugin->getVersion() != $item['version']) { |
||
| 135 | // バージョンが異なる |
||
| 136 | if (in_array(Constant::VERSION, $item['eccube_version'])) { |
||
| 137 | // 対象バージョン |
||
| 138 | $plugin->setUpdateStatus(3); |
||
| 139 | break; |
||
| 140 | } |
||
| 141 | } |
||
| 142 | } |
||
| 143 | } |
||
| 144 | } |
||
| 145 | } |
||
| 146 | } |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | |||
| 151 | return $app->render('Store/plugin.twig', array( |
||
| 152 | 'plugin_forms' => $pluginForms, |
||
| 153 | 'officialPlugins' => $officialPlugins, |
||
| 154 | 'unofficialPlugins' => $unofficialPlugins, |
||
| 155 | 'configPages' => $configPages, |
||
| 156 | 'unregisterdPlugins' => $unregisterdPlugins, |
||
| 157 | 'unregisterdPluginsConfigPages' => $unregisterdPluginsConfigPages, |
||
| 158 | )); |
||
| 159 | |||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * インストール済プラグインからのアップデート |
||
| 164 | * |
||
| 165 | * @param Application $app |
||
| 166 | * @param Request $request |
||
| 167 | * @param $id |
||
| 168 | */ |
||
| 169 | public function update(Application $app, Request $request, $id) |
||
| 228 | |||
| 229 | |||
| 230 | /** |
||
| 231 | * 対象のプラグインを有効にします。 |
||
| 232 | * |
||
| 233 | * @param Application $app |
||
| 234 | * @param $id |
||
| 235 | */ |
||
| 236 | View Code Duplication | public function enable(Application $app, $id) |
|
| 255 | |||
| 256 | /** |
||
| 257 | * 対象のプラグインを無効にします。 |
||
| 258 | * |
||
| 259 | * @param Application $app |
||
| 260 | * @param $id |
||
| 261 | */ |
||
| 262 | View Code Duplication | public function disable(Application $app, $id) |
|
| 281 | |||
| 282 | |||
| 283 | /** |
||
| 284 | * 対象のプラグインを削除します。 |
||
| 285 | * |
||
| 286 | * @param Application $app |
||
| 287 | * @param $id |
||
| 288 | */ |
||
| 289 | public function uninstall(Application $app, $id) |
||
| 290 | { |
||
| 291 | $this->isTokenValid($app); |
||
| 292 | |||
| 293 | $Plugin = $app['eccube.repository.plugin']->find($id); |
||
| 294 | |||
| 295 | if (!$Plugin) { |
||
| 296 | $app->deleteMessage(); |
||
| 297 | return $app->redirect($app->url('admin_store_plugin')); |
||
| 298 | } |
||
| 299 | |||
| 300 | $app['eccube.service.plugin']->uninstall($Plugin); |
||
| 301 | |||
| 302 | $app->addSuccess('admin.plugin.uninstall.complete', 'admin'); |
||
| 303 | |||
| 304 | return $app->redirect($app->url('admin_store_plugin')); |
||
| 305 | } |
||
| 306 | |||
| 307 | public function handler(Application $app) |
||
| 322 | |||
| 323 | View Code Duplication | public function handler_up(Application $app, $handlerId) |
|
| 330 | |||
| 331 | View Code Duplication | public function handler_down(Application $app, $handlerId) |
|
| 338 | |||
| 339 | /** |
||
| 340 | * プラグインファイルアップロード画面 |
||
| 341 | * |
||
| 342 | * @param Application $app |
||
| 343 | * @param Request $request |
||
| 344 | */ |
||
| 345 | public function install(Application $app, Request $request) |
||
| 346 | { |
||
| 347 | $form = $app['form.factory'] |
||
| 348 | ->createBuilder('plugin_local_install') |
||
| 349 | ->getForm(); |
||
| 350 | |||
| 351 | $errors = array(); |
||
| 352 | |||
| 353 | if ('POST' === $request->getMethod()) { |
||
| 354 | $form->handleRequest($request); |
||
| 355 | |||
| 356 | if ($form->isValid()) { |
||
| 357 | |||
| 358 | $tmpDir = null; |
||
| 359 | try { |
||
| 360 | $service = $app['eccube.service.plugin']; |
||
| 361 | |||
| 362 | $formFile = $form['plugin_archive']->getData(); |
||
| 363 | |||
| 364 | $tmpDir = $service->createTempDir(); |
||
| 365 | $tmpFile = sha1(Str::random(32)).'.'.$formFile->getClientOriginalExtension(); // 拡張子を付けないとpharが動かないので付ける |
||
| 366 | |||
| 367 | $formFile->move($tmpDir, $tmpFile); |
||
| 368 | |||
| 369 | $service->install($tmpDir.'/'.$tmpFile); |
||
| 370 | |||
| 371 | $fs = new Filesystem(); |
||
| 372 | $fs->remove($tmpDir); |
||
| 373 | |||
| 374 | $app->addSuccess('admin.plugin.install.complete', 'admin'); |
||
| 375 | |||
| 376 | return $app->redirect($app->url('admin_store_plugin')); |
||
| 377 | |||
| 378 | } catch (PluginException $e) { |
||
| 379 | if (!empty($tmpDir) && file_exists($tmpDir)) { |
||
| 380 | $fs = new Filesystem(); |
||
| 381 | $fs->remove($tmpDir); |
||
| 382 | } |
||
| 383 | $app['monolog']->error("plugin install failed.", array( |
||
| 384 | 'original-message' => $e->getMessage() |
||
| 385 | )); |
||
| 386 | $errors[] = $e; |
||
| 387 | } |
||
| 388 | } |
||
| 389 | } |
||
| 390 | |||
| 391 | return $app->render('Store/plugin_install.twig', array( |
||
| 392 | 'form' => $form->createView(), |
||
| 393 | 'errors' => $errors, |
||
| 394 | )); |
||
| 395 | |||
| 396 | } |
||
| 397 | |||
| 398 | /** |
||
| 399 | * オーナーズストアプラグインインストール画面 |
||
| 400 | * |
||
| 401 | * @param Application $app |
||
| 402 | * @param Request $request |
||
| 403 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 404 | */ |
||
| 405 | public function ownersInstall(Application $app, Request $request) |
||
| 406 | { |
||
| 407 | // オーナーズストアからダウンロード可能プラグイン情報を取得 |
||
| 408 | $BaseInfo = $app['eccube.repository.base_info']->get(); |
||
| 409 | |||
| 410 | $authKey = $BaseInfo->getAuthenticationKey(); |
||
| 411 | $authResult = true; |
||
| 412 | $success = 0; |
||
| 413 | $items = array(); |
||
| 414 | $promotionItems = array(); |
||
| 415 | $message = ''; |
||
| 416 | if (!is_null($authKey)) { |
||
| 417 | |||
| 418 | // オーナーズストア通信 |
||
| 419 | $url = $app['config']['owners_store_url'].'?method=list'; |
||
| 420 | list($json, $info) = $this->getRequestApi($request, $authKey, $url, $app); |
||
| 421 | |||
| 422 | if ($json === false) { |
||
| 423 | // 接続失敗時 |
||
| 424 | $success = 0; |
||
| 425 | |||
| 426 | $message = $this->getResponseErrorMessage($info); |
||
| 427 | |||
| 428 | } else { |
||
| 429 | // 接続成功時 |
||
| 430 | |||
| 431 | $data = json_decode($json, true); |
||
| 432 | |||
| 433 | if (isset($data['success'])) { |
||
| 434 | $success = $data['success']; |
||
| 435 | if ($success == '1') { |
||
| 436 | $items = array(); |
||
| 437 | |||
| 438 | // 既にインストールされているかどうか確認 |
||
| 439 | $Plugins = $app['eccube.repository.plugin']->findAll(); |
||
| 440 | $status = false; |
||
| 441 | // update_status 1 : 未インストール、2 : インストール済、 3 : 更新あり、4 : 有料購入 |
||
| 442 | foreach ($data['item'] as $item) { |
||
| 443 | foreach ($Plugins as $plugin) { |
||
| 444 | if ($plugin->getSource() == $item['product_id']) { |
||
| 445 | if ($plugin->getVersion() == $item['version']) { |
||
| 446 | // バージョンが同じ |
||
| 447 | $item['update_status'] = 2; |
||
| 448 | } else { |
||
| 449 | // バージョンが異なる |
||
| 450 | $item['update_status'] = 3; |
||
| 451 | } |
||
| 452 | $items[] = $item; |
||
| 453 | $status = true; |
||
| 454 | break; |
||
| 455 | } |
||
| 456 | } |
||
| 457 | if (!$status) { |
||
| 458 | // 未インストール |
||
| 459 | $item['update_status'] = 1; |
||
| 460 | $items[] = $item; |
||
| 461 | } |
||
| 462 | $status = false; |
||
| 463 | } |
||
| 464 | |||
| 465 | // EC-CUBEのバージョンチェック |
||
| 466 | // 参照渡しをして値を追加 |
||
| 467 | foreach ($items as &$item) { |
||
| 468 | if (in_array(Constant::VERSION, $item['eccube_version'])) { |
||
| 469 | // 対象バージョン |
||
| 470 | $item['version_check'] = 1; |
||
| 471 | } else { |
||
| 472 | // 未対象バージョン |
||
| 473 | $item['version_check'] = 0; |
||
| 474 | } |
||
| 475 | if ($item['price'] != '0' && $item['purchased'] == '0') { |
||
| 476 | // 有料商品で未購入 |
||
| 477 | $item['update_status'] = 4; |
||
| 478 | } |
||
| 479 | } |
||
| 480 | unset($item); |
||
| 481 | |||
| 482 | // promotionアイテム |
||
| 483 | $i = 0; |
||
| 484 | foreach ($items as $item) { |
||
| 485 | if ($item['promotion'] == 1) { |
||
| 486 | $promotionItems[] = $item; |
||
| 487 | unset($items[$i]); |
||
| 488 | } |
||
| 489 | $i++; |
||
| 490 | } |
||
| 491 | |||
| 492 | } else { |
||
| 493 | $message = $data['error_code'].' : '.$data['error_message']; |
||
| 494 | } |
||
| 495 | } else { |
||
| 496 | $success = 0; |
||
| 497 | $message = "EC-CUBEオーナーズストアにエラーが発生しています。"; |
||
| 498 | } |
||
| 499 | } |
||
| 500 | |||
| 501 | } else { |
||
| 502 | $authResult = false; |
||
| 503 | } |
||
| 504 | |||
| 505 | return $app->render('Store/plugin_owners_install.twig', array( |
||
| 506 | 'authResult' => $authResult, |
||
| 507 | 'success' => $success, |
||
| 508 | 'items' => $items, |
||
| 509 | 'promotionItems' => $promotionItems, |
||
| 510 | 'message' => $message, |
||
| 511 | )); |
||
| 512 | |||
| 513 | } |
||
| 514 | |||
| 515 | /** |
||
| 516 | * オーナーズブラグインインストール、アップデート |
||
| 517 | * |
||
| 518 | * @param Application $app |
||
| 519 | * @param Request $request |
||
| 520 | * @param $action |
||
| 521 | * @param $id |
||
| 522 | * @param $version |
||
| 523 | */ |
||
| 524 | public function upgrade(Application $app, Request $request, $action, $id, $version) |
||
| 525 | { |
||
| 526 | |||
| 527 | $BaseInfo = $app['eccube.repository.base_info']->get(); |
||
| 528 | |||
| 529 | $authKey = $BaseInfo->getAuthenticationKey(); |
||
| 530 | $message = ''; |
||
| 531 | |||
| 532 | if (!is_null($authKey)) { |
||
| 533 | |||
| 534 | // オーナーズストア通信 |
||
| 535 | $url = $app['config']['owners_store_url'].'?method=download&product_id='.$id; |
||
| 536 | list($json, $info) = $this->getRequestApi($request, $authKey, $url, $app); |
||
| 537 | |||
| 538 | if ($json === false) { |
||
| 539 | // 接続失敗時 |
||
| 540 | |||
| 541 | $message = $this->getResponseErrorMessage($info); |
||
| 542 | |||
| 543 | } else { |
||
| 544 | // 接続成功時 |
||
| 545 | |||
| 546 | $data = json_decode($json, true); |
||
| 547 | |||
| 548 | if (isset($data['success'])) { |
||
| 549 | $success = $data['success']; |
||
| 550 | if ($success == '1') { |
||
| 551 | $tmpDir = null; |
||
| 552 | try { |
||
| 553 | $service = $app['eccube.service.plugin']; |
||
| 554 | |||
| 555 | $item = $data['item']; |
||
| 556 | $file = base64_decode($item['data']); |
||
| 557 | $extension = pathinfo($item['file_name'], PATHINFO_EXTENSION); |
||
| 558 | |||
| 559 | $tmpDir = $service->createTempDir(); |
||
| 560 | $tmpFile = sha1(Str::random(32)).'.'.$extension; |
||
| 561 | |||
| 562 | // ファイル作成 |
||
| 563 | $fs = new Filesystem(); |
||
| 564 | $fs->dumpFile($tmpDir.'/'.$tmpFile, $file); |
||
| 565 | |||
| 566 | if ($action == 'install') { |
||
| 567 | |||
| 568 | $service->install($tmpDir.'/'.$tmpFile, $id); |
||
| 569 | $app->addSuccess('admin.plugin.install.complete', 'admin'); |
||
| 570 | |||
| 571 | } else if ($action == 'update') { |
||
| 572 | |||
| 573 | $Plugin = $app['eccube.repository.plugin']->findOneBy(array('source' => $id)); |
||
| 574 | |||
| 575 | $service->update($Plugin, $tmpDir.'/'.$tmpFile); |
||
| 576 | $app->addSuccess('admin.plugin.update.complete', 'admin'); |
||
| 577 | |||
| 578 | Cache::clear($app, false); |
||
| 579 | |||
| 580 | } |
||
| 581 | |||
| 582 | $fs = new Filesystem(); |
||
| 583 | $fs->remove($tmpDir); |
||
| 584 | |||
| 585 | // ダウンロード完了通知処理(正常終了時) |
||
| 586 | $url = $app['config']['owners_store_url'].'?method=commit&product_id='.$id.'&status=1&version='.$version; |
||
| 587 | $this->getRequestApi($request, $authKey, $url, $app); |
||
| 588 | |||
| 589 | return $app->redirect($app->url('admin_store_plugin')); |
||
| 590 | |||
| 591 | } catch (PluginException $e) { |
||
| 592 | if (!empty($tmpDir) && file_exists($tmpDir)) { |
||
| 593 | $fs = new Filesystem(); |
||
| 594 | $fs->remove($tmpDir); |
||
| 595 | } |
||
| 596 | $message = $e->getMessage(); |
||
| 597 | } |
||
| 598 | |||
| 599 | } else { |
||
| 600 | $message = $data['error_code'].' : '.$data['error_message']; |
||
| 601 | } |
||
| 602 | } else { |
||
| 603 | $message = "EC-CUBEオーナーズストアにエラーが発生しています。"; |
||
| 604 | } |
||
| 605 | } |
||
| 606 | } |
||
| 607 | |||
| 608 | // ダウンロード完了通知処理(エラー発生時) |
||
| 609 | $url = $app['config']['owners_store_url'].'?method=commit&product_id='.$id.'&status=0&version='.$version.'&message='.urlencode($message); |
||
| 610 | $this->getRequestApi($request, $authKey, $url, $app); |
||
| 611 | |||
| 612 | $app->addError($message, 'admin'); |
||
| 613 | |||
| 614 | return $app->redirect($app->url('admin_store_plugin_owners_install')); |
||
| 615 | } |
||
| 616 | |||
| 617 | /** |
||
| 618 | * 認証キー設定画面 |
||
| 619 | * |
||
| 620 | * @param Application $app |
||
| 621 | * @param Request $request |
||
| 622 | */ |
||
| 623 | public function authenticationSetting(Application $app, Request $request) |
||
| 663 | |||
| 664 | |||
| 665 | /** |
||
| 666 | * 認証キーダウンロード |
||
| 667 | * |
||
| 668 | * @param Application $app |
||
| 669 | * @param Request $request |
||
| 670 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
||
| 671 | */ |
||
| 672 | public function download(Application $app, Request $request) |
||
| 700 | |||
| 701 | |||
| 702 | /** |
||
| 703 | * APIリクエスト処理 |
||
| 704 | * |
||
| 705 | * @param Request $request |
||
| 706 | * @param $authKey |
||
| 707 | * @param string $url |
||
| 708 | * @param Application $app |
||
| 709 | * @return array |
||
| 710 | */ |
||
| 711 | private function getRequestApi(Request $request, $authKey, $url, $app) |
||
| 748 | |||
| 749 | /** |
||
| 750 | * レスポンスのチェック |
||
| 751 | * |
||
| 752 | * @param $info |
||
| 753 | * @return string |
||
| 754 | */ |
||
| 755 | private function getResponseErrorMessage($info) |
||
| 769 | |||
| 770 | |||
| 771 | /** |
||
| 772 | * フォルダ設置のみのプラグインを取得する. |
||
| 773 | * |
||
| 774 | * @param array $plugins |
||
| 775 | * @param Application $app |
||
| 776 | * @return array |
||
| 777 | */ |
||
| 778 | protected function getUnregisteredPlugins(array $plugins, \Eccube\Application $app) |
||
| 814 | } |
||
| 815 |