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 / 110
FolioNumberPrefixService
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 7
506.00
0.00% covered (danger)
0.00%
0 / 110
 __construct
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 3
 saveFolioNumberPrefix
0.00% covered (danger)
0.00%
0 / 1
56.00
0.00% covered (danger)
0.00%
0 / 32
 validateSaveFolioNumberPrefix
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 3
 insertFolioNumberPrefix
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 11
 updateFolioNumberPrefix
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 15
 deleteFolioNumberPrefix
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 23
 getFolioNumberPrefix
0.00% covered (danger)
0.00%
0 / 1
20.00
0.00% covered (danger)
0.00%
0 / 23
<?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\FolioNumberPrefix;
use com\linways\ec\core\logging\Events;
use com\linways\ec\core\logging\entities\Staff;
use com\linways\core\ams\professional\logging\AMSLogger;
class FolioNumberPrefixService extends BaseService
{
    use MakeSingletonTrait;
    private function __construct()
    {
        $this->logger = AMSLogger::getLogger('exam-controller-log');
    }
    /**
     * Save Folio Number Prefix
     * @param FolioNumberPrefix $folioNumberPrefix
     * @return $id
     */
    public function saveFolioNumberPrefix(FolioNumberPrefix $folioNumberPrefix){
        $folioNumberPrefix = $this->realEscapeObject($folioNumberPrefix);
        $staffId = $GLOBALS['userId'];
        try {
            $this->validateSaveFolioNumberPrefix($folioNumberPrefix);
            if (!empty($folioNumberPrefix->id)) {
                $folioNumberPrefix->id = $this->updateFolioNumberPrefix($folioNumberPrefix);
            } else {
                $folioNumberPrefix->id = $this->insertFolioNumberPrefix($folioNumberPrefix);
            }
            AMSLogger::log_info($this->logger,Events::EC_SAVE_FOLIO_NUMBER_PREFIX, [
                "staff" => new Staff(["id" => $staffId]),
                "request" => $folioNumberPrefix,
                "status" => StatusConstants::SUCCESS
            ]);
        } catch (\Exception $e) {
            AMSLogger::log_error($this->logger,Events::EC_SAVE_FOLIO_NUMBER_PREFIX, [
                "staff" => new Staff(["id" => $staffId]),
                "request" => $folioNumberPrefix,
                "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 folio number prefix! Please try again");
            } else if ($e->getCode() === ExamControllerException::DUPLICATE_ENTRY) {
                throw new ExamControllerException(ExamControllerException::DUPLICATE_ENTRY, "Cannot add folio prefix. prefix already entered");
            } else {
                throw new ExamControllerException($e->getCode(), $e->getMessage());
            }
        }
        return $folioNumberPrefix->id;
    }
    /**
     * Validate Folio Number Prefix Request Before Saving
     * @param FolioNumberPrefix $folioNumberPrefix
     * @return NULL
     */
    private function validateSaveFolioNumberPrefix(FolioNumberPrefix $folioNumberPrefix){
        if (empty($folioNumberPrefix->year) || empty($folioNumberPrefix->prefix))
            throw new ExamControllerException(ExamControllerException::EMPTY_PARAMETERS, " empty parameter. can't folio prefix! ");
    }
    /**
     * Insert Folio Number Prefix
     * @param FolioNumberPrefix $folioNumberPrefix
     * @return  $id
     */
    private function insertFolioNumberPrefix(FolioNumberPrefix $folioNumberPrefix){
        $query = "INSERT INTO folioNumber_prefix
                  (prefix,year)
                  VALUES
                  ('$folioNumberPrefix->prefix','$folioNumberPrefix->year')";
        try {
            $folioNumberPrefix->id = $this->executeQuery($query,true)->id;
            return $folioNumberPrefix->id;
        } catch (\Exception $e) {
            throw new ExamControllerException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * Update Folio Number Prefix
     * @param FolioNumberPrefix $folioNumberPrefix
     * @return $folioNumberPrefix->id
     */
    private function updateFolioNumberPrefix(FolioNumberPrefix $folioNumberPrefix)
    {
        $query = "UPDATE
                    folioNumber_prefix
                SET
                    prefix = '$folioNumberPrefix->prefix',
                    year = '$folioNumberPrefix->year'
                WHERE
                    id = '$folioNumberPrefix->id'";
        try {
            $this->executeQuery($query);
            return $folioNumberPrefix->id;
        } catch (\Exception $e) {
            throw new ExamControllerException($e->getCode(), $e->getMessage());
        }
    }
     /**
     * Delete Folio Number Prefix
     * @param String $id
     * @return NULL
     */
    public function deleteFolioNumberPrefix($id){
        $id = $this->realEscapeString($id);
        $staffId = $GLOBALS['userId'];
        $folioNumberPrefix = new \stdClass();
        $folioNumberPrefix->id = $id;
        $currentFolioNumberPrefixDetails = reset($this->getFolioNumberPrefix($folioNumberPrefix));
        if(empty($currentFolioNumberPrefixDetails)){
            throw new ExamControllerException(ExamControllerException::EMPTY_PARAMETERS,"Can't delete ! Folio prefix missing.");
        }
        $query = "DELETE FROM
                    folioNumber_prefix
                WHERE
                    id = '$id'";
        try {
            $this->executeQuery($query);
             // LOGGING
            AMSLogger::log_info($this->logger,Events::EC_DELETE_FOLIO_NUMBER_PREFIX,[
                "staff" => new Staff(["id" => $staffId]),
                "request" => $currentFolioNumberPrefixDetails,
                "status" => StatusConstants::SUCCESS
            ]);
        }catch (\Exception $e) {
            throw new ExamControllerException(ExamControllerException::HAVE_RELATION,"Error deleting subject category! Please try again");
        }
    }
    /**
     * get Folio Number Prefix
     * @param $searchRequest
     * @return $folioNumberPrefix
     */
    public function getFolioNumberPrefix($searchRequest){
        $searchRequest = $this->realEscapeObject($searchRequest);
        try {
            $whereQuery = null;
            $whereQuery = "";
            if (!empty($searchRequest->id)) {
                $folioNumberPrefixIdString = is_array($searchRequest->id) ? "'" . implode("','", $searchRequest->id) . "'" : "'" . $searchRequest->id . "'";
                $whereQuery .= " AND fnp.id IN ( $folioNumberPrefixIdString )";
            }
            $query = "SELECT
                            DISTINCT 
                            fnp.id as id,
                            fnp.year as year,
                            fnp.prefix as prefix
                        FROM
                            folioNumber_prefix fnp
                        WHERE
                            1 = 1 ";
            $folioNumberPrefix =  $this->executeQueryForList($query . $whereQuery);
        } catch (\Exception $e) {
            throw new ExamControllerException($e->getCode(), $e->getMessage());
        }
        return $folioNumberPrefix;
    }
    
}