| Conditions | 12 |
| Paths | 28 |
| Total Lines | 65 |
| Code Lines | 40 |
| 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 |
||
| 40 | public function editWelcomeAction(Request $request) |
||
| 41 | { |
||
| 42 | $siteSelector = $this->get('sonata.page.site.selector'); |
||
| 43 | $site = $siteSelector->retrieve(); |
||
| 44 | $em = $this->getDoctrine()->getManager(); |
||
| 45 | $page = null; |
||
| 46 | |||
| 47 | $form = $this->createFormBuilder() |
||
| 48 | ->add('content', CKEditorType::class) |
||
| 49 | ->add('save', SubmitType::class, array('label' => 'Update')) |
||
| 50 | ->getForm(); |
||
| 51 | |||
| 52 | $blockToEdit = null; |
||
| 53 | if ($site) { |
||
| 54 | $pageManager = $this->get('sonata.page.manager.page'); |
||
| 55 | // Parents only of homepage |
||
| 56 | $criteria = ['site' => $site, 'enabled' => true, 'parent' => 1, 'slug' => 'welcome']; |
||
| 57 | /** @var Page $page */ |
||
| 58 | $page = $pageManager->findOneBy($criteria); |
||
| 59 | if ($page) { |
||
|
|
|||
| 60 | $blocks = $page->getBlocks(); |
||
| 61 | /** @var Block $block */ |
||
| 62 | foreach ($blocks as $block) { |
||
| 63 | if ($block->getName() == 'Main content') { |
||
| 64 | $code = $block->getSetting('code'); |
||
| 65 | if ($code == 'content') { |
||
| 66 | $children = $block->getChildren(); |
||
| 67 | /** @var Block $child */ |
||
| 68 | foreach ($children as $child) { |
||
| 69 | if ($child->getType() == 'sonata.formatter.block.formatter') { |
||
| 70 | $blockToEdit = $child; |
||
| 71 | break (2); |
||
| 72 | } |
||
| 73 | } |
||
| 74 | } |
||
| 75 | } |
||
| 76 | } |
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | if ($blockToEdit) { |
||
| 81 | $form->setData(['content' => $blockToEdit->getSetting('content')]); |
||
| 82 | } |
||
| 83 | |||
| 84 | $form->handleRequest($request); |
||
| 85 | |||
| 86 | if ($form->isSubmitted() && $form->isValid() && $blockToEdit) { |
||
| 87 | $data = $form->getData(); |
||
| 88 | $content = $data['content']; |
||
| 89 | /** @var Block $blockToEdit */ |
||
| 90 | $blockToEdit->setSetting('rawContent', $content); |
||
| 91 | $blockToEdit->setSetting('content', $content); |
||
| 92 | $em->merge($blockToEdit); |
||
| 93 | $em->flush(); |
||
| 94 | |||
| 95 | $this->addFlash('success', $this->trans('Updated')); |
||
| 96 | |||
| 97 | return $this->redirectToRoute('home'); |
||
| 98 | } |
||
| 99 | |||
| 100 | return $this->render( |
||
| 101 | '@ChamiloCore/Index/edit_welcome.html.twig', |
||
| 102 | [ |
||
| 103 | 'page' => $page, |
||
| 104 | 'form' => $form->createView(), |
||
| 105 | ] |
||
| 244 |