| Conditions | 19 |
| Paths | 42 |
| Total Lines | 136 |
| Code Lines | 88 |
| 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 |
||
| 65 | public function createPage($pageSlug, $redirect, Request $request) |
||
| 66 | { |
||
| 67 | $siteSelector = $this->container->get('sonata.page.site.selector'); |
||
| 68 | $site = $siteSelector->retrieve(); |
||
| 69 | $em = $this->getDoctrine()->getManager(); |
||
| 70 | $page = null; |
||
| 71 | |||
| 72 | $form = $this->createFormBuilder() |
||
| 73 | ->add('content', CKEditorType::class) |
||
| 74 | ->add('save', SubmitType::class, ['label' => 'Update']) |
||
| 75 | ->getForm(); |
||
| 76 | |||
| 77 | $blockToEdit = null; |
||
| 78 | if ($site) { |
||
| 79 | $pageManager = $this->get('sonata.page.manager.page'); |
||
| 80 | // Parents only of homepage |
||
| 81 | $criteria = ['site' => $site, 'enabled' => true, 'parent' => 1, 'slug' => $pageSlug]; |
||
| 82 | /** @var Page $page */ |
||
| 83 | $page = $pageManager->findOneBy($criteria); |
||
| 84 | if ($page) { |
||
|
|
|||
| 85 | $blocks = $page->getBlocks(); |
||
| 86 | /** @var Block $block */ |
||
| 87 | foreach ($blocks as $block) { |
||
| 88 | if ($block->getName() == 'Main content') { |
||
| 89 | $code = $block->getSetting('code'); |
||
| 90 | if ($code == 'content') { |
||
| 91 | $children = $block->getChildren(); |
||
| 92 | /** @var Block $child */ |
||
| 93 | foreach ($children as $child) { |
||
| 94 | if ($child->getType() == 'sonata.formatter.block.formatter') { |
||
| 95 | $blockToEdit = $child; |
||
| 96 | break 2; |
||
| 97 | } |
||
| 98 | } |
||
| 99 | } |
||
| 100 | } |
||
| 101 | } |
||
| 102 | } else { |
||
| 103 | $pageManager = $this->get('sonata.page.manager.page'); |
||
| 104 | |||
| 105 | $criteria = ['site' => $site, 'enabled' => true, 'parent' => null, 'slug' => 'homepage']; |
||
| 106 | /** @var Page $page */ |
||
| 107 | $parent = $pageManager->findOneBy($criteria); |
||
| 108 | |||
| 109 | $page = $pageManager->create(); |
||
| 110 | $page->setSlug($pageSlug); |
||
| 111 | $page->setUrl('/'.$pageSlug); |
||
| 112 | $page->setName($pageSlug); |
||
| 113 | $page->setTitle($pageSlug); |
||
| 114 | $page->setEnabled(true); |
||
| 115 | $page->setDecorate(1); |
||
| 116 | $page->setRequestMethod('GET'); |
||
| 117 | $page->setTemplateCode('default'); |
||
| 118 | $page->setRouteName($pageSlug); |
||
| 119 | $page->setParent($parent); |
||
| 120 | $page->setSite($site); |
||
| 121 | |||
| 122 | $pageManager->save($page); |
||
| 123 | |||
| 124 | $templateManager = $this->get('sonata.page.template_manager'); |
||
| 125 | $template = $templateManager->get('default'); |
||
| 126 | $templateContainers = $template->getContainers(); |
||
| 127 | |||
| 128 | $containers = []; |
||
| 129 | foreach ($templateContainers as $id => $area) { |
||
| 130 | $containers[$id] = [ |
||
| 131 | 'area' => $area, |
||
| 132 | 'block' => false, |
||
| 133 | ]; |
||
| 134 | } |
||
| 135 | |||
| 136 | // Create blocks for this page |
||
| 137 | $blockInteractor = $this->get('sonata.page.block_interactor'); |
||
| 138 | $parentBlock = null; |
||
| 139 | foreach ($containers as $id => $area) { |
||
| 140 | if (false === $area['block'] && $templateContainers[$id]['shared'] === false) { |
||
| 141 | $block = $blockInteractor->createNewContainer( |
||
| 142 | [ |
||
| 143 | 'page' => $page, |
||
| 144 | 'name' => $templateContainers[$id]['name'], |
||
| 145 | 'code' => $id, |
||
| 146 | ] |
||
| 147 | ); |
||
| 148 | |||
| 149 | if ($id === 'content' && $templateContainers[$id]['name'] == 'Main content') { |
||
| 150 | $parentBlock = $block; |
||
| 151 | } |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | // Create block in main content |
||
| 156 | $block = $this->get('sonata.page.manager.block'); |
||
| 157 | /** @var \Sonata\BlockBundle\Model\Block $myBlock */ |
||
| 158 | $myBlock = $block->create(); |
||
| 159 | $myBlock->setType('sonata.formatter.block.formatter'); |
||
| 160 | $myBlock->setSetting('format', 'richhtml'); |
||
| 161 | $myBlock->setSetting('content', ''); |
||
| 162 | $myBlock->setSetting('rawContent', ''); |
||
| 163 | $myBlock->setSetting('template', '@SonataFormatter/Block/block_formatter.html.twig'); |
||
| 164 | $myBlock->setParent($parentBlock); |
||
| 165 | $page->addBlocks($myBlock); |
||
| 166 | $pageManager->save($page); |
||
| 167 | } |
||
| 168 | } |
||
| 169 | |||
| 170 | if ($blockToEdit) { |
||
| 171 | $form->setData(['content' => $blockToEdit->getSetting('content')]); |
||
| 172 | } |
||
| 173 | |||
| 174 | $form->handleRequest($request); |
||
| 175 | |||
| 176 | if ($form->isSubmitted() && $form->isValid() && $blockToEdit) { |
||
| 177 | $data = $form->getData(); |
||
| 178 | $content = $data['content']; |
||
| 179 | /** @var Block $blockToEdit */ |
||
| 180 | $blockToEdit->setSetting('rawContent', $content); |
||
| 181 | $blockToEdit->setSetting('content', $content); |
||
| 182 | $em->merge($blockToEdit); |
||
| 183 | $em->flush(); |
||
| 184 | |||
| 185 | $this->addFlash('success', $this->trans('Updated')); |
||
| 186 | |||
| 187 | if (!empty($redirect)) { |
||
| 188 | return $this->redirect($redirect); |
||
| 189 | } |
||
| 190 | |||
| 191 | return $this->redirectToRoute('home'); |
||
| 192 | } |
||
| 193 | |||
| 194 | $template = $pageSlug.'_edit.html.twig'; |
||
| 195 | |||
| 196 | return $this->render( |
||
| 197 | '@ChamiloCore/Index/'.$template, |
||
| 198 | [ |
||
| 199 | 'page' => $page, |
||
| 200 | 'form' => $form->createView(), |
||
| 201 | ] |
||
| 205 |