| Conditions | 8 |
| Paths | 11 |
| Total Lines | 69 |
| Code Lines | 40 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 10 |
| CRAP Score | 8 |
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 |
||
| 49 | 2 | public function edit(Application $app, $id = null) |
|
| 50 | { |
||
| 51 | $DeviceType = $app['eccube.repository.master.device_type'] |
||
| 52 | ->find(DeviceType::DEVICE_TYPE_PC); |
||
| 53 | |||
| 54 | $Block = $app['eccube.repository.block'] |
||
| 55 | ->findOrCreate($id, $DeviceType); |
||
| 56 | |||
| 57 | 2 | if (!$Block) { |
|
| 58 | throw new NotFoundHttpException(); |
||
| 59 | } |
||
| 60 | |||
| 61 | $form = $app['form.factory'] |
||
| 62 | ->createBuilder('block', $Block) |
||
| 63 | ->getForm(); |
||
| 64 | |||
| 65 | 2 | $html = ''; |
|
| 66 | 2 | $previous_filename = null; |
|
| 67 | $deletable = $Block->getDeletableFlg(); |
||
| 68 | |||
| 69 | 2 | if ($id) { |
|
| 70 | // テンプレートファイルの取得 |
||
| 71 | $previous_filename = $Block->getFileName(); |
||
| 72 | $file = $app['eccube.repository.block'] |
||
| 73 | ->getReadTemplateFile($previous_filename, $deletable); |
||
| 74 | 2 | $html = $file['tpl_data']; |
|
| 75 | } |
||
| 76 | |||
| 77 | $form->get('block_html')->setData($html); |
||
| 78 | |||
| 79 | if ($app['request']->getMethod() === 'POST') { |
||
| 80 | $form->handleRequest($app['request']); |
||
| 81 | if ($form->isValid()) { |
||
| 82 | $Block = $form->getData(); |
||
| 83 | |||
| 84 | // DB登録 |
||
| 85 | $app['orm.em']->persist($Block); |
||
| 86 | $app['orm.em']->flush(); |
||
| 87 | |||
| 88 | // ファイル生成・更新 |
||
| 89 | $tplDir = $app['config']['block_realdir']; |
||
| 90 | |||
| 91 | $filePath = $tplDir . '/' . $Block->getFileName() . '.twig'; |
||
| 92 | |||
| 93 | $fs = new Filesystem(); |
||
| 94 | $fs->dumpFile($filePath, $form->get('block_html')->getData()); |
||
| 95 | // 更新でファイル名を変更した場合、以前のファイルを削除 |
||
| 96 | if ($Block->getFileName() != $previous_filename && !is_null($previous_filename)) { |
||
| 97 | 1 | $oldFilePath = $tplDir . $previous_filename; |
|
| 98 | if ($fs->exists($oldFilePath)) { |
||
| 99 | $fs->remove($oldFilePath); |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | \Eccube\Util\Cache::clear($app,false); |
||
| 104 | |||
| 105 | $app->addSuccess('admin.register.complete', 'admin'); |
||
| 106 | |||
| 107 | return $app->redirect($app->url('admin_content_block_edit', array('id' => $Block->getId()))); |
||
| 108 | } |
||
| 109 | } |
||
| 110 | |||
| 111 | |||
| 112 | 1 | return $app->render('Content/block_edit.twig', array( |
|
| 113 | 1 | 'form' => $form->createView(), |
|
| 114 | 'block_id' => $id, |
||
| 115 | 'deletable' => $deletable, |
||
| 116 | )); |
||
| 117 | 2 | } |
|
| 118 | |||
| 156 |