Conditions | 9 |
Paths | 27 |
Total Lines | 79 |
Code Lines | 49 |
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 |
||
108 | public function edit(Application $app, Request $request) |
||
109 | { |
||
110 | $builder2 = $app['form.factory']->createBuilder('admin_system_masterdata_edit'); |
||
111 | |||
112 | $event = new EventArgs( |
||
113 | array( |
||
114 | 'builder' => $builder2, |
||
115 | ), |
||
116 | $request |
||
117 | ); |
||
118 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_SETTING_SYSTEM_MASTERDATA_EDIT_INITIALIZE, $event); |
||
119 | |||
120 | $form2 = $builder2->getForm(); |
||
121 | |||
122 | if ('POST' === $request->getMethod()) { |
||
123 | $form2->handleRequest($request); |
||
124 | |||
125 | if ($form2->isValid()) { |
||
126 | $data = $form2->getData(); |
||
127 | |||
128 | $entityName = str_replace('-', '\\', $data['masterdata_name']); |
||
129 | $entity = new $entityName(); |
||
130 | $rank = 0; |
||
131 | $ids = array_map(function ($v) {return $v['id'];}, $data['data']); |
||
132 | foreach ($data['data'] as $key => $value) { |
||
133 | if ($value['id'] !== null && $value['name'] !== null) { |
||
134 | $entity->setId($value['id']); |
||
135 | $entity->setName($value['name']); |
||
136 | $entity->setRank($rank++); |
||
137 | $app['orm.em']->merge($entity); |
||
138 | } elseif (!in_array($key, $ids)) { |
||
139 | // remove |
||
140 | $delKey = $app['orm.em']->getRepository($entityName)->find($key); |
||
141 | if ($delKey) { |
||
142 | $app['orm.em']->remove($delKey); |
||
143 | } |
||
144 | } |
||
145 | } |
||
146 | |||
147 | try { |
||
148 | $app['orm.em']->flush(); |
||
149 | |||
150 | $event = new EventArgs( |
||
151 | array( |
||
152 | 'form' => $form2, |
||
153 | ), |
||
154 | $request |
||
155 | ); |
||
156 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_SETTING_SYSTEM_MASTERDATA_EDIT_COMPLETE, $event); |
||
157 | |||
158 | $app->addSuccess('admin.register.complete', 'admin'); |
||
159 | } catch (\Exception $e) { |
||
160 | // 外部キー制約などで削除できない場合に例外エラーになる |
||
161 | $app->addError('admin.register.failed', 'admin'); |
||
162 | } |
||
163 | |||
164 | return $app->redirect($app->url('admin_setting_system_masterdata_view', array('entity' => $data['masterdata_name']))); |
||
165 | } |
||
166 | } |
||
167 | |||
168 | $builder = $app['form.factory']->createBuilder('admin_system_masterdata'); |
||
169 | |||
170 | $event = new EventArgs( |
||
171 | array( |
||
172 | 'builder' => $builder, |
||
173 | ), |
||
174 | $request |
||
175 | ); |
||
176 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_SETTING_SYSTEM_MASTERDATA_EDIT_FORM_INITIALIZE, $event); |
||
177 | |||
178 | $form = $builder->getForm(); |
||
179 | $parameter = array_merge($request->request->all(), array('masterdata' => $form2['masterdata_name']->getData())); |
||
180 | $form->submit($parameter); |
||
181 | |||
182 | return $app->render('Setting/System/masterdata.twig', array( |
||
183 | 'form' => $form->createView(), |
||
184 | 'form2' => $form2->createView(), |
||
185 | )); |
||
186 | } |
||
187 | } |
||
188 |