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 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 304
ThirdPartyApiService
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 5
3306.00
0.00% covered (danger)
0.00%
0 / 304
 __construct
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 2
 __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 / 5
 sendMessage
0.00% covered (danger)
0.00%
0 / 1
2756.00
0.00% covered (danger)
0.00%
0 / 293
 pushNotificationTov4App
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 2
<?php
namespace com\linways\core\ams\professional\service;
use HTMLPurifier;
use HTMLPurifier_Config;
use com\linways\core\ams\professional\dto\Messages;
use com\linways\core\ams\professional\dto\RecipeintType;
use Symfony\Component\Config\Definition\Exception\Exception;
use com\linways\core\ams\professional\exception\ProfessionalException;
use com\linways\core\ams\professional\service\HODService;
class ThirdPartyApiService extends BaseService
{
    // /Condition 1 - Presence of a static member variable
    private static $_instance = null;
    // /Condition 2 - Locked down the constructor
    private function __construct()
    {
    }
    // Prevent any oustide instantiation of this class
    // /Condition 3 - Prevent any object or instance of that class to be cloned
    private function __clone()
    {
    }
    // Prevent any copy of this object
    // /Condition 4 - Have a single globally accessible static method
    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;
    }
    /**
     * @param Messages $messages
     * @throws ProfessionalException
     */
    public function sendMessage($messages)
    {
        $messages->content = $this->realEscapeString($messages->content);
        $messages->subject = $this->realEscapeString($messages->subject);
        $messages->batchId = $this->realEscapeString($messages->batchId);
        $messages->deptId = $this->realEscapeString($messages->deptId);
        $messages->recType = $this->realEscapeString($messages->recType);
        $messages->senderType = $this->realEscapeString($messages->senderType);
        $msgId = $this->composeMessage($messages);
        if($messages->deptId == 'null' || empty($messages->deptId)) {
            $messages->deptId =0;
        }
        if(empty($messages->subjectId)) {
            $messages->subjectId = 0;
        }
        switch ($messages->recType) {
            case RecipeintType::ALL:
                $sqlStudents = "INSERT INTO messageRecipeints(msgId, userId, userType, batchId, deptId, subjectId, isSubbatch ) 
                                SELECT $msgId, studentID, 'STUDENT', $messages->batchId$messages->deptId$messages->subjectId
                                $messages->isSubbatch FROM studentaccount WHERE student_lock=0 ";
                
                if($messages->pushNotification)
                {
                    
                }
                $sqlStaff = "INSERT INTO messageRecipeints(msgId, userId, userType, batchId, deptId, subjectId, isSubbatch ) 
                              SELECT $msgId, staffID, 'FACULTY', $messages->batchId$messages->deptId$messages->subjectId$messages->isSubbatch 
                              FROM staffaccounts WHERE isResigned=0 AND staffID !=$messages->senderId ";
                if ($messages->deptId) {
                    $sqlStudents .= "AND deptID=$messages->deptId";
                    $sqlStaff .= "AND deptID=$messages->deptId";
                }
                $sqlStaff .= " GROUP BY staffID";
                try {
                    $this->executeQueryForObject($sqlStudents);
                    $this->executeQueryForObject($sqlStaff);
                } catch (\Exception $e) {
                    throw new ProfessionalException($e->getCode(), $e->getMessage());
                }
                break;
                
            case RecipeintType::ALL_BATCH:
                $sqlStudents = "INSERT INTO messageRecipeints(msgId, userId, userType, batchId, deptId, subjectId, isSubbatch ) 
                                SELECT $msgId, studentID, 'STUDENT', $messages->batchId$messages->deptId$messages->subjectId
                                $messages->isSubbatch FROM studentaccount WHERE student_lock=0 AND batchID=$messages->batchId AND deptID=$messages->deptId";
                $sqlStaff = "INSERT INTO messageRecipeints(msgId, userId, userType, batchId, deptId, subjectId, isSubbatch ) 
                              SELECT $msgId, st.staffID, 'FACULTY', $messages->batchId, st.deptID, $messages->subjectId$messages->isSubbatch 
                              FROM staffaccounts st INNER JOIN sbs_relation sr ON st.staffID = sr.staffID INNER JOIN batches b ON b.batchID = sr.batchID 
                              AND b.semID = sr.semID  WHERE st.isResigned=0 AND st.deptID=$messages->deptId 
                              AND sr.batchID = $messages->batchId AND st.staffID !=$messages->senderId GROUP BY st.staffID";
                try {
                    $this->executeQuery($sqlStudents);
                    $this->executeQuery($sqlStaff);
                } catch (\Exception $e) {
                    throw new ProfessionalException($e->getCode(), $e->getMessage());
                }
                break;
            case RecipeintType::ALL_SUBBATCH:
                $sqlStudents = "INSERT INTO messageRecipeints(msgId, userId, userType, batchId, deptId, subjectId, isSubbatch ) 
                                SELECT $msgId, sa.studentID, 'STUDENT', sub.subbatchID, sa.deptID, $messages->subjectId$messages->isSubbatch 
                                FROM studentaccount sa INNER JOIN subbatches sub ON sub.batchID =sa.batchID INNER JOIN subbatch_student ss ON sub.subbatchID=ss.subbatchID 
                                AND sa.studentID =ss.studentID WHERE sub.subbatchID=$messages->batchId";
                $sqlStaff = "INSERT INTO messageRecipeints(msgId, userId, userType, batchId, deptId, subjectId, isSubbatch ) 
                            SELECT $msgId, st.staffID, 'FACULTY', $messages->batchId, st.deptID, $messages->subjectId$messages->isSubbatch FROM staffaccounts st 
                            INNER JOIN sbs_relation sr ON st.staffID = sr.staffID INNER JOIN subbatch_sbs ss ON ss.sbsID=sr.sbsID 
                            WHERE st.isResigned=0 AND st.deptID=$messages->deptId AND ss.subbatchID=$messages->batchId AND st.staffID !=$messages->senderId group by st.staffID";
                try {
                    $this->executeQuery($sqlStudents);
                    $this->executeQuery($sqlStaff);
                } catch (\Exception $e) {
                    throw new ProfessionalException($e->getCode(), $e->getMessage());
                }
                break;
            case RecipeintType::ALL_FACULTY:
                $sqlStaff = "INSERT INTO messageRecipeints(msgId, userId, userType, batchId, deptId, subjectId, isSubbatch ) 
                            SELECT $msgId, staffID, 'FACULTY', $messages->batchId$messages->deptId$messages->subjectId$messages->isSubbatch 
                            FROM staffaccounts WHERE isResigned=0 AND staffID !=$messages->senderId group by staffID";
                try {
                    $this->executeQuery($sqlStaff);
                } catch (\Exception $e) {
                    throw new ProfessionalException($e->getCode(), $e->getMessage());
                }
                break;
            case RecipeintType::ALL_DEPT_FACULTY:
                $sqlStaff = "INSERT INTO messageRecipeints(msgId, userId, userType, batchId, deptId, subjectId, isSubbatch ) 
                              SELECT $msgId, staffID, 'FACULTY', $messages->batchId, deptID, $messages->subjectId$messages->isSubbatch 
                              FROM staffaccounts WHERE isResigned=0 AND deptID=$messages->deptId AND staffID !=$messages->senderId group by staffID";
                try {
                    $this->executeQuery($sqlStaff);
                } catch (\Exception $e) {
                    throw new ProfessionalException($e->getCode(), $e->getMessage());
                }
                break;
            case RecipeintType::ALL_BATCH_FACULTY:
                $sqlStaff = "INSERT INTO messageRecipeints(msgId, userId, userType, batchId, deptId, subjectId, isSubbatch ) 
                            SELECT $msgId, st.staffID, 'FACULTY', $messages->batchId, st.deptID, $messages->subjectId$messages->isSubbatch 
                            FROM staffaccounts st INNER JOIN sbs_relation sr ON st.staffID = sr.staffID INNER JOIN batches b ON b.batchID = sr.batchID 
                            AND b.semID = sr.semID  WHERE st.isResigned=0 AND st.deptID=$messages->deptId 
                            AND sr.batchID = $messages->batchId AND st.staffID !=$messages->senderId group by st.staffID";
                try {
                    $this->executeQuery($sqlStaff);
                } catch (\Exception $e) {
                    throw new ProfessionalException($e->getCode(), $e->getMessage());
                }
                break;
            case RecipeintType::ALL_SUBBATCH_FACULTY:
                $sqlStaff = "INSERT INTO messageRecipeints(msgId, userId, userType, batchId, deptId, subjectId, isSubbatch ) 
                            SELECT $msgId, st.staffID, 'FACULTY', $messages->batchId, st.deptID, $messages->subjectId$messages->isSubbatch 
                            FROM staffaccounts st INNER JOIN sbs_relation sr ON st.staffID = sr.staffID INNER JOIN subbatch_sbs ss ON ss.sbsID=sr.sbsID 
                            WHERE st.isResigned=0 AND st.deptID=$messages->deptId AND ss.subbatchID=$messages->batchId 
                            AND st.staffID !=$messages->senderId group by st.staffID";
                try {
                    $this->executeQuery($sqlStaff);
                } catch (\Exception $e) {
                    throw new ProfessionalException($e->getCode(), $e->getMessage());
                }
                break;
            case RecipeintType::ALL_STUDENTS:
                $sqlStudents = "INSERT INTO messageRecipeints(msgId, userId, userType, batchId, deptId, subjectId, isSubbatch ) 
                                SELECT $msgId, studentID, 'STUDENT', $messages->batchId$messages->deptId$messages->subjectId$messages->isSubbatch 
                                FROM studentaccount WHERE student_lock=0";
                try {
                    $this->executeQuery($sqlStudents);
                } catch (\Exception $e) {
                    throw new ProfessionalException($e->getCode(), $e->getMessage());
                }
                break;
            case RecipeintType::ALL_BATCH_STUDENT:
                if(!$messages->deptId){
                    $sqlStudents = "INSERT INTO messageRecipeints(msgId, userId, userType, batchId, deptId, subjectId, isSubbatch ) 
                                SELECT $msgId, studentID, 'STUDENT', batchID, deptID, $messages->subjectId$messages->isSubbatch 
                                FROM studentaccount WHERE student_lock=0 AND batchID=$messages->batchId ";
                }
                else{
                    $sqlStudents = "INSERT INTO messageRecipeints(msgId, userId, userType, batchId, deptId, subjectId, isSubbatch ) 
                                SELECT $msgId, studentID, 'STUDENT', batchID, deptID, $messages->subjectId$messages->isSubbatch 
                                FROM studentaccount WHERE student_lock=0 AND batchID=$messages->batchId AND deptID=$messages->deptId";
                }
                
                try {
                    $this->executeQuery($sqlStudents);
                } catch (\Exception $e) {
                    throw new ProfessionalException($e->getCode(), $e->getMessage());
                }
                break;
            case RecipeintType::ALL_DEPT_STUDENT:
                $sqlStudents = "INSERT INTO messageRecipeints(msgId, userId, userType, batchId, deptId, subjectId, isSubbatch ) 
                                SELECT $msgId, studentID, 'STUDENT', $messages->batchId, deptID, $messages->subjectId$messages->isSubbatch 
                                FROM studentaccount WHERE student_lock=0 AND deptID=$messages->deptId";
                try {
                    $this->executeQuery($sqlStudents);
                } catch (\Exception $e) {
                    throw new ProfessionalException($e->getCode(), $e->getMessage());
                }
            case RecipeintType::ALL_SUBBATCH_STUDENT:
                $sqlStudents = "INSERT INTO messageRecipeints(msgId, userId, userType, batchId, deptId, subjectId, isSubbatch ) 
                                SELECT $msgId, sa.studentID, 'STUDENT', sub.subbatchID, sa.deptID, $messages->subjectId$messages->isSubbatch 
                                FROM studentaccount sa INNER JOIN subbatches sub ON sub.batchID =sa.batchID INNER JOIN subbatch_student ss 
                                ON sub.subbatchID=ss.subbatchID AND sa.studentID =ss.studentID WHERE sub.subbatchID=$messages->batchId";
                try {
                    $this->executeQuery($sqlStudents);
                } catch (\Exception $e) {
                    throw new ProfessionalException($e->getCode(), $e->getMessage());
                }
                break;
            case RecipeintType::INDIVIDUAL:
                if ($messages->recipeint != NULL && count($messages->recipeint) > 0) {
                    $sql = 'INSERT INTO messageRecipeints(msgId, userId, userType, batchId, deptId, subjectId, isSubbatch ) VALUES ';
                    foreach ($messages->recipeint as $user) {
                        $cond[] = "($msgId$user->userId, '$user->userType', $messages->batchId$messages->deptId$messages->subjectId$messages->isSubbatch )";
                    }
                    $sql .= implode(',', $cond);
                    try {
                        $this->executeQuery($sql);
                    } catch (\Exception $e) {
                        throw new ProfessionalException($e->getCode(), $e->getMessage());
                    }
                }
                break;
            case RecipeintType::HOD:
                $sqlStaff = "INSERT INTO messageRecipeints(msgId, userId, userType, batchId, deptId, subjectId, isSubbatch ) 
                            SELECT $msgId, staffID, 'HOD', $messages->batchId, deptID, $messages->subjectId$messages->isSubbatch 
                            FROM staffaccounts WHERE isResigned=0 AND deptID=$messages->deptId AND isHOD >0";
                try {
                    $this->executeQuery($sqlStaff);
                } catch (\Exception $e) {
                    throw new ProfessionalException($e->getCode(), $e->getMessage());
                }
                break;
            case RecipeintType::PRINCIPAL:
                $sqlStaff = "INSERT INTO messageRecipeints(msgId, userId, userType, batchId, deptId, subjectId, isSubbatch ) 
                              SELECT $msgId, staffID, 'PRINCIPAL', $messages->batchId, deptID, $messages->subjectId$messages->isSubbatch 
                              FROM staffaccounts WHERE isResigned=0 AND isPrincipal >0";
                try {
                    $this->executeQuery($sqlStaff);
                } catch (\Exception $e) {
                    throw new ProfessionalException($e->getCode(), $e->getMessage());
                }
                break;
            case RecipeintType::CURRENT_STAFF:
                if ($messages->recipeint != NULL && count($messages->recipeint) > 0) {
                    $sql = 'INSERT INTO messageRecipeints(msgId, userId, userType, batchId, deptId, subjectId, isSubbatch ) VALUES ';
                    foreach ($messages->recipeint as $user) {
                        $cond[] = "($msgId$user->userId, '$user->userType', $user->batchId$user->deptId$user->subjectId$user->isSubbatch )";
                    }
                    $sql .= implode(',', $cond);
                    try {
                        $this->executeQuery($sql);
                    } catch (\Exception $e) {
                        throw new ProfessionalException($e->getCode(), $e->getMessage());
                    }
                }
                break;
                case RecipeintType::ALL_HOD:
                    $faculties = HODService::getInstance()->getAllHodDetails();
                    $sql = 'INSERT INTO messageRecipeints(msgId, userId, userType, deptId ) VALUES ';
                        foreach ($faculties as $user) {
                            $cond[] = "($msgId$user->staffID, 'HOD', $user->deptID )";
                        }
                        $sql .= implode(',', $cond);
                        try {
                            $this->executeQuery($sql);
    
                        } catch (\Exception $e) {
                            throw new ProfessionalException($e->getCode(), $e->getMessage());
                        }
                    break;
                case RecipeintType::ALL_STUDENTS_OF_BATCHYEAR:
                    if($messages->deptId){
                        $sqlStudents = "INSERT INTO messageRecipeints(msgId, userId, userType, batchId, deptId ) 
                                    SELECT 
                                            $msgId, studentID, 'STUDENT', b.batchID, s.deptID
                                        FROM
                                            studentaccount s
                                                INNER JOIN
                                            batches b ON b.batchID = s.batchID
                                        WHERE
                                            student_lock = 0
                                                AND b.batchStartYear = $messages->batchStartYear
                                                And b.deptID = $messages->deptId
                                        ;";
                    }
                    else{
                        $sqlStudents = "INSERT INTO messageRecipeints(msgId, userId, userType, batchId, deptId ) 
                                    SELECT 
                                            $msgId, studentID, 'STUDENT', b.batchID, s.deptID
                                        FROM
                                            studentaccount s
                                                INNER JOIN
                                            batches b ON b.batchID = s.batchID
                                        WHERE
                                            student_lock = 0
                                                AND b.batchStartYear = $messages->batchStartYear
                                        ;";
                    }
                    
                    try {
                        $this->executeQuery($sqlStudents);
                    } catch (\Exception $e) {
                        throw new ProfessionalException($e->getCode(), $e->getMessage());
                    }
                break;
                case RecipeintType::ALL_STUDENTS_OF_BATCHYEAR_WITH_COURSETYPE:
                    if($messages->deptId){
                        $sqlStudents = "INSERT INTO messageRecipeints(msgId, userId, userType, batchId, deptId ) 
                                    SELECT 
                                            $msgId, studentID, 'STUDENT', b.batchID, s.deptID
                                        FROM
                                            studentaccount s
                                                INNER JOIN
                                            batches b ON b.batchID = s.batchID
                                        WHERE
                                            student_lock = 0
                                                AND b.batchStartYear = $messages->batchStartYear
                                                And b.deptID = $messages->deptId
                                                AND b.courseTypeID = '$messages->courseId'
                                        ;";
                    }
                    else{
                        $sqlStudents = "INSERT INTO messageRecipeints(msgId, userId, userType, batchId, deptId ) 
                                    SELECT 
                                            $msgId, studentID, 'STUDENT', b.batchID, s.deptID
                                        FROM
                                            studentaccount s
                                                INNER JOIN
                                            batches b ON b.batchID = s.batchID
                                        WHERE
                                            student_lock = 0
                                                AND b.batchStartYear = $messages->batchStartYear
                                                AND b.courseTypeID = '$messages->courseId'
                                        ;";
                    }
                    
                    try {
                        $this->executeQuery($sqlStudents);
                    } catch (\Exception $e) {
                        throw new ProfessionalException($e->getCode(), $e->getMessage());
                    }
                break;
            // case RecipeintType::USER_GROUP:
            //     $sql = "INSERT INTO messageRecipeints ( msgId, userId, userType ) 
            //     SELECT $msgId, IF(staff_id, staff_id, student_id) AS userId, IF(staff_id, 'FACULTY', 'STUDENT') AS userType FROM message_user_group_users ";
            //     try {
            //         $this->executeQuery($sql);
            //     } catch (\Exception $e) {
            //         throw new ProfessionalException($e->getCode(), $e->getMessage());
            //     }
            // break;
        }
    }
    /**
     * @param Messages $messages
     * @throws ProfessionalException
     */
    public function pushNotificationTov4App($api)
    {
    }
   
}
?>