Conditions | 12 |
Paths | 25 |
Total Lines | 88 |
Code Lines | 54 |
Lines | 0 |
Ratio | 0 % |
Changes | 9 | ||
Bugs | 0 | Features | 2 |
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( |
||
41 | 'The parameter controller must be a valid callable controller, like "My\Namespace\Controller\Class::method"' |
||
42 | ); |
||
43 | } elseif (!class_exists($controller[0]) || !method_exists($controller[0], $controller[1])) { |
||
44 | throw new \LogicException( |
||
45 | sprintf( |
||
46 | 'Unable to find the "%s" controller or the method "%s" doesn\'t exist.', |
||
47 | $controller[0], |
||
48 | $controller[1] |
||
49 | ) |
||
50 | ); |
||
51 | } |
||
52 | |||
53 | /** Generating the alternate link for SEO */ |
||
54 | $seoHelper = $this->get('sonata.seo.page.default'); |
||
55 | $translatedPages = $entityManager->getRepository('AlpixelCMSBundle:Node')->findTranslations($node); |
||
56 | |||
57 | $router = $this->get('router'); |
||
58 | foreach ($translatedPages as $translation) { |
||
59 | $seoHelper->addLangAlternate( |
||
60 | $router->generate( |
||
61 | "alpixel_cms", |
||
62 | [ |
||
63 | 'slug' => $translation->getSlug(), |
||
64 | '_locale' => $translation->getLocale(), |
||
65 | ], |
||
66 | Router::ABSOLUTE_URL |
||
67 | ), |
||
68 | $translation->getLocale() |
||
69 | ); |
||
70 | } |
||
71 | |||
72 | return $this->forward( |
||
73 | $contentType['controller'], |
||
74 | [ |
||
75 | '_route' => $request->attributes->get('_route'), |
||
76 | '_route_params' => $request->attributes->get('_route_params'), |
||
77 | 'object' => $node, |
||
78 | ] |
||
79 | ); |
||
80 | } catch (\LogicException $e) { |
||
81 | if (!$this->container->get('kernel')->isDebug()) { |
||
82 | $logger = $this->get('logger'); |
||
83 | $logger->error($e->getMessage()); |
||
84 | } else { |
||
85 | throw $e; |
||
86 | } |
||
87 | } |
||
88 | } else { |
||
89 | //Trying to find another node with this slug, in another language |
||
90 | $node = $entityManager->getRepository('AlpixelCMSBundle:Node') |
||
91 | ->findOnePublishedBySlug($slug); |
||
92 | |||
93 | if ($node !== null) { |
||
94 | $translation = $entityManager->getRepository('AlpixelCMSBundle:Node') |
||
95 | ->findTranslation($node, $request->getLocale()); |
||
96 | if ($translation !== null) { |
||
97 | return $this->redirect( |
||
98 | $this->generateUrl( |
||
99 | 'alpixel_cms', |
||
100 | [ |
||
101 | 'slug' => $translation->getSlug(), |
||
102 | '_locale' => $translation->getLocale(), |
||
103 | ] |
||
104 | ) |
||
105 | ); |
||
106 | } |
||
107 | } |
||
108 | } |
||
109 | |||
110 | throw $this->createNotFoundException(); |
||
111 | } |
||
112 | |||
196 |