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
LibraryGatewayService
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
 saveGatewayPassword
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 4
 checkGatewayPassword
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 8
 generateGatewayAccessToken
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 11
 validateGatewayAccessToken
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 LibraryGatewayService 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 saveGatewayPassword($password){
        require_once getenv('NUCLEUS_CONF');
        $salt = $GLOBALS['GENERAL_SALT']; //From nucleus conf
        
            CommonService::getInstance()->updateSettings(SettingsConstents::LIBRARY_GATEWAY,SettingsConstents::LIBRARY_GATEWAY_PASSWORD, md5($password.$salt));
    }
    public function checkGatewayPassword($password){
        require_once getenv('NUCLEUS_CONF');
        $salt = $GLOBALS['GENERAL_SALT']; //From nucleus conf
        $calculatedPassword = md5($password.$salt);
        $originalPassword = CommonService::getInstance()->getSettings(SettingsConstents::LIBRARY_GATEWAY, SettingsConstents::LIBRARY_GATEWAY_PASSWORD);
        if($originalPassword === $calculatedPassword)
            return true;
        else return false;
    }
    public function generateGatewayAccessToken(){
        // Create constants if necessary
        $password = CommonService::getInstance()->getSettings(SettingsConstents::LIBRARY_GATEWAY, SettingsConstents::LIBRARY_GATEWAY_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 validateGatewayAccessToken($token){
        $password = CommonService::getInstance()->getSettings(SettingsConstents::LIBRARY_GATEWAY, SettingsConstents::LIBRARY_GATEWAY_PASSWORD);
        JWT::$leeway = 60;  // $leeway in seconds, This is optional
        return (array)JWT::decode($token, $password, array('HS256'));
    }
}