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 / 14
CRAP
0.00% covered (danger)
0.00%
0 / 384
AmsLogService
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 14
4032.00
0.00% covered (danger)
0.00%
0 / 384
 __construct
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 3
 __clone
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 2
 getInstance
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 7
 createAmsLog
0.00% covered (danger)
0.00%
0 / 1
72.00
0.00% covered (danger)
0.00%
0 / 19
 getAmsLogDetails
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 23
 getAmsLogById
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 27
 getAmsLogByType
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 27
 getAmsLogByName
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 27
 getAmsLogByColumnName
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 26
 getAmsLogByUserType
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 26
 getAmsLogByUserID
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 26
 updateAmsLog
0.00% covered (danger)
0.00%
0 / 1
506.00
0.00% covered (danger)
0.00%
0 / 77
 updateAmsLogByCreatedAt
0.00% covered (danger)
0.00%
0 / 1
56.00
0.00% covered (danger)
0.00%
0 / 47
 updateAmsLogById
0.00% covered (danger)
0.00%
0 / 1
72.00
0.00% covered (danger)
0.00%
0 / 47
<?php
namespace com\linways\core\ams\professional\service;
use com\linways\core\ams\professional\dto\AmsLog;
use com\linways\core\ams\professional\exception\ProfessionalException;
use com\linways\core\ams\professional\mapper\AmsLogServiceMapper;
use com\linways\core\ams\professional\util\UserUtil;
use Exception;
/**
 *
 * @Date 20/07/20
 * @author  Joel M John
 */
class AmsLogService extends BaseService
{
    /**
     * Presence of a static member variable
     *
     * @var null
     */
    private static $_instance = null;
    /**
     * Mapper variable
     * @var array
     */
    private $mapper = [];
    /**
     * Initialise mapper, logger, hooks here
     *
     * 
     */
    private function __construct()
    {
        $this->mapper = AmsLogServiceMapper::getInstance()->getMapper();
    }
    /**
     * Prevent any object or instance of that class to be cloned
     */
    private function __clone()
    {
    }
    /**
     * Have a single globally accessible static method
     *
     * @return AmsLogService|null
     */
    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;
    }
    /**
     * Creating AMS log
     * @param  Object $amsLog
     * @return Boolean
     * @throws ProfessionalException
     */
    public function createAmsLog($amsLog)
    {
        list($userType,$userID) = UserUtil::getAdminUserTypeAndId();
        try {
            if ($amsLog->type == NULL || $amsLog->name == NULL ||$amsLog->time == NULL || $amsLog->entityID == NULL) {
                throw new Exception("Fields cannot be NULL!");
            }
            $activities = array_filter((array) $amsLog, function($value) {    //filter null objects
                return !is_null($value) && $value !== ''; 
            });
            $activities = json_encode($activities);
            $sql = "INSERT INTO ams_log(activities, user_id, user_type) 
                    VALUES('$activities','$userID','$userType')";
            if($this->executeQueryForObject($sql,true)) {
                return true;
            }
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        return false;
    }
    /**
     * Select entire AMS log details
     * @return result AmsLogDetails
     * @throws ProfessionalException
     */
    public function getAmsLogDetails()
    {
        
        try {
            $sql = "SELECT id,
            JSON_EXTRACT(activities, '$.type') AS type,
            JSON_EXTRACT(activities, '$.name') AS name,
            JSON_EXTRACT(activities, '$.columnName') AS columnName,
            JSON_EXTRACT(activities, '$.fromValue') AS fromValue,
            JSON_EXTRACT(activities, '$.toValue') AS toValue,
            JSON_EXTRACT(activities, '$.entityName') AS entityName,
            JSON_EXTRACT(activities, '$.entityID') AS entityID,
            JSON_EXTRACT(activities, '$.message') AS message,
            JSON_EXTRACT(activities, '$.time') AS time,
            user_id,
            user_type,
            created_at
            FROM
                ams_log";
            
            $result = $this->executeQueryForList($sql, $this->mapper [AmsLogServiceMapper::SEARCH_AMS_LOG]);
            
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        return $result;
    }
    /**
     * Select AMS log details by Id
     * @param  Int $id
     * @return result AmsLogDetails
     * @throws ProfessionalException
     */
    public function getAmsLogById($id)
    {
        $amsLog = new AmsLog();
        $amsLog->id = $id;
        try {
            $sql = "SELECT id,
            JSON_EXTRACT(activities, '$.type') AS type,
            JSON_EXTRACT(activities, '$.name') AS name,
            JSON_EXTRACT(activities, '$.columnName') AS columnName,
            JSON_EXTRACT(activities, '$.fromValue') AS fromValue,
            JSON_EXTRACT(activities, '$.toValue') AS toValue,
            JSON_EXTRACT(activities, '$.entityName') AS entityName,
            JSON_EXTRACT(activities, '$.entityID') AS entityID,
            JSON_EXTRACT(activities, '$.message') AS message,
            JSON_EXTRACT(activities, '$.time') AS time,
            user_id,
            user_type,
            created_at
            FROM
                ams_log
            WHERE
                id = '$amsLog->id'";
            $result = $this->executeQueryForList($sql,$this->mapper [AmsLogServiceMapper::SEARCH_AMS_LOG]);
            
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        return $result;
    }
    /**
     * Select AMS log details by Type
     * @param  Object $amsObj
     * @return result AmsLogDetails
     * @throws ProfessionalException
     */
    public function getAmsLogByType($amsObj)
    {
        
        $amsLog = new AmsLog();
        $amsLog->type = $amsObj->type;
        try {
            $sql = "SELECT id,
            JSON_EXTRACT(activities, '$.type') AS type,
            JSON_EXTRACT(activities, '$.name') AS name,
            JSON_EXTRACT(activities, '$.columnName') AS columnName,
            JSON_EXTRACT(activities, '$.fromValue') AS fromValue,
            JSON_EXTRACT(activities, '$.toValue') AS toValue,
            JSON_EXTRACT(activities, '$.entityName') AS entityName,
            JSON_EXTRACT(activities, '$.entityID') AS entityID,
            JSON_EXTRACT(activities, '$.message') AS message,
            JSON_EXTRACT(activities, '$.time') AS time,
            user_id,
            user_type,
            created_at
            FROM
                ams_log
            WHERE
                JSON_EXTRACT(activities, '$.type') = '$amsLog->type'";
            $result = $this->executeQueryForList($sql,$this->mapper [AmsLogServiceMapper::SEARCH_AMS_LOG]);
            
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        return $result;
    }
    /**
     * Select AMS log details by Name
     * @param  Object $amsObj
     * @return result AmsLogDetails
     * @throws ProfessionalException
     */
    public function getAmsLogByName($amsObj)
    {
        $amsLog = new AmsLog();
        $amsLog->name = $amsObj->name;
        try {
            $sql = "SELECT id,
            JSON_EXTRACT(activities, '$.type') AS type,
            JSON_EXTRACT(activities, '$.name') AS name,
            JSON_EXTRACT(activities, '$.columnName') AS columnName,
            JSON_EXTRACT(activities, '$.fromValue') AS fromValue,
            JSON_EXTRACT(activities, '$.toValue') AS toValue,
            JSON_EXTRACT(activities, '$.entityName') AS entityName,
            JSON_EXTRACT(activities, '$.entityID') AS entityID,
            JSON_EXTRACT(activities, '$.message') AS message,
            JSON_EXTRACT(activities, '$.time') AS time,
            user_id,
            user_type,
            created_at
            FROM
                ams_log
            WHERE
                JSON_EXTRACT(activities, '$.name') = '$amsLog->name'";
            $result = $this->executeQueryForList($sql,$this->mapper [AmsLogServiceMapper::SEARCH_AMS_LOG]);
            
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        return $result;
    }
    /**
     * Select AMS log details by ColumnName
     * @param  object $amsObj
     * @return result AmsLogDetails
     * @throws ProfessionalException
     */
    public function getAmsLogByColumnName($amsObj)
    {
        
        $amsLog = new AmsLog();
        $amsLog->columnName = $amsObj->columnName;
        try {
            $sql = "SELECT id,
            JSON_EXTRACT(activities, '$.type') AS type,
            JSON_EXTRACT(activities, '$.name') AS name,
            JSON_EXTRACT(activities, '$.columnName') AS columnName,
            JSON_EXTRACT(activities, '$.fromValue') AS fromValue,
            JSON_EXTRACT(activities, '$.toValue') AS toValue,
            JSON_EXTRACT(activities, '$.entityName') AS entityName,
            JSON_EXTRACT(activities, '$.entityID') AS entityID,
            JSON_EXTRACT(activities, '$.message') AS message,
            JSON_EXTRACT(activities, '$.time') AS time,
            user_id,
            user_type,
            created_at
            FROM ams_log
            WHERE
                JSON_EXTRACT(activities, '$.columnName') = '$amsLog->columnName'";
            $result = $this->executeQueryForList($sql,$this->mapper [AmsLogServiceMapper::SEARCH_AMS_LOG]);
            
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        return $result;
    }
    /**
     * Select AMS log details by UserType
     * @param  object $amsObj
     * @return result AmsLogDetails
     * @throws ProfessionalException
     */
    public function getAmsLogByUserType($amsObj)
    {
        $amsLog = new AmsLog();
        $amsLog->user_type = $amsObj->user_type;
        try {
            $sql = "SELECT id,
            JSON_EXTRACT(activities, '$.type') AS type,
            JSON_EXTRACT(activities, '$.name') AS name,
            JSON_EXTRACT(activities, '$.columnName') AS columnName,
            JSON_EXTRACT(activities, '$.fromValue') AS fromValue,
            JSON_EXTRACT(activities, '$.toValue') AS toValue,
            JSON_EXTRACT(activities, '$.entityName') AS entityName,
            JSON_EXTRACT(activities, '$.entityID') AS entityID,
            JSON_EXTRACT(activities, '$.message') AS message,
            JSON_EXTRACT(activities, '$.time') AS time,
            user_id,
            user_type,
            created_at
            FROM ams_log
            WHERE
                user_type = '$amsLog->user_type'";
            $result = $this->executeQueryForList($sql,$this->mapper [AmsLogServiceMapper::SEARCH_AMS_LOG]);
            
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        return $result;
    }
    /**
     * Select AMS log details by UserID
     * @param  Integer $id
     * @return result AmsLogDetails
     * @throws ProfessionalException
     */
    public function getAmsLogByUserID($id)
    {
        //$id = SecurityUtils::getRandomString();
        //list($userType,$userID) = UserUtil::getUserTypeAndId();
        $amsLog = new AmsLog();
        $amsLog->user_id = $id;
        try {
            $sql = "SELECT id,
            JSON_EXTRACT(activities, '$.type') AS type,
            JSON_EXTRACT(activities, '$.name') AS name,
            JSON_EXTRACT(activities, '$.columnName') AS columnName,
            JSON_EXTRACT(activities, '$.fromValue') AS fromValue,
            JSON_EXTRACT(activities, '$.toValue') AS toValue,
            JSON_EXTRACT(activities, '$.entityName') AS entityName,
            JSON_EXTRACT(activities, '$.entityID') AS entityID,
            JSON_EXTRACT(activities, '$.message') AS message,
            JSON_EXTRACT(activities, '$.time') AS time,
            user_id,
            user_type,
            created_at
            FROM ams_log
            WHERE
                user_id = '$amsLog->user_id'";
            $result = $this->executeQueryForList($sql,$this->mapper [AmsLogServiceMapper::SEARCH_AMS_LOG]);
            
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        return $result;
    }
    
    /**
     * Update only selected Columns in AMS log details
     * @param  Object $amsObj
     * @return Boolean 
     * @throws ProfessionalException
     */
    public function updateAmsLog($amsObj)
    {
        
        $amsLog = new AmsLog();
        $amsLog->id = $amsObj->id;
        $amsLog->type = $amsObj->type;
        $amsLog->name = $amsObj->name;
        $amsLog->columnName = $amsObj->columnName;
        $amsLog->fromValue = $amsObj->fromValue;
        $amsLog->toValue = $amsObj->toValue;
        $amsLog->message = $amsObj->message;
        $amsLog->time = $amsObj->time;
        $amsLog->user_id = $amsObj->user_id;
        $amsLog->user_type = $amsObj->user_type; 
        try {
            $sql = "UPDATE ams_log 
                SET 
                activities = JSON_SET(activities
                ";
            $activityExist = 0;      //flag                  
            if($amsLog->type){
                $sql .=", '$.type','$amsLog->type";
                $activityExist = 1;
            }
            if($amsLog->name){
                $sql .=", '$.name','$amsLog->name";
                $activityExist = 1;
            }
            if($amsLog->columnName){
                $sql .=", '$.columnName','$amsLog->columnName";
                $activityExist = 1;
            }
            if($amsLog->fromValue){
                $sql .=", '$.fromValue','$amsLog->fromValue";
                $activityExist = 1;
            }
            if($amsLog->toValue){
                $sql .=", '$.toValue','$amsLog->toValue";
                $activityExist = 1;
            }
            if($amsLog->message){
                $sql .=", '$.message','$amsLog->message";
                $activityExist = 1;
            }
            if($amsLog->time){
                $sql .=", '$.time','$amsLog->time";
                $activityExist = 1;
            }
            if($activityExist == 1){
                $sql .= " )";
            }
            if($activityExist == 0){
                $sql = "UPDATE ams_log SET ";   
            }
            $sqlConditionFlag = 0;  //flag if more conditions exist for update
            if($amsLog->user_id && $activityExist ==1){
                $sql .=", user_id = '$amsLog->user_id" ;
                $sqlConditionFlag = 1;              
            }elseif($amsLog->user_id && $activityExist ==0){
                $sql .=" user_id = '$amsLog->user_id" ;
                $sqlConditionFlag = 1;
            }
            if($amsLog->user_type && $sqlConditionFlag ==1){
                $sql .= ", user_type = '$amsLog->user_type";
            }elseif($amsLog->user_type && $activityExist == 1 && $sqlConditionFlag == 0 ){
                $sql .= "AND user_type = '$amsLog->user_type";
            }
            elseif($amsLog->user_type){
                $sql .= "user_type = '$amsLog->user_type";
            }
            $sql .= "WHERE id ='$amsLog->id";
            if($this->executeQueryForObject($sql,true)){
                return true;
            }
            
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        return false;
    }
    /**
     * Update AMS log details by CreatedAt
     * @param  Object $amsObj
     * @return Boolean 
     * @throws ProfessionalException
     */
    public function updateAmsLogByCreatedAt($amsObj)
    {
        
        $amsLog = new AmsLog();
        $amsLog->id = $amsObj->id;
        $amsLog->type = $amsObj->type;
        $amsLog->name = $amsObj->name;
        $amsLog->columnName = $amsObj->columnName;
        $amsLog->fromValue = $amsObj->fromValue;
        $amsLog->toValue = $amsObj->toValue;
        $amsLog->message = $amsObj->message;
        $amsLog->time = $amsObj->time;
        $amsLog->user_id = $amsObj->user_id;
        $amsLog->user_type = $amsObj->user_type; 
        $amsLog->created_at = $amsObj->created_at;
        try {
            if ($amsLog->type == NULL || $amsLog->name == NULL||$amsLog->columnName == NULL ||
                $amsLog->time == NULL) {
                throw new Exception("Fields cannot be NULL!");
            }
            $sql = "UPDATE ams_log 
            SET 
                activities = JSON_SET(activities,
                '$.type',
                '$amsLog->type',
                '$.name',
                '$amsLog->name',
                '$.columnName',
                '$amsLog->columnName',
                '$.fromValue',
                '$amsLog->fromValue',
                '$.toValue',
                '$amsLog->toValue',
                '$.message',
                '$amsLog->message',
                '$.time',
                '$amsLog->time'),
                user_id = '$amsLog->user_id',
                user_type = '$amsLog->user_type'
            WHERE
                created_at = '$amsLog->created_at";
            if($this->executeQueryForObject($sql,true)){
                return true;
            } 
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        return false;
    }
    /**
     * Update AMS log details by Id
     * @param  Object $amsObj
     * @return Boolean 
     * @throws ProfessionalException
     */
    public function updateAmsLogById($amsObj)
    {
        
        $amsLog = new AmsLog();
        $amsLog->id = $amsObj->id;
        $amsLog->type = $amsObj->type;
        $amsLog->name = $amsObj->name;
        $amsLog->columnName = $amsObj->columnName;
        $amsLog->fromValue = $amsObj->fromValue;
        $amsLog->toValue = $amsObj->toValue;
        $amsLog->message = $amsObj->message;
        $amsLog->time = $amsObj->time;
        $amsLog->user_id = $amsObj->user_id;
        $amsLog->user_type = $amsObj->user_type; 
        $amsLog->created_at = $amsObj->created_at;
        try {
            if ($amsLog->id == NULL || $amsLog->type == NULL || $amsLog->name == NULL||$amsLog->columnName == NULL ||
                $amsLog->time == NULL) {
                throw new Exception("Fields cannot be NULL!");
            }
            $sql = "UPDATE ams_log 
            SET 
                activities = JSON_SET(activities,
                '$.type',
                '$amsLog->type',
                '$.name',
                '$amsLog->name',
                '$.columnName',
                '$amsLog->columnName',
                '$.fromValue',
                '$amsLog->fromValue',
                '$.toValue',
                '$amsLog->toValue',
                '$.message',
                '$amsLog->message',
                '$.time',
                '$amsLog->time'),
                user_id = '$amsLog->user_id',
                user_type = '$amsLog->user_type'
            WHERE
                id = '$amsLog->id";
            if($this->executeQueryForObject($sql,true)){
                return true;
            } 
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        return false;
    }
}