Conditions | 14 |
Paths | 54 |
Total Lines | 51 |
Code Lines | 27 |
Lines | 0 |
Ratio | 0 % |
Changes | 9 | ||
Bugs | 3 | 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 |
||
47 | public function setRequestLayout(GetResponseEvent $event) |
||
48 | { |
||
49 | $request = $event->getRequest(); |
||
50 | |||
51 | // Get the necessary informations to check them in layout configurations |
||
52 | $path = $request->getPathInfo(); |
||
53 | $host = $request->getHost(); |
||
54 | |||
55 | // As a layout must be set, we force it to be empty if no layout is properly configured. |
||
56 | // Then this will throw an exception, and the user will be warned of the "no-layout" config problem. |
||
57 | $finalLayout = null; |
||
58 | |||
59 | foreach ($this->layouts as $layoutConfig) { |
||
60 | $match = false; |
||
61 | |||
62 | // First check host |
||
63 | if ($layoutConfig['host'] && $host === $layoutConfig['host']) { |
||
64 | $match = true; |
||
65 | } |
||
66 | |||
67 | // Check pattern |
||
68 | if ($layoutConfig['pattern'] && preg_match('~' . $layoutConfig['pattern'] . '~', $path)) { |
||
69 | $match = true; |
||
70 | } |
||
71 | |||
72 | if ($match) { |
||
73 | $finalLayout = $layoutConfig; |
||
74 | break; |
||
75 | } |
||
76 | } |
||
77 | |||
78 | // If nothing matches, we take the first layout that has no "host" or "pattern" configuration. |
||
79 | if (null === $finalLayout) { |
||
80 | $layouts = $this->layouts; |
||
81 | do { |
||
82 | $finalLayout = array_shift($layouts); |
||
83 | if ($finalLayout['host'] || $finalLayout['pattern']) { |
||
84 | $finalLayout = null; |
||
85 | } |
||
86 | } while (null === $finalLayout && count($layouts)); |
||
87 | } |
||
88 | |||
89 | if (null === $finalLayout || !$this->templating->exists($finalLayout['resource'])) { |
||
90 | throw new \Twig_Error_Loader(sprintf( |
||
91 | 'Unable to find template %s for layout %s. The "layout" parameter must be a valid twig view to be used as a layout.', |
||
92 | $finalLayout['resource'], $finalLayout['name'] |
||
93 | ), 0, $finalLayout['resource']); |
||
94 | } |
||
95 | |||
96 | $event->getRequest()->attributes->set('_orbitale_cms_layout', $finalLayout); |
||
97 | } |
||
98 | } |
||
99 |