| Total Complexity | 55 |
| Total Lines | 406 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like NfgController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use NfgController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class NfgController extends Controller |
||
| 26 | { |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @Route("/create", name="old_nfg_create") |
||
| 30 | */ |
||
| 31 | public function createAction() |
||
| 32 | { |
||
| 33 | return $this->toNfg('nfg_url_auth', 'nfg_createback'); |
||
| 34 | } |
||
| 35 | |||
| 36 | protected function toNfg($url, $callback, $useSession = false) |
||
| 37 | { |
||
| 38 | $nfg = $this->get('procergs_logincidadao.nfgws'); |
||
| 39 | $parm['accessid'] = $nfg->obterAccessID(); |
||
|
|
|||
| 40 | if ($useSession) { |
||
| 41 | $this->getRequest() |
||
| 42 | ->getSession() |
||
| 43 | ->set('ticketacessologin', $parm['accessid']); |
||
| 44 | } |
||
| 45 | $parm['urlretorno'] = $this->generateUrl( |
||
| 46 | $callback, |
||
| 47 | array(), |
||
| 48 | UrlGeneratorInterface::ABSOLUTE_URL |
||
| 49 | ); |
||
| 50 | // $url = $this->container->getParameter('nfg_url_auth') . '?' . http_build_query($parm); |
||
| 51 | $url = $this->container->getParameter($url).'?accessid='.$parm['accessid'].'&urlretorno='.$parm['urlretorno']; |
||
| 52 | |||
| 53 | //IE referer stuff, dont kill me |
||
| 54 | return new Response( |
||
| 55 | '<html><head><meta name="referrer" content="always"/></head><body><script type="text/javascript">document.location= "'.$url.'";</script></body></html>' |
||
| 56 | ); |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @Route("/create/back", name="old_nfg_createback") |
||
| 61 | */ |
||
| 62 | public function createBackAction(Request $request) |
||
| 167 | } |
||
| 168 | |||
| 169 | protected function checkAccessToken($voterRegistration = null) |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @Route("/login", name="old_nfg_login") |
||
| 198 | */ |
||
| 199 | public function loginAction() |
||
| 200 | { |
||
| 201 | return $this->toNfg('nfg_url_login', 'nfg_loginback', true); |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @Route("/login/back", name="old_nfg_loginback") |
||
| 206 | */ |
||
| 207 | public function loginBacktAction(Request $request) |
||
| 208 | { |
||
| 209 | /** @var MeuRSHelper $meursHelper */ |
||
| 210 | $meursHelper = $this->get('meurs.helper'); |
||
| 211 | |||
| 212 | $cpf = $request->get('cpf'); |
||
| 213 | $accessid = $request->get('accessid'); |
||
| 214 | $prsec = $request->get('prsec'); |
||
| 215 | if (null == $accessid || null == $cpf || null == $prsec) { |
||
| 216 | throw new NfgException('nfg.corrupted.callback'); |
||
| 217 | } |
||
| 218 | $sig = hash_hmac( |
||
| 219 | 'sha256', |
||
| 220 | "$cpf$accessid", |
||
| 221 | $this->container->getParameter('nfg_hmac_secret') |
||
| 222 | ); |
||
| 223 | if (false == $sig || strcmp(strtoupper($sig), $prsec) !== 0) { |
||
| 224 | throw new NfgException('nfg.corrupted.callback'); |
||
| 225 | } |
||
| 226 | if ($request->getSession()->get('ticketacessologin') != $accessid) { |
||
| 227 | throw new NfgException('nfg.accessid.mismatch'); |
||
| 228 | } |
||
| 229 | $cpf = str_pad($cpf, 11, "0", STR_PAD_LEFT); |
||
| 230 | $em = $this->getDoctrine()->getManager(); |
||
| 231 | $personRepo = $em->getRepository('LoginCidadaoCoreBundle:Person'); |
||
| 232 | $user = $personRepo->findOneBy( |
||
| 233 | array( |
||
| 234 | 'cpf' => $cpf, |
||
| 235 | ) |
||
| 236 | ); |
||
| 237 | |||
| 238 | if ($user instanceof PersonInterface) { |
||
| 239 | $personMeuRS = $meursHelper->getPersonMeuRS($user, true); |
||
| 240 | } else { |
||
| 241 | $personMeuRS = null; |
||
| 242 | } |
||
| 243 | |||
| 244 | if (!$user || !$personMeuRS->getNfgAccessToken()) { |
||
| 245 | throw new NfgException('nfg.user.notfound'); |
||
| 246 | } |
||
| 247 | $response = $this->redirect($this->generateUrl('lc_home')); |
||
| 248 | try { |
||
| 249 | $loginManager = $this->container->get('fos_user.security.login_manager'); |
||
| 250 | $firewallName = $this->container->getParameter('fos_user.firewall_name'); |
||
| 251 | $loginManager->loginUser($firewallName, $user, $response); |
||
| 252 | } catch (AccountStatusException $ex) { |
||
| 253 | // We simply do not authenticate users which do not pass the user |
||
| 254 | // checker (not enabled, expired, etc.). |
||
| 255 | } |
||
| 256 | |||
| 257 | return $response; |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @Route("/bind", name="old_nfg_bind") |
||
| 262 | */ |
||
| 263 | public function bindAction() |
||
| 264 | { |
||
| 265 | return $this->toNfg('nfg_url_auth', 'nfg_bindback'); |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @Route("/bind/back", name="old_nfg_bindback") |
||
| 270 | */ |
||
| 271 | public function bindBackAction(Request $request) |
||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * @return MeuRSHelper |
||
| 352 | */ |
||
| 353 | private function getMeuRSHelper() |
||
| 354 | { |
||
| 355 | return $this->get('meurs.helper'); |
||
| 356 | } |
||
| 357 | |||
| 358 | protected function checkOtherPerson(&$result1, &$em, &$personRepo) |
||
| 359 | { |
||
| 360 | $otherPerson = $personRepo->findOneBy( |
||
| 361 | array( |
||
| 362 | 'cpf' => $result1['CodCpf'], |
||
| 363 | ) |
||
| 364 | ); |
||
| 365 | if (!$otherPerson) { |
||
| 366 | return; |
||
| 367 | } |
||
| 368 | |||
| 369 | if ($otherPerson->getNfgAccessToken()) { |
||
| 370 | $this->solveConflict($result1, $otherPerson); |
||
| 371 | } else { |
||
| 372 | if ($result1['CodNivelAcesso'] == 1) { |
||
| 373 | throw new NfgException( |
||
| 374 | 'notification.nfg.already.cpf.but.weak', |
||
| 375 | NfgException::E_BIND |
||
| 376 | ); |
||
| 377 | } else { |
||
| 378 | $this->notifyAndClearCpfAndNfg($otherPerson); |
||
| 379 | } |
||
| 380 | } |
||
| 381 | } |
||
| 382 | |||
| 383 | private function solveConflict($thisPerson, Person $otherPerson) |
||
| 399 | ); |
||
| 400 | } |
||
| 401 | } |
||
| 402 | |||
| 403 | private function notifyAndClearCpfAndNfg(Person $person) |
||
| 404 | { |
||
| 405 | $person->setCpf(null); |
||
| 406 | $person->setNfgAccessToken(null); |
||
| 407 | $person->setNfgProfile(null); |
||
| 408 | //@TODO do no use updateUser |
||
| 409 | $this->container->get('fos_user.user_manager')->updateUser($person); |
||
| 410 | // TODO: notify user |
||
| 411 | } |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @Route("/unbind", name="old_nfg_unbind") |
||
| 415 | */ |
||
| 416 | public function unbindAction() |
||
| 431 | } |
||
| 432 | } |
||
| 433 |