Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 33
OPACLibraryService
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 7
90.00
0.00% covered (danger)
0.00%
0 / 33
 __construct
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 1
 __clone
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 1
 getInstance
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 4
 saveOpacLibraryPassword
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 4
 checkOpacLibraryPassword
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 8
 generateOpacLibraryAccessToken
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 11
 validateOpacLibraryAccessToken
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 4
<?php
    namespace com\linways\core\ams\professional\service;
    use Firebase\JWT\JWT;
    use com\linways\core\ams\professional\dto\SettingsConstents;
    class OPACLibraryService extends BaseService {
        private static $_instance = null;
        
        // /Condition 2 - Locked down the constructor
        private function __construct() {
        }
        // Prevent any oustide instantiation of this class
        
        // /Condition 3 - Prevent any object or instance of that class to be cloned
        private function __clone() {
        }
        // Prevent any copy of this object
        
        // /Condition 4 - Have a single globally accessible static method
        public static function getInstance() {
            if (! is_object ( self::$_instance )) // or if( is_null(self::$_instance) ) or if( self::$_instance == null )
                self::$_instance = new self ();
            
            return self::$_instance;
        }
        
        public function saveOpacLibraryPassword($password){
            require_once getenv('NUCLEUS_CONF');
            $salt = $GLOBALS['GENERAL_SALT']; //From nucleus conf
            
            CommonService::getInstance()->updateSettings(SettingsConstents::OPAC_LIBRARY,SettingsConstents::OPAC_LIBRARY_PASSWORD, md5($password.$salt));
        }
        public function checkOpacLibraryPassword($password){
            require_once getenv('NUCLEUS_CONF');
            $salt = $GLOBALS['GENERAL_SALT']; //From nucleus conf
            $calculatedPassword = md5($password.$salt);
            $originalPassword = CommonService::getInstance()->getSettings(SettingsConstents::OPAC_LIBRARY, SettingsConstents::OPAC_LIBRARY_PASSWORD);
            if($originalPassword === $calculatedPassword)
                return true;
            else return false;
        }
        public function generateOpacLibraryAccessToken(){
            // Create constants if necessary
            $password = CommonService::getInstance()->getSettings(SettingsConstents::OPAC_LIBRARY, SettingsConstents::OPAC_LIBRARY_PASSWORD);
            global $COLLEGE_CODE;
            $payload = array(
                "col" => $COLLEGE_CODE,
                "rand" => bin2hex(openssl_random_pseudo_bytes(16)),
                "iss" => "https://linways.com",
                "exp" => time() + (30 * 24 * 60 * 60), // expires in 30 days
                "iat" => time()
            );
            return JWT::encode($payload, $password );
        }
        public function validateOpacLibraryAccessToken($token){
            $password = CommonService::getInstance()->getSettings(SettingsConstents::OPAC_LIBRARY, SettingsConstents::OPAC_LIBRARY_PASSWORD);
            JWT::$leeway = 60;  // $leeway in seconds, This is optional
            return (array)JWT::decode($token, $password, array('HS256'));
        }
    }