| Conditions | 8 |
| Paths | 24 |
| Total Lines | 79 |
| Code Lines | 44 |
| 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 |
||
| 22 | public function indexAction(Request $request, Application $app) |
||
| 23 | { |
||
| 24 | if (!$app['security']->isGranted('ROLE_ADMIN')) { |
||
| 25 | $app->abort(403); |
||
| 26 | } |
||
| 27 | |||
| 28 | $form = $app['form.factory']->create( |
||
| 29 | new SettingsType($app) |
||
| 30 | ); |
||
| 31 | |||
| 32 | if ($request->getMethod() == 'POST') { |
||
| 33 | $form->handleRequest($request); |
||
| 34 | |||
| 35 | if ($form->isValid()) { |
||
| 36 | $data = $form->getData(); |
||
| 37 | |||
| 38 | if (!empty($data)) { |
||
| 39 | foreach ($data as $key => $value) { |
||
| 40 | $settingEntity = $app['orm.em'] |
||
| 41 | ->getRepository('Application\Entity\SettingEntity') |
||
| 42 | ->findOneByKey($key) |
||
| 43 | ; |
||
| 44 | |||
| 45 | if ($settingEntity === null) { |
||
| 46 | $settingEntity = new SettingEntity(); |
||
| 47 | |||
| 48 | $settingEntity |
||
| 49 | ->setKey($key) |
||
| 50 | ; |
||
| 51 | } |
||
| 52 | |||
| 53 | $settingEntity |
||
| 54 | ->setValue($value) |
||
| 55 | ; |
||
| 56 | |||
| 57 | $app['orm.em']->persist($settingEntity); |
||
| 58 | } |
||
| 59 | |||
| 60 | try { |
||
| 61 | $app['orm.em']->flush(); |
||
| 62 | |||
| 63 | $app['flashbag']->add( |
||
| 64 | 'success', |
||
| 65 | $app['translator']->trans( |
||
| 66 | 'The settings were successfully saved!' |
||
| 67 | ) |
||
| 68 | ); |
||
| 69 | } catch (\Exception $e) { |
||
| 70 | $app['flashbag']->add( |
||
| 71 | 'danger', |
||
| 72 | $e->getMessage() |
||
| 73 | ); |
||
| 74 | } |
||
| 75 | |||
| 76 | return $app->redirect( |
||
| 77 | $app['url_generator']->generate( |
||
| 78 | 'members-area.settings' |
||
| 79 | ) |
||
| 80 | ); |
||
| 81 | } else { |
||
| 82 | $app['flashbag']->add( |
||
| 83 | 'info', |
||
| 84 | $app['translator']->trans( |
||
| 85 | 'No changes were saved!' |
||
| 86 | ) |
||
| 87 | ); |
||
| 88 | } |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | return new Response( |
||
| 93 | $app['twig']->render( |
||
| 94 | 'contents/members-area/settings/index.html.twig', |
||
| 95 | array( |
||
| 96 | 'form' => $form->createView(), |
||
| 97 | ) |
||
| 98 | ) |
||
| 99 | ); |
||
| 100 | } |
||
| 101 | } |
||
| 102 |