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 | * インストール済プラグイン画面 |
||
| 45 | * |
||
| 46 | * @param Application $app |
||
| 47 | * @param Request $request |
||
| 48 | */ |
||
| 49 | public function index(Application $app, Request $request) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * インストール済プラグインからのアップデート |
||
| 157 | * |
||
| 158 | * @param Application $app |
||
| 159 | * @param Request $request |
||
| 160 | * @param $id |
||
| 161 | */ |
||
| 162 | public function update(Application $app, Request $request, $id) |
||
| 163 | { |
||
| 164 | |||
| 165 | $Plugin = $app['eccube.repository.plugin']->find($id); |
||
| 166 | |||
| 167 | $form = $app['form.factory'] |
||
| 168 | ->createNamedBuilder('form'.$id, 'plugin_management', null, array( |
||
| 169 | 'plugin_id' => null, // placeHolder |
||
| 170 | )) |
||
| 171 | ->getForm(); |
||
| 172 | |||
| 173 | $message = ''; |
||
| 174 | |||
| 175 | if ('POST' === $request->getMethod()) { |
||
| 176 | $form->handleRequest($request); |
||
| 177 | |||
| 178 | if ($form->isValid()) { |
||
| 179 | |||
| 180 | $tmpDir = null; |
||
| 181 | try { |
||
| 182 | |||
| 183 | $formFile = $form['plugin_archive']->getData(); |
||
| 184 | |||
| 185 | $tmpDir = $app['eccube.service.plugin']->createTempDir(); |
||
| 186 | $tmpFile = sha1(Str::random(32)).'.'.$formFile->getClientOriginalExtension(); |
||
| 187 | |||
| 188 | $formFile->move($tmpDir, $tmpFile); |
||
| 189 | $app['eccube.service.plugin']->update($Plugin, $tmpDir.'/'.$tmpFile); |
||
| 190 | |||
| 191 | $fs = new Filesystem(); |
||
| 192 | $fs->remove($tmpDir); |
||
| 193 | |||
| 194 | $app->addSuccess('admin.plugin.update.complete', 'admin'); |
||
| 195 | |||
| 196 | return $app->redirect($app->url('admin_store_plugin')); |
||
| 197 | |||
| 198 | } catch (PluginException $e) { |
||
| 199 | if (!empty($tmpDir) && file_exists($tmpDir)) { |
||
| 200 | $fs = new Filesystem(); |
||
| 201 | $fs->remove($tmpDir); |
||
| 202 | } |
||
| 203 | $message = $e->getMessage(); |
||
| 204 | } |
||
| 205 | } else { |
||
| 206 | $errors = $form->getErrors(true); |
||
| 207 | foreach ($errors as $error) { |
||
| 208 | $message = $error->getMessage(); |
||
| 209 | } |
||
| 210 | |||
| 211 | } |
||
| 212 | |||
| 213 | } |
||
| 214 | |||
| 215 | $app->addError($message, 'admin'); |
||
| 216 | |||
| 217 | return $app->redirect($app->url('admin_store_plugin')); |
||
| 218 | } |
||
| 219 | |||
| 220 | |||
| 221 | /** |
||
| 222 | * 対象のプラグインを有効にします。 |
||
| 223 | * |
||
| 224 | * @param Application $app |
||
| 225 | * @param $id |
||
| 226 | */ |
||
| 227 | View Code Duplication | public function enable(Application $app, $id) |
|
| 228 | { |
||
| 229 | $this->isTokenValid($app); |
||
| 230 | |||
| 231 | $Plugin = $app['eccube.repository.plugin']->find($id); |
||
| 232 | |||
| 233 | if (!$Plugin) { |
||
| 234 | throw new NotFoundHttpException(); |
||
| 235 | } |
||
| 236 | |||
| 237 | if ($Plugin->getEnable() == Constant::ENABLED) { |
||
| 238 | $app->addError('admin.plugin.already.enable', 'admin'); |
||
| 239 | } else { |
||
| 240 | $app['eccube.service.plugin']->enable($Plugin); |
||
| 241 | $app->addSuccess('admin.plugin.enable.complete', 'admin'); |
||
| 242 | } |
||
| 243 | |||
| 244 | return $app->redirect($app->url('admin_store_plugin')); |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * 対象のプラグインを無効にします。 |
||
| 249 | * |
||
| 250 | * @param Application $app |
||
| 251 | * @param $id |
||
| 252 | */ |
||
| 253 | View Code Duplication | public function disable(Application $app, $id) |
|
| 254 | { |
||
| 255 | $this->isTokenValid($app); |
||
| 256 | |||
| 257 | $Plugin = $app['eccube.repository.plugin']->find($id); |
||
| 258 | |||
| 259 | if (!$Plugin) { |
||
| 260 | throw new NotFoundHttpException(); |
||
| 261 | } |
||
| 262 | |||
| 263 | if ($Plugin->getEnable() == Constant::ENABLED) { |
||
| 264 | $app['eccube.service.plugin']->disable($Plugin); |
||
| 265 | $app->addSuccess('admin.plugin.disable.complete', 'admin'); |
||
| 266 | } else { |
||
| 267 | $app->addError('admin.plugin.already.disable', 'admin'); |
||
| 268 | } |
||
| 269 | |||
| 270 | return $app->redirect($app->url('admin_store_plugin')); |
||
| 271 | } |
||
| 272 | |||
| 273 | |||
| 274 | /** |
||
| 275 | * 対象のプラグインを削除します。 |
||
| 276 | * |
||
| 277 | * @param Application $app |
||
| 278 | * @param $id |
||
| 279 | */ |
||
| 280 | public function uninstall(Application $app, $id) |
||
| 281 | { |
||
| 282 | $this->isTokenValid($app); |
||
| 283 | |||
| 284 | $Plugin = $app['eccube.repository.plugin']->find($id); |
||
| 285 | |||
| 286 | if (!$Plugin) { |
||
| 287 | $app->deleteMessage(); |
||
| 288 | return $app->redirect($app->url('admin_store_plugin')); |
||
| 289 | } |
||
| 290 | |||
| 291 | $app['eccube.service.plugin']->uninstall($Plugin); |
||
| 292 | |||
| 293 | $app->addSuccess('admin.plugin.uninstall.complete', 'admin'); |
||
| 294 | |||
| 295 | return $app->redirect($app->url('admin_store_plugin')); |
||
| 296 | } |
||
| 297 | |||
| 298 | public function handler(Application $app) |
||
| 299 | { |
||
| 300 | $handlers = $app['eccube.repository.plugin_event_handler']->getHandlers(); |
||
| 301 | |||
| 302 | // 一次元配列からイベント毎の二次元配列に変換する |
||
| 303 | $HandlersPerEvent = array(); |
||
| 304 | foreach ($handlers as $handler) { |
||
| 305 | $HandlersPerEvent[$handler->getEvent()][$handler->getHandlerType()][] = $handler; |
||
| 306 | } |
||
| 307 | |||
| 308 | return $app->render('Store/plugin_handler.twig', array( |
||
| 309 | 'handlersPerEvent' => $HandlersPerEvent |
||
| 310 | )); |
||
| 311 | |||
| 312 | } |
||
| 313 | |||
| 314 | View Code Duplication | public function handler_up(Application $app, $handlerId) |
|
| 321 | |||
| 322 | View Code Duplication | public function handler_down(Application $app, $handlerId) |
|
| 323 | { |
||
| 324 | $repo = $app['eccube.repository.plugin_event_handler']; |
||
| 325 | $repo->upPriority($repo->find($handlerId), false); |
||
| 326 | |||
| 329 | |||
| 330 | /** |
||
| 331 | * プラグインファイルアップロード画面 |
||
| 332 | * |
||
| 333 | * @param Application $app |
||
| 334 | * @param Request $request |
||
| 335 | */ |
||
| 336 | public function install(Application $app, Request $request) |
||
| 392 | |||
| 393 | /** |
||
| 394 | * オーナーズストアプラグインインストール画面 |
||
| 395 | * |
||
| 396 | * @param Application $app |
||
| 397 | * @param Request $request |
||
| 398 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 399 | */ |
||
| 400 | public function ownersInstall(Application $app, Request $request) |
||
| 509 | |||
| 510 | /** |
||
| 511 | * オーナーズブラグインインストール、アップデート |
||
| 512 | * |
||
| 513 | * @param Application $app |
||
| 514 | * @param Request $request |
||
| 515 | * @param $action |
||
| 516 | * @param $id |
||
| 517 | * @param $version |
||
| 518 | */ |
||
| 519 | public function upgrade(Application $app, Request $request, $action, $id, $version) |
||
| 608 | |||
| 609 | /** |
||
| 610 | * 認証キー設定画面 |
||
| 611 | * |
||
| 612 | * @param Application $app |
||
| 613 | * @param Request $request |
||
| 614 | */ |
||
| 615 | public function authenticationSetting(Application $app, Request $request) |
||
| 655 | |||
| 656 | |||
| 657 | /** |
||
| 658 | * APIリクエスト処理 |
||
| 659 | * |
||
| 660 | * @param Request $request |
||
| 661 | * @param $authKey |
||
| 662 | * @param string $url |
||
| 663 | * @param Application $app |
||
| 664 | * @return array |
||
| 665 | */ |
||
| 666 | private function getRequestApi(Request $request, $authKey, $url, $app) |
||
| 696 | |||
| 697 | /** |
||
| 698 | * レスポンスのチェック |
||
| 699 | * |
||
| 700 | * @param $info |
||
| 701 | * @return string |
||
| 702 | */ |
||
| 703 | private function getResponseErrorMessage($info) |
||
| 717 | |||
| 718 | |||
| 719 | /** |
||
| 720 | * フォルダ設置のみのプラグインを取得する. |
||
| 721 | * |
||
| 722 | * @param array $plugins |
||
| 723 | * @param Application $app |
||
| 724 | * @return array |
||
| 725 | */ |
||
| 726 | protected function getUnregisteredPlugins(array $plugins, \Eccube\Application $app) |
||
| 762 | } |
||
| 763 |