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 |
||
43 | class PluginController extends AbstractController |
||
44 | { |
||
45 | /** |
||
46 | * @var PluginService |
||
47 | */ |
||
48 | protected $pluginService; |
||
49 | |||
50 | /** |
||
51 | * @var BaseInfo |
||
52 | */ |
||
53 | protected $BaseInfo; |
||
54 | |||
55 | /** |
||
56 | * @var PluginRepository |
||
57 | */ |
||
58 | protected $pluginRepository; |
||
59 | |||
60 | /** |
||
61 | * @var PluginApiService |
||
62 | */ |
||
63 | protected $pluginApiService; |
||
64 | |||
65 | /** |
||
66 | * PluginController constructor. |
||
67 | * |
||
68 | * @param PluginRepository $pluginRepository |
||
69 | * @param PluginService $pluginService |
||
70 | * @param BaseInfoRepository $baseInfoRepository |
||
71 | * @param PluginApiService $pluginApiService |
||
72 | * |
||
73 | * @throws \Doctrine\ORM\NoResultException |
||
74 | * @throws \Doctrine\ORM\NonUniqueResultException |
||
75 | */ |
||
76 | public function __construct(PluginRepository $pluginRepository, PluginService $pluginService, BaseInfoRepository $baseInfoRepository, PluginApiService $pluginApiService) |
||
77 | { |
||
78 | $this->pluginRepository = $pluginRepository; |
||
79 | $this->pluginService = $pluginService; |
||
80 | $this->BaseInfo = $baseInfoRepository->get(); |
||
81 | $this->pluginApiService = $pluginApiService; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * インストール済プラグイン画面 |
||
86 | * |
||
87 | * @Route("/%eccube_admin_route%/store/plugin", name="admin_store_plugin") |
||
88 | * @Template("@admin/Store/plugin.twig") |
||
89 | * @param Request $request |
||
90 | * @return array |
||
91 | */ |
||
92 | public function index(Request $request) |
||
|
|||
93 | { |
||
94 | $pluginForms = []; |
||
95 | $configPages = []; |
||
96 | $Plugins = $this->pluginRepository->findBy([], ['code' => 'ASC']); |
||
97 | |||
98 | // ファイル設置プラグインの取得. |
||
99 | $unregisteredPlugins = $this->getUnregisteredPlugins($Plugins); |
||
100 | $unregisteredPluginsConfigPages = []; |
||
101 | foreach ($unregisteredPlugins as $unregisteredPlugin) { |
||
102 | try { |
||
103 | $code = $unregisteredPlugin['code']; |
||
104 | // プラグイン用設定画面があれば表示(プラグイン用のサービスプロバイダーに定義されているか) |
||
105 | $unregisteredPluginsConfigPages[$code] = $this->generateUrl('plugin_'.$code.'_config'); |
||
106 | } catch (RouteNotFoundException $e) { |
||
107 | // プラグインで設定画面のルートが定義されていない場合は無視 |
||
108 | } |
||
109 | } |
||
110 | |||
111 | $officialPlugins = []; |
||
112 | $unofficialPlugins = []; |
||
113 | |||
114 | foreach ($Plugins as $Plugin) { |
||
115 | $form = $this->formFactory |
||
116 | ->createNamedBuilder( |
||
117 | 'form'.$Plugin->getId(), |
||
118 | PluginManagementType::class, |
||
119 | null, |
||
120 | [ |
||
121 | 'plugin_id' => $Plugin->getId(), |
||
122 | ] |
||
123 | ) |
||
124 | ->getForm(); |
||
125 | $pluginForms[$Plugin->getId()] = $form->createView(); |
||
126 | |||
127 | try { |
||
128 | // プラグイン用設定画面があれば表示(プラグイン用のサービスプロバイダーに定義されているか) |
||
129 | $configPages[$Plugin->getCode()] = $this->generateUrl(Container::underscore($Plugin->getCode()).'_admin_config'); |
||
130 | } catch (\Exception $e) { |
||
131 | // プラグインで設定画面のルートが定義されていない場合は無視 |
||
132 | } |
||
133 | if ($Plugin->getSource() == 0) { |
||
134 | // 商品IDが設定されていない場合、非公式プラグイン |
||
135 | $unofficialPlugins[] = $Plugin; |
||
136 | } else { |
||
137 | $officialPlugins[$Plugin->getSource()] = $Plugin; |
||
138 | } |
||
139 | } |
||
140 | |||
141 | // Todo: Need new authentication mechanism |
||
142 | // オーナーズストアからダウンロード可能プラグイン情報を取得 |
||
143 | $authKey = $this->BaseInfo->getAuthenticationKey(); |
||
144 | // オーナーズストア通信 |
||
145 | list($json, $info) = $this->pluginApiService->getPurchased(); |
||
146 | $officialPluginsDetail = []; |
||
147 | if ($json) { |
||
148 | // 接続成功時 |
||
149 | $data = json_decode($json, true); |
||
150 | foreach ($data as $item) { |
||
151 | if (isset($officialPlugins[$item['id']])) { |
||
152 | $Plugin = $officialPlugins[$item['id']]; |
||
153 | $officialPluginsDetail[$item['id']] = $item; |
||
154 | $officialPluginsDetail[$item['id']]['update_status'] = 0; |
||
155 | View Code Duplication | if ($this->pluginService->isUpdate($Plugin->getVersion(), $item['version'])) { |
|
156 | $officialPluginsDetail[$item['id']]['update_status'] = 1; |
||
157 | } |
||
158 | } else { |
||
159 | $Plugin = new Plugin(); |
||
160 | $Plugin->setName($item['name']); |
||
161 | $Plugin->setCode($item['code']); |
||
162 | $Plugin->setVersion($item['version']); |
||
163 | $Plugin->setSource($item['id']); |
||
164 | $Plugin->setEnabled(false); |
||
165 | $officialPlugins[$item['id']] = $Plugin; |
||
166 | $officialPluginsDetail[$item['id']] = $item; |
||
167 | $officialPluginsDetail[$item['id']]['update_status'] = 0; |
||
168 | View Code Duplication | if ($this->pluginService->isUpdate($Plugin->getVersion(), $item['version'])) { |
|
169 | $officialPluginsDetail[$item['id']]['update_status'] = 1; |
||
170 | } |
||
171 | } |
||
172 | } |
||
173 | } |
||
174 | |||
175 | return [ |
||
176 | 'plugin_forms' => $pluginForms, |
||
177 | 'officialPlugins' => $officialPlugins, |
||
178 | 'unofficialPlugins' => $unofficialPlugins, |
||
179 | 'configPages' => $configPages, |
||
180 | 'unregisteredPlugins' => $unregisteredPlugins, |
||
181 | 'unregisteredPluginsConfigPages' => $unregisteredPluginsConfigPages, |
||
182 | 'officialPluginsDetail' => $officialPluginsDetail, |
||
183 | ]; |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * インストール済プラグインからのアップデート |
||
188 | * |
||
189 | * @Route("/%eccube_admin_route%/store/plugin/{id}/update", requirements={"id" = "\d+"}, name="admin_store_plugin_update", methods={"POST"}) |
||
190 | * |
||
191 | * @param Request $request |
||
192 | * @param Plugin $Plugin |
||
193 | * |
||
194 | * @return RedirectResponse |
||
195 | */ |
||
196 | public function update(Request $request, Plugin $Plugin) |
||
197 | { |
||
198 | $form = $this->formFactory |
||
199 | ->createNamedBuilder( |
||
200 | 'form'.$Plugin->getId(), |
||
201 | PluginManagementType::class, |
||
202 | null, |
||
203 | [ |
||
204 | 'plugin_id' => null, // placeHolder |
||
205 | ] |
||
206 | ) |
||
207 | ->getForm(); |
||
208 | |||
209 | $message = ''; |
||
210 | $form->handleRequest($request); |
||
211 | if ($form->isSubmitted() && $form->isValid()) { |
||
212 | $tmpDir = null; |
||
213 | try { |
||
214 | $formFile = $form['plugin_archive']->getData(); |
||
215 | $tmpDir = $this->pluginService->createTempDir(); |
||
216 | $tmpFile = sha1(StringUtil::random(32)).'.'.$formFile->getClientOriginalExtension(); |
||
217 | $formFile->move($tmpDir, $tmpFile); |
||
218 | $this->pluginService->update($Plugin, $tmpDir.'/'.$tmpFile); |
||
219 | $fs = new Filesystem(); |
||
220 | $fs->remove($tmpDir); |
||
221 | $this->addSuccess('admin.plugin.update.complete', 'admin'); |
||
222 | |||
223 | return $this->redirectToRoute('admin_store_plugin'); |
||
224 | } catch (PluginException $e) { |
||
225 | if (!empty($tmpDir) && file_exists($tmpDir)) { |
||
226 | $fs = new Filesystem(); |
||
227 | $fs->remove($tmpDir); |
||
228 | } |
||
229 | $message = $e->getMessage(); |
||
230 | } catch (\Exception $er) { |
||
231 | // Catch composer install error | Other error |
||
232 | if (!empty($tmpDir) && file_exists($tmpDir)) { |
||
233 | $fs = new Filesystem(); |
||
234 | $fs->remove($tmpDir); |
||
235 | } |
||
236 | log_error('plugin install failed.', ['original-message' => $er->getMessage()]); |
||
237 | $message = 'admin.plugin.install.fail'; |
||
238 | } |
||
239 | } else { |
||
240 | $errors = $form->getErrors(true); |
||
241 | foreach ($errors as $error) { |
||
242 | $message = $error->getMessage(); |
||
243 | } |
||
244 | } |
||
245 | |||
246 | $this->addError($message, 'admin'); |
||
247 | |||
248 | return $this->redirectToRoute('admin_store_plugin'); |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * 対象のプラグインを有効にします。 |
||
253 | * |
||
254 | * @Route("/%eccube_admin_route%/store/plugin/{id}/enable", requirements={"id" = "\d+"}, name="admin_store_plugin_enable", methods={"POST"}) |
||
255 | * |
||
256 | * @param Plugin $Plugin |
||
257 | * |
||
258 | * @return RedirectResponse|JsonResponse |
||
259 | * @throws PluginException |
||
260 | */ |
||
261 | public function enable(Plugin $Plugin, CacheUtil $cacheUtil, Request $request) |
||
262 | { |
||
263 | $this->isTokenValid(); |
||
264 | |||
265 | $log = null; |
||
266 | |||
267 | if ($Plugin->isEnabled()) { |
||
268 | if ($request->isXmlHttpRequest()) { |
||
269 | return $this->json(['success' => true]); |
||
270 | } else { |
||
271 | $this->addError('admin.plugin.already.enable', 'admin'); |
||
272 | } |
||
273 | } else { |
||
274 | |||
275 | // ストアからインストールしたプラグインは依存プラグインが有効化されているかを確認 |
||
276 | if ($Plugin->getSource()) { |
||
277 | $requires = $this->pluginService->getPluginRequired($Plugin); |
||
278 | View Code Duplication | $requires = array_filter($requires, function($req) { |
|
279 | $code = preg_replace('/^ec-cube\//', '', $req['name']); |
||
280 | /** @var Plugin $DependPlugin */ |
||
281 | $DependPlugin = $this->pluginRepository->findOneBy(['code' => $code]); |
||
282 | return $DependPlugin->isEnabled() == false; |
||
283 | }); |
||
284 | if (!empty($requires)) { |
||
285 | $names = array_map(function($req) { |
||
286 | return "「${req['description']}」"; |
||
287 | }, $requires); |
||
288 | $message = trans('%depend_name%を先に有効化してください。', ['%name%' => $Plugin->getName(), '%depend_name%' => implode(', ', $names)]); |
||
289 | |||
290 | if ($request->isXmlHttpRequest()) { |
||
291 | return $this->json(['success' => false, 'message' => $message], 400); |
||
292 | } else { |
||
293 | $this->addError($message, 'admin'); |
||
294 | return $this->redirectToRoute('admin_store_plugin'); |
||
295 | } |
||
296 | } |
||
297 | } |
||
298 | |||
299 | ob_start(); |
||
300 | $this->pluginService->enable($Plugin); |
||
301 | $log = ob_get_clean(); |
||
302 | ob_end_flush(); |
||
303 | } |
||
304 | |||
305 | // $cacheUtil->clearCache(); |
||
306 | |||
307 | View Code Duplication | if ($request->isXmlHttpRequest()) { |
|
308 | return $this->json(['success' => true, 'log' => $log]); |
||
309 | } else { |
||
310 | $this->addSuccess(trans('「%plugin_name%」を有効にしました。', ['%plugin_name%' => $Plugin->getName()]), 'admin'); |
||
311 | return $this->redirectToRoute('admin_store_plugin'); |
||
312 | } |
||
313 | } |
||
314 | |||
315 | /** |
||
316 | * 対象のプラグインを無効にします。 |
||
317 | * |
||
318 | * @Route("/%eccube_admin_route%/store/plugin/{id}/disable", requirements={"id" = "\d+"}, name="admin_store_plugin_disable", methods={"POST"}) |
||
319 | * |
||
320 | * @param Request $request |
||
321 | * @param Plugin $Plugin |
||
322 | * |
||
323 | * @param CacheUtil $cacheUtil |
||
324 | * @return \Symfony\Component\HttpFoundation\JsonResponse|RedirectResponse |
||
325 | */ |
||
326 | public function disable(Request $request, Plugin $Plugin, CacheUtil $cacheUtil) |
||
327 | { |
||
328 | $this->isTokenValid(); |
||
329 | |||
330 | $log = null; |
||
331 | if ($Plugin->isEnabled()) { |
||
332 | $dependents = $this->pluginService->findDependentPluginNeedDisable($Plugin->getCode()); |
||
333 | if (!empty($dependents)) { |
||
334 | $dependName = $dependents[0]; |
||
335 | $DependPlugin = $this->pluginRepository->findOneBy(['code' => $dependents[0]]); |
||
336 | if ($DependPlugin) { |
||
337 | $dependName = $DependPlugin->getName(); |
||
338 | } |
||
339 | $message = trans('admin.plugin.disable.depend', ['%name%' => $Plugin->getName(), '%depend_name%' => $dependName]); |
||
340 | |||
341 | if ($request->isXmlHttpRequest()) { |
||
342 | return $this->json(['message'=> $message], 400); |
||
343 | } else { |
||
344 | $this->addError($message, 'admin'); |
||
345 | return $this->redirectToRoute('admin_store_plugin'); |
||
346 | } |
||
347 | } |
||
348 | |||
349 | ob_start(); |
||
350 | $this->pluginService->disable($Plugin); |
||
351 | $log = ob_get_clean(); |
||
352 | ob_end_flush(); |
||
353 | |||
354 | |||
355 | } else { |
||
356 | if ($request->isXmlHttpRequest()) { |
||
357 | return $this->json(['success' => true]); |
||
358 | } else { |
||
359 | $this->addError('admin.plugin.already.disable', 'admin'); |
||
360 | return $this->redirectToRoute('admin_store_plugin'); |
||
361 | } |
||
362 | } |
||
363 | |||
364 | // キャッシュを削除してリダイレクト |
||
365 | // リダイレクトにredirectToRoute関数を使用していないのは、削除したキャッシュが再生成されてしまうため。 |
||
366 | $url = $this->generateUrl('admin_store_plugin'); |
||
367 | // $cacheUtil->clearCache(); |
||
368 | |||
369 | View Code Duplication | if ($request->isXmlHttpRequest()) { |
|
370 | return $this->json(['success' => true, 'log' => $log]); |
||
371 | } else { |
||
372 | $this->addSuccess('admin.plugin.disable.complete', 'admin'); |
||
373 | return $this->redirect($url); |
||
374 | } |
||
375 | } |
||
376 | |||
377 | /** |
||
378 | * 対象のプラグインを削除します。 |
||
379 | * |
||
380 | * @Route("/%eccube_admin_route%/store/plugin/{id}/uninstall", requirements={"id" = "\d+"}, name="admin_store_plugin_uninstall", methods={"DELETE"}) |
||
381 | * |
||
382 | * @param Plugin $Plugin |
||
383 | * |
||
384 | * @return RedirectResponse |
||
385 | * @throws \Exception |
||
386 | */ |
||
387 | public function uninstall(Plugin $Plugin) |
||
388 | { |
||
389 | $this->isTokenValid(); |
||
390 | |||
391 | if ($Plugin->isEnabled()) { |
||
392 | $this->addError('admin.plugin.uninstall.error.not_disable', 'admin'); |
||
393 | |||
394 | return $this->redirectToRoute('admin_store_plugin'); |
||
395 | } |
||
396 | |||
397 | // Check other plugin depend on it |
||
398 | $pluginCode = $Plugin->getCode(); |
||
399 | $otherDepend = $this->pluginService->findDependentPlugin($pluginCode); |
||
400 | if (!empty($otherDepend)) { |
||
401 | $DependPlugin = $this->pluginRepository->findOneBy(['code' => $otherDepend[0]]); |
||
402 | $dependName = $otherDepend[0]; |
||
403 | if ($DependPlugin) { |
||
404 | $dependName = $DependPlugin->getName(); |
||
405 | } |
||
406 | $message = trans('admin.plugin.uninstall.depend', ['%name%' => $Plugin->getName(), '%depend_name%' => $dependName]); |
||
407 | $this->addError($message, 'admin'); |
||
408 | |||
409 | return $this->redirectToRoute('admin_store_plugin'); |
||
410 | } |
||
411 | |||
412 | $this->pluginService->uninstall($Plugin); |
||
413 | $this->addSuccess('admin.plugin.uninstall.complete', 'admin'); |
||
414 | |||
415 | return $this->redirectToRoute('admin_store_plugin'); |
||
416 | } |
||
417 | |||
418 | /** |
||
419 | * プラグインファイルアップロード画面 |
||
420 | * |
||
421 | * @Route("/%eccube_admin_route%/store/plugin/install", name="admin_store_plugin_install") |
||
422 | * @Template("@admin/Store/plugin_install.twig") |
||
423 | * |
||
424 | * @param Request $request |
||
425 | * |
||
426 | * @return array|RedirectResponse |
||
427 | */ |
||
428 | public function install(Request $request) |
||
429 | { |
||
430 | $form = $this->formFactory |
||
431 | ->createBuilder(PluginLocalInstallType::class) |
||
432 | ->getForm(); |
||
433 | $errors = []; |
||
434 | $form->handleRequest($request); |
||
435 | if ($form->isSubmitted() && $form->isValid()) { |
||
436 | $tmpDir = null; |
||
437 | try { |
||
438 | $service = $this->pluginService; |
||
439 | /** @var UploadedFile $formFile */ |
||
440 | $formFile = $form['plugin_archive']->getData(); |
||
441 | $tmpDir = $service->createTempDir(); |
||
442 | // 拡張子を付けないとpharが動かないので付ける |
||
443 | $tmpFile = sha1(StringUtil::random(32)).'.'.$formFile->getClientOriginalExtension(); |
||
444 | $formFile->move($tmpDir, $tmpFile); |
||
445 | $tmpPath = $tmpDir.'/'.$tmpFile; |
||
446 | $service->install($tmpPath); |
||
447 | // Remove tmp file |
||
448 | $fs = new Filesystem(); |
||
449 | $fs->remove($tmpDir); |
||
450 | $this->addSuccess('admin.plugin.install.complete', 'admin'); |
||
451 | |||
452 | return $this->redirectToRoute('admin_store_plugin'); |
||
453 | } catch (PluginException $e) { |
||
454 | if (!empty($tmpDir) && file_exists($tmpDir)) { |
||
455 | $fs = new Filesystem(); |
||
456 | $fs->remove($tmpDir); |
||
457 | } |
||
458 | log_error('plugin install failed.', ['original-message' => $e->getMessage()]); |
||
459 | $errors[] = $e; |
||
460 | } catch (\Exception $er) { |
||
461 | // Catch composer install error | Other error |
||
462 | if (!empty($tmpDir) && file_exists($tmpDir)) { |
||
463 | $fs = new Filesystem(); |
||
464 | $fs->remove($tmpDir); |
||
465 | } |
||
466 | log_error('plugin install failed.', ['original-message' => $er->getMessage()]); |
||
467 | $this->addError('admin.plugin.install.fail', 'admin'); |
||
468 | } |
||
469 | } else { |
||
470 | foreach ($form->getErrors(true) as $error) { |
||
471 | $errors[] = $error; |
||
472 | } |
||
473 | } |
||
474 | |||
475 | return [ |
||
476 | 'form' => $form->createView(), |
||
477 | 'errors' => $errors, |
||
478 | ]; |
||
479 | } |
||
480 | |||
481 | /** |
||
482 | * 認証キー設定画面 |
||
483 | * |
||
484 | * @Route("/%eccube_admin_route%/store/plugin/authentication_setting", name="admin_store_authentication_setting") |
||
485 | * @Template("@admin/Store/authentication_setting.twig") |
||
486 | */ |
||
487 | public function authenticationSetting(Request $request) |
||
488 | { |
||
489 | $builder = $this->formFactory |
||
490 | ->createBuilder(AuthenticationType::class, $this->BaseInfo); |
||
491 | |||
492 | $form = $builder->getForm(); |
||
493 | $form->handleRequest($request); |
||
494 | |||
495 | if ($form->isSubmitted() && $form->isValid()) { |
||
496 | // 認証キーの登録 and PHP path |
||
497 | $this->BaseInfo = $form->getData(); |
||
498 | $this->entityManager->persist($this->BaseInfo); |
||
499 | $this->entityManager->flush(); |
||
500 | |||
501 | $this->addSuccess('admin.common.save_complete', 'admin'); |
||
502 | } |
||
503 | |||
504 | return [ |
||
505 | 'form' => $form->createView(), |
||
506 | 'eccubeUrl' => $this->generateUrl('homepage', [], UrlGeneratorInterface::ABSOLUTE_URL) |
||
507 | ]; |
||
508 | } |
||
509 | |||
510 | /** |
||
511 | * APIリクエスト処理 |
||
512 | * |
||
513 | * @param Request $request |
||
514 | * @param string|null $authKey |
||
515 | * @param string $url |
||
516 | * |
||
517 | * @deprecated since release, please refer PluginApiService |
||
518 | * |
||
519 | * @return array |
||
520 | */ |
||
521 | private function getRequestApi(Request $request, $authKey, $url) |
||
551 | |||
552 | /** |
||
553 | * レスポンスのチェック |
||
554 | * |
||
555 | * @param $info |
||
556 | * |
||
557 | * @return string |
||
558 | * |
||
559 | * @deprecated since release, please refer PluginApiService |
||
560 | */ |
||
561 | View Code Duplication | private function getResponseErrorMessage($info) |
|
574 | |||
575 | /** |
||
576 | * フォルダ設置のみのプラグインを取得する. |
||
577 | * |
||
578 | * @param array $plugins |
||
579 | * |
||
580 | * @return array |
||
581 | */ |
||
582 | protected function getUnregisteredPlugins(array $plugins) |
||
618 | } |
||
619 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.