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