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 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 92
RuleEvaluationService
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 6
552.00
0.00% covered (danger)
0.00%
0 / 92
 __construct
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 2
 evaluateRule
0.00% covered (danger)
0.00%
0 / 1
42.00
0.00% covered (danger)
0.00%
0 / 26
 processOperand
0.00% covered (danger)
0.00%
0 / 1
20.00
0.00% covered (danger)
0.00%
0 / 16
 getRuleConstantValue
0.00% covered (danger)
0.00%
0 / 1
30.00
0.00% covered (danger)
0.00%
0 / 20
 solveEquation
0.00% covered (danger)
0.00%
0 / 1
30.00
0.00% covered (danger)
0.00%
0 / 20
 getAttendance
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 8
<?php
namespace com\linways\ec\core\service;
use com\linways\base\util\MakeSingletonTrait;
use com\linways\ec\core\constant\StatusConstants;
use com\linways\ec\core\exception\ExamControllerException;
use com\linways\ec\core\request\SearchRuleRequest;
use com\linways\core\ams\professional\service\AttendanceService;
use com\linways\core\ams\professional\service\CommonService;
use com\linways\core\ams\professional\service\examcontroller\ExamRegistrationService;
class RuleEvaluationService extends BaseService
{
    use MakeSingletonTrait;
    private $operandValue = [];
    private function __construct() {
        $operandValue = [];
    }
    /**
     * Evaluate rule
     * @param Object $rule
     * @param RuleData $data
     * @return Boolean
     */
    public function evaluateRule ($rule, RuleData $data)
    {
        return true; // Only for testing and demo purpose
        $rule = $this->realEscapeObject($rule);
        $data = $this->realEscapeObject($data);
        // foreach ($rule as $ruleProp => $ruleValue) {
        //     switch ($ruleProp) {
        //         case 'OPARAND':
        //             foreach ($rule->OPARAND as $oparand => $value) {
        //                 $this->evaluateRule($value);
        //             }
        //             break;
        //         case 'EQUATION':
        //             $this->solveEquation($value);
        //             break;
                
        //         case 'FUNCTION':
        //             $this->callFunction($value);
        //             break;
                
        //         case 'PARAM':
        //             $this->getValue($value);
        //             break;
                
        //         case 'ROUND_OFF':
        //             $this->roundOffValue($value);
        //             break;
                
        //         default:
        //             $this->getValue($value);
        //             break;
        //     }
        // }
        try{
            $result = null;
            if (!empty($rule->EQUATION)) {
                $equationOparandNames = [];
                foreach ($rule->OPARAND as $oparandName => $oparandValue) {
                    $equationOparandNames[$oparandName] = $oparandName;
                    $operandValue[$oparandName] = $this->processOperand($oparandValue, $data);
                }
                $result = $this->solveEquation($rule->EQUATION, $equationOparandNames);
            }
            if (!empty($rule->FUNCTION)) {
                $operandValue[$rule->PARAM] = $this->processOperand($oparandValue);
                $result = $this->callFunction($rule->FUNCTION, $rule->PARAM);
            }
            if (!empty($rule->ROUND_OFF)) {
                $result = $this->roundOffValue($result, $rule->ROUND_OFF);
            }
            
        }catch(\Exception $e) {
            throw new ExamControllerException ($e->getCode(),$e->getMessage());
        }
        
        return $result;
    }
    /**
     * Process Operand
     * @param Object $rule
     * @return Boolean
     */
    private function processOperand ($oparand, $data)
    {
        try{
            $result = null;
            if (is_string($oparand)) {
                return $this->getRuleConstantValue($oparand, $data);
            }
            else if (!empty($oparand)){
                $result = $this->evaluateRule($oparand, $data);
            }
            else {
                // throw Exception "invalid rule"
            }
        }catch(\Exception $e) {
            throw new ExamControllerException ($e->getCode(),$e->getMessage());
        }
        
        return $result;
    }
    /**
     * get Rule constant value
     * @param String $ruleConstant
     * @return $value
     */
    private function getRuleConstantValue ($ruleConstant, $data)
    {
        try{
            $result = null;
            switch ($ruleConstant) {
                case 'ATTENDANCE':
                    return $this->getAttendance($data);
                    break;
                
                case 'OPEN_COURSE_APPLIED':
                    return $this->getOpenCourseAppliedStatus($data);
                    break;
                
                case 'MAX_SUBJECT_BLOCK_COUNT':
                    return $this->getSubjectBlockCount($data);
                    break;
                
                default:
                    // Throw exception  invalid constant!
                    break;
            }
        }catch(\Exception $e) {
            throw new ExamControllerException ($e->getCode(),$e->getMessage());
        }
    }
    /**
     * get Rule constant value
     * @param String $ruleConstant
     * @return $value
     */
    private function solveEquation ($equation, $data)
    {
        try{
            $result = null;
            switch ($ruleConstant) {
                case 'ATTENDANCE':
                    return $this->getAttendance($data);
                    break;
                
                case 'OPEN_COURSE_APPLIED':
                    return $this->getOpenCourseAppliedStatus($data);
                    break;
                
                case 'MAX_SUBJECT_BLOCK_COUNT':
                    return $this->getSubjectBlockCount($data);
                    break;
                
                default:
                    // Throw exception  invalid constant!
                    break;
            }
        }catch(\Exception $e) {
            throw new ExamControllerException ($e->getCode(),$e->getMessage());
        }
    }
    /**
     * get student attendance percentage
     * @param String $ruleConstant
     * @return $value
     */
    private function getAttendance ($data)
    {
        try{
            $attendanceDetails = null;
            // $attendanceDetails = AttendanceService::getInstance()->getStudentAttendancePercentageDetails($data->studentId, $examregID);
        }catch(\Exception $e) {
            throw new ExamControllerException ($e->getCode(),$e->getMessage());
        }
        return $attendanceDetails;
    }
}