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