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 / 71
BaseService
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 7
702.00
0.00% covered (danger)
0.00%
0 / 71
 establishPdoConnection
0.00% covered (danger)
0.00%
0 / 1
20.00
0.00% covered (danger)
0.00%
0 / 12
 establishConnection
0.00% covered (danger)
0.00%
0 / 1
20.00
0.00% covered (danger)
0.00%
0 / 12
 setDBConnectionProperties
0.00% covered (danger)
0.00%
0 / 1
42.00
0.00% covered (danger)
0.00%
0 / 16
 setPdoDBConnectionProperties
0.00% covered (danger)
0.00%
0 / 1
42.00
0.00% covered (danger)
0.00%
0 / 16
 start_transaction
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 5
 commit_transaction
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 5
 rollBack_transaction
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 5
<?php
namespace com\linways\core\ams\professional\service;
use com\linways\base\connection\MySqlConnector;
use com\linways\base\connection\MySqlQuery;
use com\linways\base\connection\PdoConnector;
use com\linways\core\ams\professional\exception\ProfessionalException;
                
 class BaseService extends MySqlQuery 
{
    /**
     * update to true when begin transection 
     * @var Boolean
     */
    public $begin = false;
    /**
     * update to true when begin transection 
     * @var Boolean
     */
    public $commit = false;
    /**
     * update to true when begin transection 
     * @var Boolean
     */
    public $rollBack = false;
    /**
     * 
     * @var MySqlConnector
     */
    public $mySqlConnector;
    /**
     * 
     * @var PdoConnector
     */
    public $pdoConnector;
    
    /**
     * Establish a PDO Connection
     * If connection and db properties not set this method 
     * help to establish the connection.
     * This will be called from MySqlQuery or also you can call externally
     */
    public function establishPdoConnection()
    {
        $this->pdoConnector = PdoConnector::getInstance();
        /**
         * This condition is mandatory to check multiple database connections.
         * If not added then connection will be reused from the first project ie projectA service
         * invoked first then pdoconnection will established based on the projectA DB configuration
         * after that if we invoke projectB service pdoconnection will be resused from ProjectA bz of
         * singleton instance of pdoconnection
         * to avoid this situation we need to set 'instanceof' attribute value in setDBConnectionProperties() method
         */
        if($this->pdoConnector->instanceOf  != null && !$this->pdoConnector->instanceOf  instanceof BaseService)
        {
            $this->pdoConnector->closeConnection();
        }
        if(!$this->pdoConnector->connection)
        {
        
            $this->setPdoDBConnectionProperties();
        }
        $this->pdoConnection = $this->pdoConnector->getConnection();
    }
    /**
     * Establish a MYSQL Connection
     * If connection and db properties not set this method 
     * help to establish the connection.
     * This will be called from MySqlQuery or also you can call externally
     */
    public function establishConnection()
    {
        $this->mySqlConnector = MySqlConnector::getInstance();
        /**
         * This condition is mandatory to check multiple database connections.
         * If not added then connection will be reused from the first project ie projectA service
         * invoked first then mysqlconnection will established based on the projectA DB configuration
         * after that if we invoke projectB service mysqlConnection will be resused from ProjectA bz of
         * singleton instance of mysqlconnection
         * to avoid this situation we need to set 'instanceof' attribute value in setDBConnectionProperties() method
         */
        if($this->mySqlConnector->instanceOf  != null && !$this->mySqlConnector->instanceOf  instanceof BaseService)
        {
            $this->mySqlConnector->closeConnection();
        }
        if(!$this->mySqlConnector->connection)
        {
        
            $this->setDBConnectionProperties();
        }
        $this->connection = $this->mySqlConnector->getConnection();
    }
    /**
     * Set DB Connection properties
     */
    protected function setDBConnectionProperties()
    {
        $this->mySqlConnector->instanceOf = $this;
        // check DB configurations
        $dbconfigFilePath = getenv('DB_PROFESIONAL_CONFIG');
        
        //check file configured correctly 
        if(empty($dbconfigFilePath))
        {
            throw new ProfessionalException(ProfessionalException::DB_CONFIG_FILE_PATH_NOT_DEFINED_IN_SERVER,"Professional database configuration file 'db_prof_conf.php' path not configured in server conf!.");
        }
        //include db configuration file to load connection properties
        require $dbconfigFilePath;
//         global  $DB_HOST,$DB_USER,$DB_PASSWD,$DB_NAME;
        if (empty ( $DB_HOST ) || empty ( $DB_USER ) || empty ( $DB_PASSWD ) || empty ( $DB_NAME )) {
            throw new ProfessionalException ( ProfessionalException::DB_CONFIG_NOT_SET, "Professional  DB configuration not set properly" );
        }
        
        //Set DB Connection properties
        $this->mySqlConnector->DB_HOST = $DB_HOST;
        $this->mySqlConnector->DB_USER = $DB_USER;
        $this->mySqlConnector->DB_PASSWD = $DB_PASSWD;
        $this->mySqlConnector->DB_NAME = $DB_NAME;
    }
    /**
     * Set DB Connection properties
     */
    protected function setPdoDBConnectionProperties()
    {
        $this->pdoConnector->instanceOf = $this;
        // check DB configurations
        $dbconfigFilePath = getenv('DB_PROFESIONAL_CONFIG');
        
        //check file configured correctly 
        if(empty($dbconfigFilePath))
        {
            throw new ProfessionalException(ProfessionalException::DB_CONFIG_FILE_PATH_NOT_DEFINED_IN_SERVER,"Professional database configuration file 'db_prof_conf.php' path not configured in server conf!.");
        }
        //include db configuration file to load connection properties
        require $dbconfigFilePath;
//         global  $DB_HOST,$DB_USER,$DB_PASSWD,$DB_NAME;
        if (empty ( $DB_HOST ) || empty ( $DB_USER ) || empty ( $DB_PASSWD ) || empty ( $DB_NAME )) {
            throw new ProfessionalException ( ProfessionalException::DB_CONFIG_NOT_SET, "Professional  DB configuration not set properly" );
        }
        
        //Set DB Connection properties
        $this->pdoConnector->DB_HOST = $DB_HOST;
        $this->pdoConnector->DB_USER = $DB_USER;
        $this->pdoConnector->DB_PASSWD = $DB_PASSWD;
        $this->pdoConnector->DB_NAME = $DB_NAME;
    }
    function start_transaction(){
        if(!$this->begin){
            $this->beginTransaction();
            $this->begin = true;
        }
    }
    function commit_transaction(){
        if(!$this->commit){
            $this->commit();
            $this->begin = false;
        }
    }
    function rollBack_transaction(){
        if(!$this->rollBack){
            $this->begin = false;
            $this->rollBack();
        }
    }
}