我认为我的代码逻辑有错误,因为有两个连续的 elseif 块具有几乎相同的代码:
//first elseif
elseif (!$socialUser && empty($siteUserId)) {
//first time user
$secretWord = $this->RandomString->getRand(7);
$data = array('User'=> array(
'username' => $this->userData['username'],
'password' => $this->Auth->password($secretWord),
'email' => $this->userData['email'],
'name' => $this->userData['name']
));
$siteUserId = $this->_addSiteUser($data);
if ($siteUserId){
$data = array('SocialUser' => array(
'title' => 'facebook',
'identifier' => $this->FB_userId,
'user_id' => $siteUserId
));
if ($this->_addSocialUser($data)){
$this->Auth->login($siteUserId);
$l = $this->Session->read('Auth.redirect');
if (empty($l)) $l = array('controller' => 'qurans', 'action' => 'index');
$this->controller->Session->setFlash(__('You are logined using Facebook Sucessfully!'.$this->userData['name'], true).' '.$secretWord, 'done_msg');
$this->Session->delete('Auth.redirect');
$this->controller->redirect($l);
}
else{
$this->controller->Session->setFlash(__('There is an error during adding you as a social member. Contact admin!',true), 'error_msg');
// $this->controller->redirect($this->Auth->loginAction);
$this->logout();
$this->controller->redirect(array('controller' => 'qurans', 'action' => 'index'));
}
}
}
//second elseif
elseif($socialUser && empty($siteUserId)){
$secretWord = $this->RandomString->getRand(7);
$data = array('User'=> array(
'username' => $this->userData['username'],
'password' => $this->Auth->password($secretWord),
'email' => $this->userData['email'],
'name' => $this->userData['name']
));
$siteUserId = $this->_addSiteUser($data);
if ($siteUserId){
//HERE IS ONLY THE DIFFERENCE
$data = $socialUser;
$data['SocialUser']['user_id'] = $siteUserId;
//DIFFERENCE END HERE
if ($this->_addSocialUser($data)){
$this->Auth->login($siteUserId);
$l = $this->Session->read('Auth.redirect');
if (empty($l)) $l = array('controller' => 'qurans', 'action' => 'index');
$this->controller->Session->setFlash(__('You are logined using Facebook Sucessfully!'.$this->userData['name'], true).' '.$secretWord, 'done_msg');
$this->Session->delete('Auth.redirect');
$this->controller->redirect($l);
}
else{
$this->controller->Session->setFlash(__('There is an error during adding you as a social member. Contact admin!',true), 'error_msg');
// $this->controller->redirect($this->Auth->loginAction);
$this->logout();
$this->controller->redirect(array('controller' => 'qurans', 'action' => 'index'));
}
}
}
我认为代码工作正常,但我对两个连续 elseif 块之间的复制和粘贴代码块感觉不好?有什么想法可以改进此代码吗?还是很好?