Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 153 |
| CertificateUploadService | |
0.00% |
0 / 1 |
|
0.00% |
0 / 6 |
1806.00 | |
0.00% |
0 / 153 |
| __construct | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 3 |
|||
| saveCertificateUpload | |
0.00% |
0 / 1 |
42.00 | |
0.00% |
0 / 30 |
|||
| validatesaveCertificateUpload | |
0.00% |
0 / 1 |
30.00 | |
0.00% |
0 / 3 |
|||
| insertCertificateUpload | |
0.00% |
0 / 1 |
12.00 | |
0.00% |
0 / 12 |
|||
| deleteCertificateUpload | |
0.00% |
0 / 1 |
56.00 | |
0.00% |
0 / 28 |
|||
| getCertificateUpload | |
0.00% |
0 / 1 |
420.00 | |
0.00% |
0 / 77 |
|||
| <?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\FeeType; | |
| use com\linways\ec\core\logging\Events; | |
| use com\linways\ec\core\logging\entities\Staff; | |
| use com\linways\core\ams\professional\logging\AMSLogger; | |
| use com\linways\ec\core\service\CommonExamService; | |
| use com\linways\base\util\TwigRenderer; | |
| use com\linways\core\ams\professional\util\PdfUtil; | |
| use com\linways\core\ams\professional\service\ResourceService; | |
| use com\linways\core\ams\professional\request\GetPreSignedUrlRequest; | |
| use com\linways\core\ams\professional\constant\BackendTypes; | |
| class CertificateUploadService extends BaseService | |
| { | |
| use MakeSingletonTrait; | |
| private function __construct() | |
| { | |
| $this->logger = AMSLogger::getLogger('exam-controller-log'); | |
| } | |
| /** | |
| * Save Certificate Upload | |
| * @param $certificateUpload | |
| * @return $id | |
| */ | |
| public function saveCertificateUpload( $certificateUpload){ | |
| $certificateUpload = $this->realEscapeObject($certificateUpload); | |
| $certificateUpload->createdBy = $GLOBALS['userId']; | |
| $certificateUpload->updatedBy = $GLOBALS['userId']; | |
| $staffId = $GLOBALS['userId']; | |
| try { | |
| $this->validatesaveCertificateUpload($certificateUpload); | |
| $certificateUpload->id = $this->insertCertificateUpload($certificateUpload); | |
| $this->logger->info(Events::EC_SAVE_CERTIFICATE_UPLOAD, [ | |
| "staff" => new Staff(["id" => $staffId]), | |
| "request" => $certificateUpload, | |
| "status" => StatusConstants::SUCCESS | |
| ]); | |
| } catch (\Exception $e) { | |
| $this->logger->error(Events::EC_SAVE_CERTIFICATE_UPLOAD, [ | |
| "staff" => new Staff(["id" => $staffId]), | |
| "request" => $certificateUpload, | |
| "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 upload certificate! Please try again"); | |
| } else if ($e->getCode() === ExamControllerException::DUPLICATE_ENTRY) { | |
| throw new ExamControllerException(ExamControllerException::DUPLICATE_ENTRY, "Cannot save upload certificate."); | |
| } else { | |
| throw new ExamControllerException($e->getCode(), $e->getMessage()); | |
| } | |
| } | |
| return $certificateUpload->id; | |
| } | |
| /** | |
| * Validate save upload Certificate | |
| * @param $certificateUpload | |
| * @return NULL | |
| */ | |
| private function validatesaveCertificateUpload($certificateUpload){ | |
| if (empty($certificateUpload->studentId) || empty($certificateUpload->groupId) || empty($certificateUpload->resourseId) || empty($certificateUpload->type)) | |
| throw new ExamControllerException(ExamControllerException::EMPTY_PARAMETERS, "empty parameter"); | |
| } | |
| /** | |
| * insert Transcript Application | |
| * @param $certificateUpload | |
| * @return $id | |
| */ | |
| private function insertCertificateUpload($certificateUpload){ | |
| $properties = !empty($certificateUpload->properties) ? "'" . json_encode($certificateUpload->properties) . "'" : "'{}'"; | |
| $query = "INSERT INTO ec_certificate_uploads | |
| (student_id,groups_id,type,resourse_id,properties,created_by,updated_by) | |
| VALUES | |
| ('$certificateUpload->studentId','$certificateUpload->groupId','$certificateUpload->type','$certificateUpload->resourseId',$properties,'$certificateUpload->createdBy','$certificateUpload->updatedBy')"; | |
| try { | |
| $certificateUpload->id = $this->executeQuery($query,true)->id; | |
| return $certificateUpload->id; | |
| } catch (\Exception $e) { | |
| throw new ExamControllerException($e->getCode(), $e->getMessage()); | |
| } | |
| } | |
| /** | |
| * Delete CertificateUpload | |
| * @param String $id | |
| * @return NULL | |
| */ | |
| public function deleteCertificateUpload($certificateUpload){ | |
| $certificateUpload = $this->realEscapeObject($certificateUpload); | |
| if (empty($certificateUpload->studentId) || empty($certificateUpload->groupId)){ | |
| throw new ExamControllerException(ExamControllerException::EMPTY_PARAMETERS, "Empty parameter.Can't delete "); | |
| } | |
| if (!empty($certificateUpload->studentId)) { | |
| $whereQuery .= " AND student_id = '$certificateUpload->studentId' "; | |
| } | |
| if (!empty($certificateUpload->groupId)) { | |
| $whereQuery .= " AND groups_id = '$certificateUpload->groupId' "; | |
| } | |
| if (!empty($certificateUpload->resourseId)) { | |
| $whereQuery .= " AND resourse_id = '$certificateUpload->resourseId' "; | |
| } | |
| $query = "DELETE FROM | |
| ec_certificate_uploads | |
| WHERE | |
| 1 = 1 "; | |
| try { | |
| $this->executeQuery($query.$whereQuery); | |
| // LOGGING | |
| $this->logger->info(Events::EC_DELETE_CERTIFICATE_UPLOAD,[ | |
| "staff" => new Staff(["id" => $staffId]), | |
| "request" => $certificateUpload, | |
| "status" => StatusConstants::SUCCESS | |
| ]); | |
| }catch (\Exception $e) { | |
| throw new ExamControllerException(ExamControllerException::HAVE_RELATION,"Error deleting mderation rule! Please try again"); | |
| } | |
| } | |
| /** | |
| * get CertificateUpload | |
| * @param $searchRequest | |
| * @return $certificates | |
| */ | |
| public function getCertificateUpload($searchRequest){ | |
| $searchRequest = $this->realEscapeObject($searchRequest); | |
| try { | |
| $whereQuery = null; | |
| $whereQuery = ""; | |
| if (!empty($searchRequest->studentId)) { | |
| $studentIdString = is_array($searchRequest->studentId) ? "'" . implode("','", $searchRequest->studentId) . "'" : "'" . $searchRequest->studentId . "'"; | |
| $whereQuery .= " AND ecu.student_id IN ( $studentIdString )"; | |
| } | |
| if (!empty($searchRequest->id)) { | |
| $idString = is_array($searchRequest->id) ? "'" . implode("','", $searchRequest->id) . "'" : "'" . $searchRequest->id . "'"; | |
| $whereQuery .= " AND ecu.id IN ( $idString )"; | |
| } | |
| if (!empty($searchRequest->type)) { | |
| $typeString = is_array($searchRequest->type) ? "'" . implode("','", $searchRequest->type) . "'" : "'" . $searchRequest->type . "'"; | |
| $whereQuery .= " AND ecu.type IN ( $typeString )"; | |
| } | |
| if (!empty($searchRequest->groupId)) { | |
| $groupIdString = is_array($searchRequest->groupId) ? "'" . implode("','", $searchRequest->groupId) . "'" : "'" . $searchRequest->groupId . "'"; | |
| $whereQuery .= " AND ecu.groups_id IN ( $groupIdString )"; | |
| } | |
| if (!empty($searchRequest->resourseId)) { | |
| $resourseIdString = is_array($searchRequest->resourseId) ? "'" . implode("','", $searchRequest->resourseId) . "'" : "'" . $searchRequest->resourseId . "'"; | |
| $whereQuery .= " AND ecu.resourse_id IN ( $resourseIdString )"; | |
| } | |
| if (!empty($searchRequest->examRegistrationId)) { | |
| $examRegistrationIdString = is_array($searchRequest->examRegistrationId) ? "'" . implode("','", $searchRequest->examRegistrationId) . "'" : "'" . $searchRequest->examRegistrationId . "'"; | |
| $whereQuery .= " AND ecu.properties->>'$.examRegistrationId' IN ( $examRegistrationIdString )"; | |
| } | |
| if (!empty($searchRequest->assessmentId)) { | |
| $assessmentIdString = is_array($searchRequest->assessmentId) ? "'" . implode("','", $searchRequest->assessmentId) . "'" : "'" . $searchRequest->assessmentId . "'"; | |
| $whereQuery .= " AND ecu.properties->>'$.assessmentId' IN ( $assessmentIdString )"; | |
| } | |
| $query = "SELECT | |
| DISTINCT | |
| ecu.id as id, | |
| ecu.student_id as studentId, | |
| ecu.groups_id as groupId, | |
| ecu.resourse_id as resourseId, | |
| ecu.properties as properties, | |
| ecu.type as certificateType | |
| FROM | |
| ec_certificate_uploads ecu | |
| WHERE | |
| 1 = 1 "; | |
| $certificates = $this->executeQueryForList($query . $whereQuery); | |
| $allcertificates = []; | |
| foreach ($certificates as $certificate) { | |
| $getUrlRequest = new GetPreSignedUrlRequest(); | |
| $getUrlRequest->resourceId = $certificate->resourseId; | |
| $getUrlRequest->backendType = BackendTypes::S3; | |
| $getUrlRequest->secretKey = getenv('AWS_CLIENT_SECRET_KEY'); | |
| $getUrlRequest->accessKey = getenv('AWS_ACCESS_KEY'); | |
| $url = ResourceService::getInstance()->getPreSignedUrlByResourceId($getUrlRequest); | |
| $certificate->name = $url->name; | |
| $certificate->url = $url->url; | |
| switch ($certificate->certificateType) { | |
| case 'SSLC': | |
| $allcertificates[$certificate->certificateType]->type = 'Sslc Certificate' ; | |
| break; | |
| case 'MARK_CARD': | |
| $allcertificates[$certificate->certificateType]->type = 'Mark list Certificate'; | |
| break; | |
| case 'PROJECT_THESIS': | |
| $allcertificates[$certificate->certificateType]->type = 'Project Thesis Report'; | |
| break; | |
| default: | |
| $allcertificates[$certificate->certificateType]->type = $certificate->certificateType; | |
| break; | |
| } | |
| $allcertificates[$certificate->certificateType]->certificates[] = $certificate; | |
| // $certificate->name = '123'; | |
| // $certificate->url = '123'; | |
| } | |
| $allcertificates = array_values($allcertificates); | |
| } catch (\Exception $e) { | |
| throw new ExamControllerException($e->getCode(), $e->getMessage()); | |
| } | |
| return $allcertificates; | |
| } | |
| } |