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