| Conditions | 9 |
| Paths | 12 |
| Total Lines | 54 |
| Code Lines | 37 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 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 |
||
| 18 | public function dispatchAction(Request $request, $slug) |
||
| 19 | { |
||
| 20 | $entityManager = $this->get('doctrine.orm.entity_manager'); |
||
| 21 | $node = $entityManager->getRepository('AlpixelCMSBundle:Node') |
||
| 22 | ->findOnePublishedBySlugAndLocale($slug, $request->getLocale()); |
||
| 23 | |||
| 24 | if ($node !== null) { |
||
| 25 | $contentType = $this->get('alpixel_cms.helper.cms')->getContentTypeFromNodeElementClass($node); |
||
| 26 | $controller = explode('::', $contentType['controller']); |
||
| 27 | |||
| 28 | try { |
||
| 29 | if (count($controller) !== 2) { |
||
| 30 | throw new \LogicException('The parameter controller must be a valid callable controller, like "My\Namespace\Controller\Class::method"'); |
||
| 31 | } elseif (!class_exists($controller[0]) || !method_exists($controller[0], $controller[1])) { |
||
| 32 | throw new \LogicException(sprintf( |
||
| 33 | 'Unable to find the "%s" controller or the method "%s" doesn\'t exist.', |
||
| 34 | $controller[0], |
||
| 35 | $controller[1] |
||
| 36 | )); |
||
| 37 | } |
||
| 38 | |||
| 39 | return $this->forward($contentType['controller'], [ |
||
| 40 | '_route' => $request->attributes->get('_route'), |
||
| 41 | '_route_params' => $request->attributes->get('_route_params'), |
||
| 42 | 'object' => $node, |
||
| 43 | ]); |
||
| 44 | } catch (\LogicException $e) { |
||
| 45 | $environment = $this->container->get('kernel')->getEnvironment(); |
||
| 46 | if ($environment === 'prod') { |
||
| 47 | $logger = $this->get('logger'); |
||
| 48 | $logger->error($e->getMessage()); |
||
| 49 | } else { |
||
| 50 | throw $e; |
||
| 51 | } |
||
| 52 | } |
||
| 53 | } else { |
||
| 54 | //Trying to find another node with this slug, in another language |
||
| 55 | $node = $entityManager->getRepository('AlpixelCMSBundle:Node') |
||
| 56 | ->findOnePublishedBySlug($slug); |
||
| 57 | |||
| 58 | if ($node !== null) { |
||
| 59 | $translation = $entityManager->getRepository('AlpixelCMSBundle:Node') |
||
| 60 | ->findTranslation($node, $request->getLocale()); |
||
| 61 | if ($translation !== null) { |
||
| 62 | return $this->redirect($this->generateUrl('alpixel_cms', [ |
||
| 63 | 'slug' => $translation->getSlug(), |
||
| 64 | '_locale' => $translation->getLocale(), |
||
| 65 | ])); |
||
| 66 | } |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | throw $this->createNotFoundException(); |
||
| 71 | } |
||
| 72 | |||
| 112 |