From 853167d08e2dd645524c078ab8b71101c5ceb317 Mon Sep 17 00:00:00 2001
From: PavelBegunkov <asml.Silence@gmail.com>
Date: Thu, 25 Dec 2014 21:59:34 +0300
Subject: [PATCH] php refactoring

---
 .../account/classes/Kohana/Account.php        | 100 ++++++++-------
 .../modules/account/classes/Kohana/User.php   | 115 ++++++++----------
 .../account/classes/Model/Kohana/Account.php  |  20 +--
 3 files changed, 121 insertions(+), 114 deletions(-)

diff --git a/~dev_rating/modules/account/classes/Kohana/Account.php b/~dev_rating/modules/account/classes/Kohana/Account.php
index 845111b45..5667090c7 100644
--- a/~dev_rating/modules/account/classes/Kohana/Account.php
+++ b/~dev_rating/modules/account/classes/Kohana/Account.php
@@ -42,72 +42,82 @@ class Kohana_Account {
     public function createTeacher($lastName, $firstName, $secondName, $degreeID, $departamentID)
     {
         $activationCode = $this->generateActivationCode();
-        $response = $this->_model->createTeacher($lastName, $firstName, $secondName, $degreeID, $departamentID, $activationCode);
-        if($response == -1)
-            return -1;
-        else
-            return $activationCode;
+        $response = $this->_model->
+                    createTeacher(  $lastName, $firstName, 
+                                    $secondName, $degreeID, 
+                                    $departamentID, $activationCode);
+        if ($response === -1) {
+            $activationCode = -1;
+        }
+        return $activationCode;
     }
     
     public function createTeacherByDepName($lastName, $firstName, $secondName, $departamentName)
     {
         $activationCode = $this->generateActivationCode();
-        $response = $this->_model->createTeacherByDepName($lastName, $firstName, $secondName, $departamentName, $activationCode);
-        if($response == -1)
-            return -1;
-        else
-            return $activationCode;
+        $response = $this->_model->
+                    createTeacherByDepName( $lastName, $firstName, 
+                                            $secondName, $departamentName, 
+                                            $activationCode);
+        if ($response === -1) {
+            $activationCode = -1;
+        }
+        return $activationCode;
     }
     
-    public function createStudent($lastName, $firstName, $secondName, $grade, $groupNum, $facultyID)
+    public function createStudent(  $lastName, $firstName, $secondName, 
+                                    $grade, $groupNum, $facultyID)
     {
         $activationCode = $this->generateActivationCode();
-        $response = $this->_model->createStudent($lastName, $firstName, $secondName, $grade, $groupNum, $facultyID, $activationCode);
-        if($response == -1)
-            return -1;
-        else
-            return $activationCode;
+        $response = $this->_model->
+                    createStudent(  $lastName, $firstName, $secondName, 
+                                    $grade, $groupNum, $facultyID, 
+                                    $activationCode);
+        if ($response === -1) {
+            $activationCode = -1;
+        }
+        return $activationCode;
     }
 	
     public function createStudentEx($lastName, $firstName, $secondName, $studentGradeNum, $studentGroupNum, $studentDegree, $studentSpec, $facultyID)
     {
         $activationCode = $this->generateActivationCode();
         $response = $this->_model->createStudentEx($lastName, $firstName, $secondName, $studentGradeNum, $studentGroupNum, $studentDegree, $studentSpec, $facultyID, $activationCode);
-        if($response == -1)
-            return -1;
-        else
-            return $activationCode;
+        if ($response === -1) {
+            $activationCode = -1;
+        }
+        return $activationCode;
     }
 
     public function createSubject($name, $abbr, $facultyID)
     {
-        $response = $this->_model->createSubject($name, $abbr, $facultyID);
-        if($response == -1)
-            return -1;
-        else
-            return $activationCode;
+        $response = $this->_model->
+                    createSubject($name, $abbr, $facultyID);
+        if ($response === -1) {
+            $activationCode = -1;
+        }
+        return $activationCode;
     }
     
     private function checkTokenLifetime($creationDate)
     {
         $config = Kohana::$config->load('security.securityPolicy');
-        return (time() - $creationDate) > $config['recoveryToken']['lifetime'];
+        $lifetime = $config['recoveryToken']['lifetime'];
+        return (time() - $creationDate) > $lifetime;
     }
 
 
     private function checkRecoveryStatus($email)
     {
         $recovery = $this->_model->GetRecoveryInfoByEMail($email);
-        $recoveryHandled = array();
         $status = true;
-        foreach($recovery as $row)
-        {
-            if($this->checkTokenLifetime(strtotime($row['Date'])))
-            {
+        foreach($recovery as $row) {
+            $date = strtotime($row['Date']);
+            if($this->checkTokenLifetime($date)) {
                 $this->_model->useRecoveryToken($row['Token']);
-            }
-            else
+            } else {
                 $status = false;
+            }
         }
         return $status;
     }
@@ -116,14 +126,18 @@ class Kohana_Account {
     {
         $config = Kohana::$config->load('security.securityPolicy');
         $recovery = $this->_model->getRecoveryInfoByToken($token)->offsetGet(0);
-        if($recovery['isUsed'])
-            return false;
-        if($this->checkTokenLifetime(strtotime($recovery['Date'])))
-        {
-            $this->_model->useRecoveryToken($recovery['Token']);
-            return false;
+        $response = true;
+        
+        if ($recovery['isUsed']) {
+            $response = false;
+        } else {
+            $date = strtotime($recovery['Date']);
+            if($this->checkTokenLifetime($date)) {
+                $this->_model->useRecoveryToken($recovery['Token']);
+                $response = false;
+            }
         }
-        return true;
+        return $response;
     }
     
     public function createRecoveryRequest($email)
@@ -132,12 +146,14 @@ class Kohana_Account {
         if($this->checkRecoveryStatus($email))
         {
             $this->_model->createRecoveryToken($email, $requestToken);
-            $subject =  ASSEMBLY_SYSTEM_NAME.": Восстановление пароля"; 
+            $subject =  ASSEMBLY_SYSTEM_NAME.": Восстановление пароля";
+            
             $twig = Twig::factory('email/recovery');
             $twig->curl = URL::base(TRUE, 'https');
             $twig->Token = $requestToken;
             $twig->EMail = $email;
             $twig->Subject = $subject;
+            
             $message = $twig->render();
             $headers = 'MIME-Version: 1.0' . "\r\n";
             $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
@@ -183,6 +199,4 @@ class Kohana_Account {
         $response = $this->_model->changePassword($id, $newPassword);
         return $response != -1;
     }
-        
-    
 }
\ No newline at end of file
diff --git a/~dev_rating/modules/account/classes/Kohana/User.php b/~dev_rating/modules/account/classes/Kohana/User.php
index 135562dc6..644ee48fe 100644
--- a/~dev_rating/modules/account/classes/Kohana/User.php
+++ b/~dev_rating/modules/account/classes/Kohana/User.php
@@ -24,6 +24,23 @@ class Kohana_User implements ArrayAccess {
         return self::$_instance;
     }
     
+    private function setSessionLifetime() {
+            $session = &$this->_session;
+            $last_time = $session->get('last_time');
+            $cur_time = time();
+            $timeout = self::SESSION_LIFETIME;
+            if (isset($last_time) AND $last_time != null) {
+                $dif_time = $cur_time - $last_time;
+                if ($dif_time > $timeout) {
+                    $this->completeSignOut();
+                }
+            } else {
+                $dif_time = $timeout+10; 
+            }
+            $session->set('dif_time', $dif_time);
+            $session->set('last_time', $cur_time);    
+    }
+    
     private function __construct($config = array()) {
         $this->_config = $config;
         $this->_session = Session::instance();
@@ -32,29 +49,11 @@ class Kohana_User implements ArrayAccess {
         $this->_config['hash_key'] = $this->_model->getHashKey();
         $this->_config['hash_method'] = 'sha256';
         $isSignedIn = $this->isSignedIn();
-        if($isSignedIn)
-        {
+        if($isSignedIn) {
             $id = $this->_session->get('ID');
             $this->_userInfo = $this->_getInfoFromDB($id);
+            $this->setSessionLifetime();
         }
-
-
-        if ($isSignedIn) {
-            $last_time = $this->_session->get('last_time');
-            $cur_time = time();
-            $timeout = self::SESSION_LIFETIME;
-            if (isset($last_time) AND $last_time != null) {
-                $dif_time = $cur_time - $last_time;
-                if ($dif_time > $timeout) {
-                    $this->completeSignOut();
-                }
-                $this->_session->set('dif_time', $dif_time);
-            } else {
-                $this->_session->set('dif_time', $timeout+10); 
-            }
-            $this->_session->set('last_time', $cur_time);    
-        }
-
     }
     
     /**
@@ -69,28 +68,26 @@ class Kohana_User implements ArrayAccess {
      */          
     public function signUp($code, $email, $login, $password)
     {
-        if($this->_model->isActivationCodeValid($code))
-        {
-        	$isLogin = Account::instance()->isLoginExists($login);
-        	$isMail = Account::instance()->isMailExists($email);
-            if(!$isLogin && !$isMail)
-            {
-                $id = $this->_model->activateAccount($login, $password, $email, $code);
-                $this->completeSignIn($id, $this->hash($password));
-                return array(true, 'ok');
-            }
-            else
-            {
-                if($isLogin)
-                    return array(false, 'login_exists');
-                if($isMail)
-                    return array(false, 'mail_exists');
-            }
-        }
-        else
-        {
+        $model = &$this->_model;
+        $account = Account::instance();
+        
+        $isValid = $model->isActivationCodeValid($code);
+        if (!$isValid) {
             return array(false, 'invalid_code');
         }
+
+        $isLogin = $account->isLoginExists($login);
+        $isMail = $account->isMailExists($email);
+        
+        if ($isLogin) {
+            return array(false, 'login_exists');
+        } else if ($isMail) {
+            return array(false, 'mail_exists');
+        }
+        
+        $id = $model->activateAccount($login, $password, $email, $code);
+        $this->completeSignIn($id, $this->hash($password));
+        return array(true, 'ok');
     }
 
     /**
@@ -101,23 +98,24 @@ class Kohana_User implements ArrayAccess {
      * @return  bool
      */      
     public function signIn($login, $password) {
-        $id = $this->_model->checkAuth($login, $password);
-        if($id == -1)
+        $id = (int)$this->_model->checkAuth($login, $password);
+        if ($id === -1) {
             return false;
-        else
+        } else {
             return $this->completeSignIn($id, $this->hash($password));
+        }
     }
     
     protected function completeSignIn($id, $passhash) {
         $userHash = $this->hash($id.Request::$user_agent.Request::$client_ip).$this->_config['hash_key'];
-        $passhash = $this->hash($passhash.$this->_config['hash_key']);
-        Cookie::set('userhash', $passhash);
+        $passwordHash = $this->hash($passhash.$this->_config['hash_key']);
+        Cookie::set('userhash', $passwordHash);
         $this->_userInfo = $this->_getInfoFromDB($id);
         $this->_session->regenerate();
         $this->_session->set('ID', $id);
         $this->_session->set('LoggedIn', true);
         $this->_session->set('UserHash', $this->hash($userHash));
-        $this->_session->set('PasswordHash', $passhash);
+        $this->_session->set('PasswordHash', $passwordHash);
         return TRUE;
     }    
 
@@ -128,13 +126,9 @@ class Kohana_User implements ArrayAccess {
      */   
     public function isSignedIn()
     {
-        if($this->_session->get('LoggedIn'))
-        {
-            if(!$this->checkHash())
-            {
-                $this->completeSignOut();
-                return false;
-            }
+        $session = &$this->_session;
+        if($session->get('LoggedIn') && !$this->checkHash()) {
+            $this->completeSignOut();
         }
         return $this->_session->get('LoggedIn');
     }
@@ -155,15 +149,17 @@ class Kohana_User implements ArrayAccess {
      */    
     public function signOut()
     {
-        if($this->isSignedIn())
-        {
+        if($this->isSignedIn()) {
             return $this->completeSignOut();
         }
+        return FALSE;
     }
     
     protected function completeSignOut()
     {
-        $this->_session->set('ID', FALSE)->set('LoggedIn', FALSE)->set('UserHash', FALSE);
+        $this->_session ->set('ID', FALSE)
+                        ->set('LoggedIn', FALSE)
+                        ->set('UserHash', FALSE);
         Cookie::delete('userhash');
         unset($this->_userInfo);
         $this->_session->restart();
@@ -262,12 +258,9 @@ class Kohana_User implements ArrayAccess {
     // ---------------------------- [ARRAY] ------------------------------------
     
     public function offsetSet($offset, $value) {
-        if(isset($this->_userInfo[$offset]))
-        {
+        if(isset($this->_userInfo[$offset])) {
             return $this->_userInfo[$offset];
-        }
-        else
-        {
+        } else {
             throw new Kohana_Exception('Invalid key: '.$offset);
         }
     }
diff --git a/~dev_rating/modules/account/classes/Model/Kohana/Account.php b/~dev_rating/modules/account/classes/Model/Kohana/Account.php
index c834275bc..8c35309cb 100644
--- a/~dev_rating/modules/account/classes/Model/Kohana/Account.php
+++ b/~dev_rating/modules/account/classes/Model/Kohana/Account.php
@@ -5,8 +5,8 @@ class Model_Kohana_Account extends Model
     public function setHashKey($key)
     {
         $sql = "SELECT `SetHashKey`('$key') AS `Key`;";
-        $key = DB::query(Database::SELECT, $sql)->execute();
-        return $key->get('Key');
+        $res = DB::query(Database::SELECT, $sql)->execute();
+        return $res->get('Key');
     }
     
     public function getHashKey()
@@ -18,8 +18,8 @@ class Model_Kohana_Account extends Model
     
     public function checkAuth($login, $password) {
         $sql = "SELECT `SignIn`('$login', '$password') AS `ID`;";
-        $login = DB::query(Database::SELECT, $sql)->execute();
-        return $login->get('ID');
+        $res = DB::query(Database::SELECT, $sql)->execute();
+        return $res->get('ID');
     }
     
     public function ChangeTeacherInfo($id, $lastName, $firstName, $secondName, $degreeID, $departamentID)
@@ -102,15 +102,15 @@ class Model_Kohana_Account extends Model
     public function getAccNumByLogin($login)
     {
         $sql = "SELECT `GetAccCountByLogin`('$login') AS Num;";
-        $login = DB::query(Database::SELECT, $sql)->execute();
-        return $login->get('Num');
+        $res = DB::query(Database::SELECT, $sql)->execute();
+        return $res->get('Num');
     }
     
     public function getAccNumByMail($email)
     {
         $sql = "SELECT `GetAccCountByMail`('$email') AS Num;";
-        $email = DB::query(Database::SELECT, $sql)->execute();
-        return $email->get('Num');
+        $res = DB::query(Database::SELECT, $sql)->execute();
+        return $res->get('Num');
     }    
     
     public function isActivationCodeValid($code)
@@ -126,8 +126,8 @@ class Model_Kohana_Account extends Model
     public function createRecoveryToken($email, $token)
     {
         $sql = "SELECT `CreateRecoveryToken`('$email', '$token') AS Num;";
-        $email = DB::query(Database::SELECT, $sql)->execute();
-        return $email->get('Num');
+        $res = DB::query(Database::SELECT, $sql)->execute();
+        return $res->get('Num');
     }
     
     public function getRecoveryInfoByEMail($email)
-- 
GitLab