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 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 96
V4IntimationLogService
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 2
1056.00
0.00% covered (danger)
0.00%
0 / 96
 saveV4IntimationLog
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 42
 validateIntimationLog
0.00% covered (danger)
0.00%
0 / 1
870.00
0.00% covered (danger)
0.00%
0 / 54
<?php
namespace com\linways\core\ams\professional\service;
use com\linways\base\constant\UserType;
use com\linways\base\util\MakeSingletonTrait;
use com\linways\core\ams\professional\constant\V4IntimationLogModuleConstants;
use com\linways\core\ams\professional\constant\V4IntimationLogReceiverTypeContext;
use com\linways\core\ams\professional\constant\V4IntimationLogStatusConstants;
use com\linways\core\ams\professional\constant\V4IntimationTypeConstants;
use com\linways\core\ams\professional\dto\V4IntimationLog;
use com\linways\core\ams\professional\dto\V4IntimationLogExtraDetails;
use com\linways\core\ams\professional\exception\ProfessionalException;
class V4IntimationLogService extends BaseService
{
    use MakeSingletonTrait;
    /**
     * @param SaveV4IntimationLogRequest $request
     * @return String $id
     * @throws ProfessionalException
     */
    /**
     * Save v4 intimation log
     *
     * This method inserts logs to v4 intimation log table
     *
     * @param V4IntimationLog $v4IntimationLog
     * @return String $id
     * @throws ProfessionalException
     **/
    public function saveV4IntimationLog(V4IntimationLog $v4IntimationLog)
    {
        try {
            $this->validateIntimationLog($v4IntimationLog);
            $isScheduledIntimation = $v4IntimationLog->isScheduledIntimation == true ? 1 : 0;
            $v4IntimationLog = $this->realEscapeObject($v4IntimationLog);
            $extraDetails = json_encode($v4IntimationLog->extraDetails);
            $insertSql = "INSERT
                INTO
                v4_intimation_log (user_id,
                user_type,
                receiver_type_context,
                type,
                content,
                status,
                module,
                context,
                sender_user_id,
                sender_user_type,
                send_time,
                extra_details,
                is_scheduled_intimation,
                created_by)
            VALUES ('$v4IntimationLog->userId',
            '$v4IntimationLog->userType',
            '$v4IntimationLog->receiverTypeContext',
            '$v4IntimationLog->type',
            '$v4IntimationLog->content',
            '$v4IntimationLog->status',
            '$v4IntimationLog->module',
            '$v4IntimationLog->context',
            '$v4IntimationLog->senderUserId',
            '$v4IntimationLog->senderUserType',
            '$v4IntimationLog->sendTime',
            '$extraDetails',
            '$isScheduledIntimation',
            '$v4IntimationLog->createdBy')";
            $result = $this->executeQuery($insertSql,true);
            return $result->id;
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * Validate intimation log
     *
     * This method validate the intimation log object
     *
     * @param V4IntimationLog $v4IntimationLog
     * @return null
     * @throws ProfessionalException
     **/
    protected function validateIntimationLog(V4IntimationLog $v4IntimationLog)
    {
        // IF ANY NEW SUPPORTED TYPES ARE ADDED IN FUTURE, ADD THEM HERE
        $currentSupportedUserTypes = [UserType::STAFF, UserType::STUDENT];
        $currentSupportedIntimationTypes = [V4IntimationTypeConstants::EMAIL, V4IntimationTypeConstants::SMS];
        $currentSupportedStatuses = [V4IntimationLogStatusConstants::SUCCESS, V4IntimationLogStatusConstants::FAILED];
        $currentSupportedSenderUserTypes = [UserType::STAFF, UserType::STUDENT];
        $currentSupportedModules = [V4IntimationLogModuleConstants::ACADEMICS];
        $currentSupportedReceiverTypeConstants = [V4IntimationLogReceiverTypeContext::STAFF, V4IntimationLogReceiverTypeContext::STUDENT, V4IntimationLogReceiverTypeContext::FATHER, V4IntimationLogReceiverTypeContext::MOTHER];
        if (empty($v4IntimationLog->userId)
            || empty($v4IntimationLog->userType)
            || empty($v4IntimationLog->type)
            || empty($v4IntimationLog->status)
            || empty($v4IntimationLog->module)
            || empty($v4IntimationLog->context)
            || (empty($v4IntimationLog->senderUserId) && $v4IntimationLog->isScheduledIntimation != true)
            || empty($v4IntimationLog->senderUserType)
            || empty($v4IntimationLog->receiverTypeContext)
            || (empty($v4IntimationLog->createdBy) && $v4IntimationLog->isScheduledIntimation != true)
        ) {
            throw new ProfessionalException(ProfessionalException::EMPTY_PARAMETERS, 'Cannot save log. Empty parameter(s).');
        }
        // VALIDATING USER TYPE
        if (!in_array($v4IntimationLog->userType, $currentSupportedUserTypes)) {
            throw new ProfessionalException(ProfessionalException::INVALID_PARAMETERS, "User type should be either of ".implode(" or ", $currentSupportedUserTypes)."");
        }
        // VALIDATE CONTEXT LENGTH SHOULD BE LESS THAN 200
        if (strlen($v4IntimationLog->context) > 200) {
            throw new ProfessionalException(ProfessionalException::INVALID_PARAMETERS, "Context should be less than 200 characters");
        }
        // Check if the context is a constant defined in V4IntimationLogContextConstants class
        if (!defined('com\linways\core\ams\professional\constant\V4IntimationLogContextConstants::' . $v4IntimationLog->context)) {
            throw new ProfessionalException(ProfessionalException::INVALID_PARAMETERS, "Context should be a constant defined in V4IntimationLogContextConstants class");
        }
        // VALIDATING INTIMATION TYPE
        if (!in_array($v4IntimationLog->type, $currentSupportedIntimationTypes)) {
            throw new ProfessionalException(ProfessionalException::INVALID_PARAMETERS, "Intimation type should be either of ".implode(" or ", $currentSupportedIntimationTypes)."");
        }
        // VALIDATING STATUS
        if (!in_array($v4IntimationLog->status, $currentSupportedStatuses)) {
            throw new ProfessionalException(ProfessionalException::INVALID_PARAMETERS, "Status type should be either of ".implode(" or ", $currentSupportedStatuses)."");
        }
        // VALIDATING SENDER USER TYPE
        if (!in_array($v4IntimationLog->senderUserType, $currentSupportedSenderUserTypes)) {
            throw new ProfessionalException(ProfessionalException::INVALID_PARAMETERS, "Sender user type should be either of ".implode(" or ", $currentSupportedSenderUserTypes)."");
        }
        // VALIDATING RECEIVER TYPE CONTEXT
        if (!in_array($v4IntimationLog->receiverTypeContext, $currentSupportedReceiverTypeConstants)) {
            throw new ProfessionalException(ProfessionalException::INVALID_PARAMETERS, "Receiver type context should be either of ".implode(" or ", $currentSupportedReceiverTypeConstants)."");
        }
        // VALIDATING MODULE
        if (!in_array($v4IntimationLog->module, $currentSupportedModules)) {
            throw new ProfessionalException(ProfessionalException::INVALID_PARAMETERS, "Sender user type should be either of ".implode(" or ", $currentSupportedModules)."");
        }
        // VALIDATE EXTRA DETAILS
        if (!($v4IntimationLog->extraDetails instanceof V4IntimationLogExtraDetails)) {
            throw new ProfessionalException(ProfessionalException::INVALID_PARAMETERS, "Extra details should be an instance of V4IntimationLogExtraDetails");
        }
        // VALIDATE isAttachmentIncluded is set either as '0 or '1' if the intimation type is email
        if ($v4IntimationLog->type == V4IntimationTypeConstants::EMAIL && (!is_string($v4IntimationLog->extraDetails->isAttachmentIncluded) || ($v4IntimationLog->extraDetails->isAttachmentIncluded !== '0' && $v4IntimationLog->extraDetails->isAttachmentIncluded !== '1'))) {
            throw new ProfessionalException(ProfessionalException::INVALID_PARAMETERS, "isAttachmentIncluded should be set for email intimation type and it should be either string '0' or '1' value");
        }
        // VALIDATE errorCode if status is failed
        if ($v4IntimationLog->status == V4IntimationLogStatusConstants::FAILED && (empty($v4IntimationLog->extraDetails->errorCode) || empty($v4IntimationLog->extraDetails->errorMessage))) {
            throw new ProfessionalException(ProfessionalException::INVALID_PARAMETERS, "Error code and error message should be included in the extra details for failed status");
        }
    }
}