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 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 128
ExamFineTypeService
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 8
650.00
0.00% covered (danger)
0.00%
0 / 128
 __construct
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 3
 saveFineType
0.00% covered (danger)
0.00%
0 / 1
56.00
0.00% covered (danger)
0.00%
0 / 32
 validateSaveFineType
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 3
 insertFineType
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 11
 updateFineType
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 15
 deleteFineType
0.00% covered (danger)
0.00%
0 / 1
20.00
0.00% covered (danger)
0.00%
0 / 27
 getFineTypes
0.00% covered (danger)
0.00%
0 / 1
20.00
0.00% covered (danger)
0.00%
0 / 23
 checkFineTypeIsAssignedFeeTemplate
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 14
<?php
namespace com\linways\ec\core\service;
use com\linways\base\util\MakeSingletonTrait;
use com\linways\base\util\SecurityUtils;
use com\linways\ec\core\constant\StatusConstants;
use com\linways\ec\core\exception\ExamControllerException;
use com\linways\ec\core\dto\FineType;
use com\linways\ec\core\logging\Events;
use com\linways\ec\core\logging\entities\Staff;
use com\linways\core\ams\professional\logging\AMSLogger;
class ExamFineTypeService extends BaseService
{
    use MakeSingletonTrait;
    private function __construct()
    {
        $this->logger = AMSLogger::getLogger('exam-controller-log');
    }
    /**
     * Save fine Type
     * @param FineType $fineType
     * @return $id
     */
    public function saveFineType(FineType $fineType){
        $fineType = $this->realEscapeObject($fineType);
        $staffId = $GLOBALS['userId'];
        try {
            $this->validateSaveFineType($fineType);
            if (!empty($fineType->id)) {
                $fineType->id = $this->updateFineType($fineType);
            } else {
                $fineType->id = $this->insertFineType($fineType);
            }
            AMSLogger::log_info($this->logger,Events::EC_SAVE_FINE_TYPE, [
                "staff" => new Staff(["id" => $staffId]),
                "request" => $fineType,
                "status" => StatusConstants::SUCCESS
            ]);
        } catch (\Exception $e) {
            AMSLogger::log_error($this->logger,Events::EC_SAVE_FINE_TYPE, [
                "staff" => new Staff(["id" => $staffId]),
                "request" => $fineType,
                "errorCode" => $e->getCode(),
                "errorMessage" => $e->getMessage(),
                "status" => StatusConstants::FAILED
            ]);
            if ($e->getCode() !== ExamControllerException::INVALID_PARAMETERS && $e->getCode() !== ExamControllerException::EMPTY_PARAMETERS && $e->getCode() !== "DUPLICATE_ENTRY") {
                throw new ExamControllerException($e->getCode(), "Failed to save fine Type! Please try again");
            } else if ($e->getCode() === ExamControllerException::DUPLICATE_ENTRY) {
                throw new ExamControllerException(ExamControllerException::DUPLICATE_ENTRY, "Cannot create fine type.This fine type already created");
            } else {
                throw new ExamControllerException($e->getCode(), $e->getMessage());
            }
        }
        return $fineType->id;
    }
    /**
     * Validate fine type Request Before Saving
     * @param FineType $fineType
     * @return NULL
     */
    private function validateSaveFineType(FineType $fineType){
        if (empty($fineType->name) || empty($fineType->priority))
            throw new ExamControllerException(ExamControllerException::EMPTY_PARAMETERS, " Fine name is  empty! Please fill name ");
    }
    /**
     * Insert fine type
     * @param FineType $fineType
     * @return  $id
     */
    private function insertFineType(FineType $fineType){
        $query = "INSERT INTO exam_finetype
                  (examfineName,priority)
                  VALUES
                  ('$fineType->name','$fineType->priority')";
        try {
            $fineType->id = $this->executeQuery($query,true)->id;
            return $fineType->id;
        } catch (\Exception $e) {
            throw new ExamControllerException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * Update fine types
     * @param FineType $fineType
     * @return $fineType->id
     */
    private function updateFineType(FineType $fineType)
    {
        $query = "UPDATE
                    exam_finetype
                SET
                    examfineName = '$fineType->name',
                    priority = '$fineType->priority'
                WHERE
                    examfineID = '$fineType->id'";
        try {
            $this->executeQuery($query);
            return $fineType->id;
        } catch (\Exception $e) {
            throw new ExamControllerException($e->getCode(), $e->getMessage());
        }
    }
     /**
     * Delete Exam fine Type
     * @param String $id
     * @return NULL
     */
    public function deleteFineType($id){
        $id = $this->realEscapeString($id);
        $staffId = $GLOBALS['userId'];
        $fineType = new \stdClass();
        $fineType->id = $id;
        $currentFineTypeDetails = reset($this->getFineTypes($fineType));
        if(empty($currentFineTypeDetails)){
            throw new ExamControllerException(ExamControllerException::EMPTY_PARAMETERS,"Can't delete ! fine Type missing.");
        }
        // to check the fine type is assigned in exam fine template 
        $isAssigned = $this->checkFineTypeIsAssignedFeeTemplate($id);
        if(!empty($isAssigned)){
            throw new ExamControllerException(ExamControllerException::EMPTY_PARAMETERS,"Can't delete ! fine Type Already assigned.");
        }
        $query = "DELETE FROM
                    exam_finetype
                WHERE
                    examfineID = '$id'";
        try {
            $this->executeQuery($query);
             // LOGGING
            AMSLogger::log_info($this->logger,Events::EC_DELETE_FINE_TYPE,[
                "staff" => new Staff(["id" => $staffId]),
                "request" => $currentFineTypeDetails,
                "status" => StatusConstants::SUCCESS
            ]);
        }catch (\Exception $e) {
            throw new ExamControllerException(ExamControllerException::HAVE_RELATION,"Error deleting mderation rule! Please try again");
        }
    }
    /**
     * get fine Types
     * @param $searchRequest
     * @return $fineTypes
     */
    public function getFineTypes($searchRequest){
        $searchRequest = $this->realEscapeObject($searchRequest);
        try {
            $whereQuery = "";
            $orderBy = " ORDER BY eft.priority ASC ";
            if (!empty($searchRequest->id)) {
                $fineTypeIdString = is_array($searchRequest->id) ? "'" . implode("','", $searchRequest->id) . "'" : "'" . $searchRequest->id . "'";
                $whereQuery .= " AND eft.examfineID IN ( $fineTypeIdString )";
            }
            $query = "SELECT
                            DISTINCT 
                            eft.examfineID as id,
                            eft.examfineName as name,
                            eft.priority as priority
                        FROM
                        exam_finetype eft
                        WHERE
                            1 = 1 ";
            $fineTypes =  $this->executeQueryForList($query . $whereQuery.$orderBy);
        } catch (\Exception $e) {
            throw new ExamControllerException($e->getCode(), $e->getMessage());
        }
        return $fineTypes;
    }
     /**
     * check Fine Type Is Assigned Fee Template
     * @param $id
     * @return $templateId
     */
    public function checkFineTypeIsAssignedFeeTemplate($id){
        try {
            $query = "SELECT 
                        id
                    FROM 
                        ec_examregistration_fee_templates
                    WHERE 
                        JSON_CONTAINS_PATH(fine_properties, 'one', '$[0].fineType.\"$id\"')
                    LIMIT 1 ";
            $templateId =  $this->executeQueryForObject($query );
        } catch (\Exception $e) {
            throw new ExamControllerException($e->getCode(), $e->getMessage());
        }
        return $templateId;
    }
    
}