Conditions | 28 |
Paths | 2964 |
Total Lines | 180 |
Code Lines | 91 |
Lines | 8 |
Ratio | 4.44 % |
Changes | 15 | ||
Bugs | 5 | Features | 1 |
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 |
||
54 | public function onBootstrap(EventInterface $e) |
||
55 | { |
||
56 | // this is useful for zfr-cors to accept chrome extension like Postman |
||
57 | UriFactory::registerScheme('chrome-extension', 'Zend\Uri\Uri'); |
||
58 | |||
59 | $serviceManager = $e->getApplication()->getServiceManager(); |
||
60 | $config = $e->getApplication()->getServiceManager()->get('config'); |
||
61 | |||
62 | // Locale management |
||
63 | $translator = $serviceManager->get('translator'); |
||
64 | $defaultLocale = 'fr'; |
||
65 | |||
66 | // Gestion de la locale |
||
67 | if (PHP_SAPI !== 'cli') { |
||
68 | $config = $e->getApplication()->getServiceManager()->get('config'); |
||
69 | if (isset($config['playgroundLocale'])) { |
||
70 | $pgLocale = $config['playgroundLocale']; |
||
71 | $defaultLocale = $pgLocale['default']; |
||
72 | |||
73 | if (isset($pgLocale['strategies'])) { |
||
74 | $pgstrat = $pgLocale['strategies']; |
||
75 | |||
76 | // Is there a locale in the URL ? |
||
77 | if (in_array('uri', $pgstrat)) { |
||
78 | $path = $e->getRequest()->getUri()->getPath(); |
||
79 | $parts = explode('/', trim($path, '/')); |
||
80 | $localeCandidate = array_shift($parts); |
||
81 | // I switch from locale to... language |
||
82 | $localeCandidate = substr($localeCandidate, 0, 2); |
||
83 | |||
84 | if (in_array($localeCandidate, $pgLocale['supported'])) { |
||
85 | $locale = $localeCandidate; |
||
86 | } |
||
87 | } |
||
88 | |||
89 | // Is there a cookie for the locale ? |
||
90 | if (empty($locale) && in_array('cookie', $pgstrat)) { |
||
91 | $serviceManager->get('router')->setTranslator($translator); |
||
92 | if ($serviceManager->get('router')->match($serviceManager->get('request')) && |
||
93 | strpos($serviceManager->get('router')->match($serviceManager->get('request'))->getMatchedRouteName(), 'admin') !==false |
||
94 | ) { |
||
95 | View Code Duplication | if ($e->getRequest()->getCookie() && |
|
96 | $e->getRequest()->getCookie()->offsetExists('pg_locale_back') |
||
97 | ) { |
||
98 | $locale = $e->getRequest()->getCookie()->offsetGet('pg_locale_back'); |
||
99 | } |
||
100 | View Code Duplication | } else { |
|
101 | if ($e->getRequest()->getCookie() && |
||
102 | $e->getRequest()->getCookie()->offsetExists('pg_locale_frontend') |
||
103 | ) { |
||
104 | $locale = $e->getRequest()->getCookie()->offsetGet('pg_locale_frontend'); |
||
105 | } |
||
106 | } |
||
107 | } |
||
108 | |||
109 | // Is there a locale in the request Header ? |
||
110 | if (empty($locale) && in_array('header', $pgstrat)) { |
||
111 | if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { |
||
112 | $localeCandidate = \Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']); |
||
113 | // I switch from locale to... language |
||
114 | $localeCandidate = substr($localeCandidate, 0, 2); |
||
115 | if (in_array($localeCandidate, $pgLocale['supported'])) { |
||
116 | $locale = $localeCandidate; |
||
117 | } |
||
118 | } |
||
119 | } |
||
120 | } |
||
121 | // I take the default locale |
||
122 | if (empty($locale)) { |
||
123 | $locale = $defaultLocale; |
||
124 | } |
||
125 | } |
||
126 | |||
127 | // I take the default locale |
||
128 | if (empty($locale)) { |
||
129 | $locale = $defaultLocale; |
||
130 | } |
||
131 | |||
132 | $translator->setLocale($locale); |
||
133 | |||
134 | // Attach the translator to the router |
||
135 | $e->getRouter()->setTranslator($translator); |
||
136 | $e->getRouter()->setTranslatorTextDomain('routes'); |
||
137 | |||
138 | // Attach the translator to the plugins |
||
139 | $translate = $serviceManager->get('viewhelpermanager')->get('translate'); |
||
140 | $translate->getTranslator()->setLocale($locale); |
||
141 | |||
142 | $options = $serviceManager->get('playgroundcore_module_options'); |
||
143 | $options->setLocale($locale); |
||
144 | } |
||
145 | |||
146 | // positionnement de la langue pour les traductions de date avec strftime |
||
147 | setlocale(LC_TIME, "fr_FR", 'fr_FR.utf8', 'fra'); |
||
148 | |||
149 | AbstractValidator::setDefaultTranslator($translator, 'playgroundcore'); |
||
150 | |||
151 | /* |
||
152 | * Entity translation based on Doctrine Gedmo library |
||
153 | */ |
||
154 | $doctrine = $serviceManager->get('doctrine.entitymanager.orm_default'); |
||
155 | $evm = $doctrine->getEventManager(); |
||
156 | |||
157 | $translatableListener = new \Gedmo\Translatable\TranslatableListener(); |
||
158 | $translatableListener->setDefaultLocale($defaultLocale); |
||
159 | |||
160 | // If no translation is found, fallback to entity data |
||
161 | $translatableListener->setTranslationFallback(true); |
||
162 | |||
163 | // set Locale |
||
164 | if (!empty($locale)) { |
||
165 | $translatableListener->setTranslatableLocale($locale); |
||
166 | } |
||
167 | |||
168 | $evm->addEventSubscriber($translatableListener); |
||
169 | |||
170 | /** |
||
171 | * Adding a Filter to slugify a string (make it URL compliiant) |
||
172 | */ |
||
173 | $filterChain = new \Zend\Filter\FilterChain(); |
||
174 | $filterChain->getPluginManager()->setInvokableClass( |
||
175 | 'slugify', |
||
176 | 'PlaygroundCore\Filter\Slugify' |
||
177 | ); |
||
178 | $filterChain->attach(new Filter\Slugify()); |
||
179 | |||
180 | // Start the session container |
||
181 | $sessionConfig = new SessionConfig(); |
||
182 | $sessionConfig->setOptions($config['session']); |
||
183 | $sessionManager = new SessionManager($sessionConfig); |
||
184 | $sessionManager->start(); |
||
185 | |||
186 | /** |
||
187 | * Optional: If you later want to use namespaces, you can already store the |
||
188 | * Manager in the shared (static) Container (=namespace) field |
||
189 | */ |
||
190 | \Zend\Session\Container::setDefaultManager($sessionManager); |
||
191 | |||
192 | // Google Analytics : When the render event is triggered, we invoke the view helper to |
||
193 | // render the javascript code. |
||
194 | $e->getApplication()->getEventManager()->attach(\Zend\Mvc\MvcEvent::EVENT_RENDER, function (\Zend\Mvc\MvcEvent $e) use ($serviceManager) { |
||
195 | $view = $serviceManager->get('ViewHelperManager'); |
||
196 | $plugin = $view->get('googleAnalytics'); |
||
197 | $plugin(); |
||
198 | |||
199 | $pluginOG = $view->get('facebookOpengraph'); |
||
200 | $pluginOG(); |
||
201 | |||
202 | $pluginTC = $view->get('twitterCard'); |
||
203 | $pluginTC(); |
||
204 | }); |
||
205 | |||
206 | |||
207 | if (PHP_SAPI !== 'cli') { |
||
208 | $session = new Container('facebook'); |
||
209 | $fb = $e->getRequest()->getPost()->get('signed_request'); |
||
210 | if ($fb) { |
||
211 | $signedReq = explode('.', $fb, 2); |
||
212 | $payload = $signedReq[1]; |
||
213 | $data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true); |
||
214 | $session->offsetSet('signed_request', $data); |
||
215 | |||
216 | // This fix exists only for safari on Windows : we need to redirect the user to the page outside of iframe |
||
217 | // for the cookie to be accepted. Core just adds a 'redir_fb_page_id' var to alert controllers |
||
218 | // that they need to send the user back to FB... |
||
219 | |||
220 | if (!count($_COOKIE) > 0 && strpos($_SERVER['HTTP_USER_AGENT'], 'Safari')) { |
||
221 | echo '<script type="text/javascript">' . |
||
222 | 'window.top.location.href = window.location.href+"?redir_fb_page_id='. $data["page"]["id"]. '";' . |
||
223 | '</script>'; |
||
224 | } |
||
225 | |||
226 | // This fix exists only for IE6+, when this app is embedded into an iFrame : The P3P policy has to be set. |
||
227 | $response = $e->getResponse(); |
||
228 | if ($response instanceof \Zend\Http\Response && (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') || strpos($_SERVER['HTTP_USER_AGENT'], 'rv:11.'))) { |
||
229 | $response->getHeaders()->addHeaderLine('P3P:CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"'); |
||
230 | } |
||
231 | } |
||
232 | } |
||
233 | } |
||
234 | |||
432 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.