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 / 12
CRAP
0.00% covered (danger)
0.00%
0 / 242
ValuationMethodService
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 12
2862.00
0.00% covered (danger)
0.00%
0 / 242
 __construct
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 2
 saveValuationMethod
0.00% covered (danger)
0.00%
0 / 1
56.00
0.00% covered (danger)
0.00%
0 / 25
 validateSaveValuationMethodRequest
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 4
 insertValuationMethod
0.00% covered (danger)
0.00%
0 / 1
30.00
0.00% covered (danger)
0.00%
0 / 16
 updateValuationMethod
0.00% covered (danger)
0.00%
0 / 1
20.00
0.00% covered (danger)
0.00%
0 / 18
 deleteValuationMethod
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 15
 trashValuationMethod
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 19
 restoreValuationMethod
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 19
 searchValuationMethod
0.00% covered (danger)
0.00%
0 / 1
156.00
0.00% covered (danger)
0.00%
0 / 53
 deleteValuationMethodIdentityContext
0.00% covered (danger)
0.00%
0 / 1
20.00
0.00% covered (danger)
0.00%
0 / 21
 deleteValuationMethodByIdetifiyingContext
0.00% covered (danger)
0.00%
0 / 1
30.00
0.00% covered (danger)
0.00%
0 / 20
 getValuationMethodIdentityContext
0.00% covered (danger)
0.00%
0 / 1
20.00
0.00% covered (danger)
0.00%
0 / 30
<?php
namespace com\linways\core\ams\professional\service\academic;
use com\linways\base\util\SecurityUtils;
use com\linways\base\util\MakeSingletonTrait;
use com\linways\core\ams\professional\service\BaseService;
use com\linways\core\ams\professional\mapper\academic\ValuationMethodServiceMapper;
use com\linways\core\ams\professional\request\academic\SearchValuationMethodRequest;
use com\linways\core\ams\professional\constant\academic\ValuationMethodTypeConstant;
use com\linways\core\ams\professional\constant\academic\StatusConstants;
use com\linways\core\ams\professional\exception\ProfessionalException;
use com\linways\core\ams\professional\dto\api\ValuationMethod;
class ValuationMethodService extends BaseService
{
    use MakeSingletonTrait;
    private function __construct() {
        $this->mapper = ValuationMethodServiceMapper::getInstance()->getMapper();
    }
    /**
     * Save valuationMethod
     * @param ValuationMethod $valuationMethod
     * @return String $id
     */
    public function saveValuationMethod (ValuationMethod $valuationMethod)
    {
        $valuationMethod = $this->realEscapeObject($valuationMethod);
        $valuationMethod->createdBy = $GLOBALS['userId'] ?? $valuationMethod->createdBy;
        $valuationMethod->updatedBy = $GLOBALS['userId'] ?? $valuationMethod->updatedBy;
        try{
            $this->validateSaveValuationMethodRequest($valuationMethod);
            if(!empty($valuationMethod->id))
            {
                $valuationMethod->id = $this->updateValuationMethod($valuationMethod);
            }
            else
            {
                $valuationMethod->id = $this->insertValuationMethod($valuationMethod);
            }
        }catch(\Exception $e) {
            if($e->getCode() !== ProfessionalException::INVALID_PARAMETERS && $e->getCode() !== ProfessionalException::EMPTY_PARAMETERS && $e->getCode() !== ProfessionalException::DUPLICATE_ENTRY) {
                throw new ProfessionalException($e->getCode(),"Failed to save valuation method! Please try again");
            } else if ($e->getCode() === ProfessionalException::DUPLICATE_ENTRY) {
                throw new ProfessionalException (ProfessionalException::DUPLICATE_ENTRY,"Cannot create valuation method.".$valuationMethod->name." already exists!");
            } else {
                throw new ProfessionalException ($e->getCode(),$e->getMessage());
            }
        }
        return $valuationMethod->id;
    }
    /**
     * Validate ValuationMethod Request Before Saving
     * @param ValuationMethod $valuationMethod
     * @return NULL
     */
    private function validateSaveValuationMethodRequest($valuationMethod)
    {
        if(empty($valuationMethod->type))
            throw new ProfessionalException(ProfessionalException::EMPTY_PARAMETERS,"Valuation method type is empty! Please enter a type for valuation method");
        //TODO: Validate type if updating
    }
    /**
     * Insert valuationMethod
     * @param ValuationMethod $valuationMethod
     * @return String $id
     */
    private function insertValuationMethod(ValuationMethod $valuationMethod)
    {
        $name = !empty($valuationMethod->name) ? "'".$valuationMethod->name."'" : "NULL";
        $properties = !empty($valuationMethod->properties) ? "'".json_encode($valuationMethod->properties)."'" : "'{}'";
        $identifyingContext = !empty($valuationMethod->identifyingContext) ? "'" . json_encode($valuationMethod->identifyingContext) . "'" : "NULL";
        $id = SecurityUtils::getRandomString();
        $query = "INSERT INTO `valuation_method`
            (`id`,`identifying_context`,`type`,`name`,`properties`,`created_by`,`updated_by`)
            VALUES
            ('$id',$identifyingContext,'$valuationMethod->type',$name,$properties,'$valuationMethod->createdBy','$valuationMethod->updatedBy')";
        try {
            $this->executeQuery($query);
            return $id;
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(),$e->getMessage());
        }
    }
    /**
     * Update ValuationMethod
     * @param ValuationMethod $valuationMethod
     * @return NULL
     */
    private function updateValuationMethod(ValuationMethod $valuationMethod)
    {
        $properties = !empty($valuationMethod->properties) ? "'".json_encode($valuationMethod->properties)."'" : "NULL";
        $query = "UPDATE
                    `valuation_method`
                SET
                    `name` = ".(!empty($valuationMethod->name) ? "'".$valuationMethod->name."'" : "name").",
                    `type` = '$valuationMethod->type',
                    `properties` = $properties,
                    `updated_by` = '$valuationMethod->updatedBy'
                WHERE
                    `id` = '$valuationMethod->id'";
        try {
            $this->executeQuery($query);
            return $valuationMethod->id;
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * Delete ValuationMethod
     * @param String $id
     * @return NULL
     */
    public function deleteValuationMethod($id)
    {
        $id = $this->realEscapeString($id);
        if(empty($id)) {
            throw new ProfessionalException(ProfessionalException::EMPTY_PARAMETERS,"No valuation method selected! Please select a valuationMethod to delete");
        }
        $query = "DELETE FROM
                    `valuation_method`
                WHERE
                    `id` = '$id'";
        try {
            $this->executeQuery($query);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(),"Error deleting valuation method! Please try again");
        }
    }
    /**
     * Trash ValuationMethod (Soft Delete)
     * @param String $id
     * @return NULL
     */
    public function trashValuationMethod($id)
    {
        $id = $this->realEscapeString($id);
        $updatedBy = $GLOBALS['userId'];
        if(empty($id)) {
            throw new ProfessionalException(ProfessionalException::EMPTY_PARAMETERS,"No valuation method selected! Please select a valuationMethod to delete");
        }
        //TODO: Do validation before deleting
        $query = "UPDATE
                    `valuation_method`
                SET
                    `trashed` = UTC_TIMESTAMP(),
                    `updated_by` = '$updatedBy'
                WHERE
                    `id` = '$id'";
        try {
            $this->executeQuery($query);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(),"Error deleting valuation method! Please try again");
        }
    }
    /**
     * Restore ValuationMethod
     * @param String $id
     * @return NULL
     */
    public function restoreValuationMethod($id)
    {
        $id = $this->realEscapeString($id);
        $updatedBy = $GLOBALS['userId'];
        if(empty($id)) {
            throw new ProfessionalException(ProfessionalException::EMPTY_PARAMETERS,"No valuation method selected! Please select a valuationMethod to restore");
        }
        $query = "UPDATE
                    `valuation_method`
                SET
                    `trashed` = NULL,
                    `updated_by` = '$updatedBy'
                WHERE
                    `id` = '$id'";
        try {
            $this->executeQuery($query);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(),"Error restoring valuation method! Please try again");
        }
    }
    /**
     * Search ValuationMethod Details
     * @param SearchValuationMethodRequest $request
     * @return ValuationMethod
     */
    public function searchValuationMethod(SearchValuationMethodRequest $request)
    {
        $request = $this->realEscapeObject($request);
        $whereQuery = "";
        $limitQuery = "";
        if($request->trashed === StatusConstants::ACTIVE) {
            $whereQuery .= "";
        }
        if($request->trashed === StatusConstants::TRASHED) {
            $whereQuery .= "";
        }
        if(!empty($request->id)) {
            $whereQuery .= " AND vm.id='$request->id";
        }
        if(!empty($request->name)) {
            $whereQuery .= " AND  vm.name LIKE '%$request->name%' ";
        }
        if(!empty($request->identifyingContext)) {
            $identifyingContext = is_object($request->identifyingContext)
            ? "'" . json_encode($request->identifyingContext) . "'"
            : is_string($request->identifyingContext)
                ? "'$request->identifyingContext'"
                : "'{}'";
            $whereQuery .= " AND JSON_CONTAINS(vm.identifying_context, $identifyingContext, '$') ";
            if ($request->identifyingContext == FilterModeConstants::STRICT) {
                $whereQuery .= " AND CAST(vm.identifying_context AS JSON) = CAST($identifyingContext AS JSON) ";
            }
        }
        if($request->startIndex !== "" && $request->endIndex !== "")
        {
            $limitQuery .= " LIMIT $request->startIndex,$request->endIndex";
        }
        $query = "SELECT
            vm.id,
            vm.identifying_context,
            vm.type,
            vm.name,
            vm.properties,
            vm.trashed,
            vm.created_by,
            vm.created_date,
            vm.updated_by,
            vm.updated_date
        FROM
            `valuation_method` vm
        WHERE
            1 = 1";
        try {
            $valuationMethods = $this->executeQueryForList($query.$whereQuery.$limitQuery,$this->mapper[ValuationMethodServiceMapper::SEARCH_VALUATION_METHOD]);
        } catch (\Exception $e) {
            throw new ProfessionalException(ProfessionalException::ERROR_FETCHING,"Cannot fetch valuation method details! Please try again");
        }
        return $valuationMethods;
    }
    /**
     * Delete Valuation Method By identity context
     * @param String $type
     * @param Array $properties
     * @return NULL
     */
    public function deleteValuationMethodIdentityContext($type,$properties)
    {
        $type = $this->realEscapeString($type);
        $properties = $this->realEscapeArray($properties);
        if(empty($type)) {
            throw new ProfessionalException(ProfessionalException::EMPTY_PARAMETERS,"Please set an evaluation type");
        }
        if(!count($properties)) {
            throw new ProfessionalException(ProfessionalException::EMPTY_PARAMETERS,"Please set atleast one property");
        }
        $query = "DELETE FROM
                    `valuation_method`
                WHERE
                    `type` = '$type'  ";
        $query .= " and ".implode(" and ",array_map(function($property) {
            return " identifying_context->>'$.$property->name' = '$property->value";
        },$properties));
        try {
            $this->executeQuery($query);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(),"Error deleting valuation method! Please try again");
        }
    }
    /**
     * Delete ValuationMethod
     * @param String $id
     * @return NULL
     */
    public function deleteValuationMethodByIdetifiyingContext($context,$value,$type)
    {
        $context = $this->realEscapeString($context);
        $value = $this->realEscapeString($value);
        if(empty($context)) {
            throw new ProfessionalException(ProfessionalException::EMPTY_PARAMETERS,"No valuation method selected! Please specify the context");
        }
        if(empty($value)) {
            throw new ProfessionalException(ProfessionalException::EMPTY_PARAMETERS,"No valuation method selected! Please specify the value");
        }
        if(empty($type)) {
            throw new ProfessionalException(ProfessionalException::EMPTY_PARAMETERS,"No valuation method selected! Please specify the value");
        }
        $query = "DELETE FROM `valuation_method`
        WHERE identifying_context->>'$.$context' = '$value' AND type = '$type';";
        try {
            $this->executeQuery($query);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(),"Error deleting valuation method! Please try again");
        }
    }
    /**
     * Get Valuation Method By identity context
     * @param String $type
     * @param Array $properties
     * @return NULL
     */
    public function getValuationMethodIdentityContext($type,$properties)
    {
        $type = $this->realEscapeString($type);
        $properties = $this->realEscapeArray($properties);
        if(empty($type)) {
            throw new ProfessionalException(ProfessionalException::EMPTY_PARAMETERS,"Please set an evaluation type");
        }
        if(!count($properties)) {
            throw new ProfessionalException(ProfessionalException::EMPTY_PARAMETERS,"Please set atleast one property");
        }
        $query = "SELECT vm.id,
            vm.identifying_context,
            vm.type,
            vm.name,
            vm.properties,
            vm.trashed,
            vm.created_by,
            vm.created_date,
            vm.updated_by,
            vm.updated_date FROM
                    `valuation_method` vm
                WHERE
                    vm.type = '$type'  ";
        $query .= " and ".implode(" and ",array_map(function($property) {
            return " vm.identifying_context->>'$.$property->name' = '$property->value";
        },$properties));
        try {
            return $this->executeQueryForList($query,$this->mapper[ValuationMethodServiceMapper::SEARCH_VALUATION_METHOD])[0];
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(),"Error while fetching valuation method! Please try again");
        }
    }
}