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