| Conditions | 9 |
| Paths | 42 |
| Total Lines | 86 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 54 | public function index(Request $request, MailTemplate $Mail = null, Environment $twig) |
||
| 55 | { |
||
| 56 | $builder = $this->formFactory |
||
| 57 | ->createBuilder(MailType::class, $Mail); |
||
| 58 | |||
| 59 | $event = new EventArgs( |
||
| 60 | [ |
||
| 61 | 'builder' => $builder, |
||
| 62 | 'Mail' => $Mail, |
||
| 63 | ], |
||
| 64 | $request |
||
| 65 | ); |
||
| 66 | $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_MAIL_INDEX_INITIALIZE, $event); |
||
| 67 | |||
| 68 | $form = $builder->getForm(); |
||
| 69 | $form['template']->setData($Mail); |
||
| 70 | $htmlFileName = $Mail ? $this->getHtmlFileName($Mail->getFileName()) : null; |
||
| 71 | |||
| 72 | // 更新時 |
||
| 73 | if (!is_null($Mail)) { |
||
| 74 | // テンプレートファイルの取得 |
||
| 75 | $source = $twig->getLoader() |
||
| 76 | ->getSourceContext($Mail->getFileName()) |
||
| 77 | ->getCode(); |
||
| 78 | |||
| 79 | $form->get('tpl_data')->setData($source); |
||
| 80 | if ($twig->getLoader()->exists($htmlFileName)) { |
||
| 81 | $source = $twig->getLoader() |
||
| 82 | ->getSourceContext($htmlFileName) |
||
| 83 | ->getCode(); |
||
| 84 | |||
| 85 | $form->get('html_tpl_data')->setData($source); |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | if ('POST' === $request->getMethod()) { |
||
| 90 | $form->handleRequest($request); |
||
| 91 | |||
| 92 | // 新規登録は現時点では未実装とする. |
||
| 93 | if (is_null($Mail)) { |
||
| 94 | $this->addError('admin.common.save_error', 'admin'); |
||
| 95 | |||
| 96 | return $this->redirectToRoute('admin_setting_shop_mail'); |
||
| 97 | } |
||
| 98 | |||
| 99 | if ($form->isValid()) { |
||
| 100 | $this->entityManager->flush(); |
||
| 101 | |||
| 102 | // ファイル生成・更新 |
||
| 103 | $templatePath = $this->getParameter('eccube_theme_front_dir'); |
||
| 104 | $filePath = $templatePath.'/'.$Mail->getFileName(); |
||
| 105 | |||
| 106 | $fs = new Filesystem(); |
||
| 107 | $mailData = $form->get('tpl_data')->getData(); |
||
| 108 | $mailData = StringUtil::convertLineFeed($mailData); |
||
| 109 | $fs->dumpFile($filePath, $mailData); |
||
| 110 | |||
| 111 | // HTMLファイル用 |
||
| 112 | $htmlMailData = $form->get('html_tpl_data')->getData(); |
||
| 113 | if (!is_null($htmlMailData)) { |
||
| 114 | $htmlMailData = StringUtil::convertLineFeed($htmlMailData); |
||
| 115 | $fs->dumpFile($templatePath.'/'.$htmlFileName, $htmlMailData); |
||
| 116 | } |
||
| 117 | |||
| 118 | $event = new EventArgs( |
||
| 119 | [ |
||
| 120 | 'form' => $form, |
||
| 121 | 'Mail' => $Mail, |
||
| 122 | 'templatePath' => $templatePath, |
||
| 123 | 'filePath' => $filePath, |
||
| 124 | ], |
||
| 125 | $request |
||
| 126 | ); |
||
| 127 | $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_MAIL_INDEX_COMPLETE, $event); |
||
| 128 | |||
| 129 | $this->addSuccess('admin.common.save_complete', 'admin'); |
||
| 130 | |||
| 131 | return $this->redirectToRoute('admin_setting_shop_mail_edit', ['id' => $Mail->getId()]); |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | return [ |
||
| 136 | 'form' => $form->createView(), |
||
| 137 | 'id' => is_null($Mail) ? null : $Mail->getId(), |
||
| 138 | ]; |
||
| 139 | } |
||
| 140 | |||
| 157 |