Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 10 |
CRAP | |
0.00% |
0 / 157 |
AsyncReportService | |
0.00% |
0 / 1 |
|
0.00% |
0 / 10 |
2352.00 | |
0.00% |
0 / 157 |
__construct | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 3 |
|||
__clone | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 2 |
|||
getInstance | |
0.00% |
0 / 1 |
6.00 | |
0.00% |
0 / 5 |
|||
saveAsyncReport | |
0.00% |
0 / 1 |
56.00 | |
0.00% |
0 / 25 |
|||
createAsyncReport | |
0.00% |
0 / 1 |
12.00 | |
0.00% |
0 / 9 |
|||
updateAsyncReport | |
0.00% |
0 / 1 |
12.00 | |
0.00% |
0 / 8 |
|||
getAsyncReport | |
0.00% |
0 / 1 |
306.00 | |
0.00% |
0 / 48 |
|||
deleteAsyncReport | |
0.00% |
0 / 1 |
42.00 | |
0.00% |
0 / 28 |
|||
deleteAsyncReportFromScheduler | |
0.00% |
0 / 1 |
42.00 | |
0.00% |
0 / 21 |
|||
updateAsyncReportHideFlag | |
0.00% |
0 / 1 |
6.00 | |
0.00% |
0 / 8 |
<?php | |
namespace com\linways\core\ams\professional\service; | |
use com\linways\core\ams\professional\dto\AsyncReport; | |
use com\linways\core\ams\professional\exception\ProfessionalException; | |
use com\linways\core\ams\professional\service\ResourceService; | |
use com\linways\core\ams\professional\request\GetAsyncReportRequest; | |
use com\linways\core\ams\professional\request\RemoveResourceRequest; | |
use com\linways\core\ams\professional\mapper\AsyncReportServiceMapper; | |
use com\linways\core\ams\professional\request\AsyncReportDeleteRequest; | |
class AsyncReportService extends BaseService | |
{ | |
// /Condition 1 - Presence of a static member variable | |
private static $_instance = null; | |
private $mapper = []; | |
// /Condition 2 - Locked down the constructor | |
private function __construct() | |
{ | |
$this->mapper = AsyncReportServiceMapper::getInstance()->getMapper(); | |
} | |
// Prevent any outside 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; | |
} | |
/** | |
* Save async report | |
* @param AsyncReport $asyncReport | |
* @return Object|AsyncReport | |
* @throws ProfessionalException | |
*/ | |
public function saveAsyncReport(AsyncReport $asyncReport) | |
{ | |
$asyncReport = $this->realEscapeObject($asyncReport); | |
if (empty($asyncReport->context)) { | |
throw new ProfessionalException(ProfessionalException::CONTEXT_CANNOT_BE_EMPTY, "Invalid Request! Context cannot be empty"); | |
} | |
if (gettype($asyncReport->reportDetails) === "array") { | |
$asyncReport->reportDetails = json_decode(json_encode($asyncReport->reportDetails)); | |
} else if (gettype($asyncReport->reportDetails) !== "object") { | |
throw new ProfessionalException(ProfessionalException::INVALID_REQUEST, "Invalid Request! Report details must be an object of AsyncReportDetails"); | |
} | |
if (empty($asyncReport->reportDetails->reportFileType)) { | |
throw new ProfessionalException(ProfessionalException::REPORT_TYPE_CANNOT_BE_EMPTY, "Invalid Request! Report type cannot be empty"); | |
} | |
try { | |
if ($asyncReport->asyncReportId) { | |
$this->updateAsyncReport($asyncReport); | |
} else { | |
$id = $this->createAsyncReport($asyncReport); | |
$asyncReport->asyncReportId = $id; | |
} | |
} catch (\Exception $e) { | |
throw new ProfessionalException(ProfessionalException::SAVING_ASYNC_REPORT_FAILED, "Saving async"); | |
} | |
return $asyncReport; | |
} | |
/** | |
* create new report entry | |
* @param AsyncReport $asyncReport | |
* @return Integer | |
* @throws ProfessionalException | |
*/ | |
private function createAsyncReport(AsyncReport $asyncReport) | |
{ | |
$query = "INSERT INTO async_reports (report_details,lin_resource_id,context,created_by,created_date,updated_by,updated_date,user_type) VALUES ('" . (json_encode($asyncReport->reportDetails)) . "'," . ($asyncReport->linResourceId ? "'" . $asyncReport->linResourceId . "'" : 'NULL') . ",'$asyncReport->context','$asyncReport->createdBy',UTC_TIMESTAMP(),'$asyncReport->updatedBy',UTC_TIMESTAMP(),'$asyncReport->createdByUserType')"; | |
try { | |
$id = $this->executeQueryForObject($query, true); | |
return $id; | |
} catch (\Exception $e) { | |
throw new ProfessionalException($e->getCode(), $e->getMessage()); | |
} | |
} | |
/** | |
* update existing report entry | |
* @param AsyncReport $asyncReport | |
* @return NULL | |
* @throws ProfessionalException | |
*/ | |
private function updateAsyncReport(AsyncReport $asyncReport) | |
{ | |
$query = "UPDATE async_reports SET report_details = '" . (json_encode($asyncReport->reportDetails)) . "',context='$asyncReport->context',updated_by='$asyncReport->updatedBy',updated_date=UTC_TIMESTAMP(),lin_resource_id=" . ($asyncReport->linResourceId ? "'" . $asyncReport->linResourceId . "'" : 'NULL') . " WHERE id='$asyncReport->asyncReportId'"; | |
try { | |
$this->executeQuery($query); | |
} catch (\Exception $e) { | |
throw new ProfessionalException($e->getCode(), $e->getMessage()); | |
} | |
} | |
/** | |
* get async report | |
* @param GetAsyncReportRequest $request | |
* @return Object|Array|AsyncReport|AsyncReport[] | |
* @throws ProfessioanlException | |
* @throws ProfessionalException | |
*/ | |
public function getAsyncReport(GetAsyncReportRequest $request) | |
{ | |
$request = $this->realEscapeObject($request); | |
$whereQuery = ""; | |
$limitQuery = ""; | |
if ($request->asyncReportId) { | |
$whereQuery .= " AND id='$request->asyncReportId'"; | |
} | |
if ($request->context) { | |
$whereQuery .= " AND context='$request->context'"; | |
} | |
if ($request->createdByUserType) { | |
$whereQuery .= " AND user_type='$request->createdByUserType'"; | |
} | |
if ($request->createdBy) { | |
$whereQuery .= " AND created_by='$request->createdBy'"; | |
} | |
if (!empty($request->hide) && $request->hide != null) { | |
$whereQuery .= " AND hide !='$request->hide'"; | |
} | |
if ($request->fromDate && $request->toDate) { | |
$whereQuery .= " AND DATE_FORMAT(CONVERT_TZ(updated_date,'+00:00',@@global.time_zone),'%Y-%m-%d') BETWEEN '$request->fromDate' AND '$request->toDate'"; | |
} else if ($request->fromDate) { | |
$whereQuery .= " AND DATE_FORMAT(CONVERT_TZ(updated_date,'+00:00',@@global.time_zone),'%Y-%m-%d') >= '$request->fromDate'"; | |
} else if ($request->toDate) { | |
$whereQuery .= " AND DATE_FORMAT(CONVERT_TZ(updated_date,'+00:00',@@global.time_zone),'%Y-%m-%d') <= '$request->toDate'"; | |
} | |
if ($request->startIndex !== "" && $request->endIndex) { | |
$limitQuery .= " LIMIT $request->startIndex,$request->endIndex"; | |
} | |
if ($request->onlyUploadedReports) { | |
$whereQuery .= " AND lin_resource_id IS NOT NULL"; | |
} | |
if($request->uploadedByName) | |
{ | |
$whereQuery .= " AND IF(ar.user_type='STAFF',sa.staffName,st.studentName) LIKE '%$request->uploadedByName%'"; | |
} | |
if($request->reportName) | |
{ | |
$whereQuery .= " AND ar.report_details->>\"$.reportName\" LIKE '%$request->reportName%'"; | |
} | |
$query = "SELECT ar.id,ar.report_details,ar.lin_resource_id,ar.context,DATE_FORMAT(CONVERT_TZ(ar.created_date,'+00:00',@@global.time_zone),'%d-%m-%Y %H:%i:%p') as created_date,DATE_FORMAT(CONVERT_TZ(ar.updated_date,'+00:00',@@global.time_zone),'%d-%m-%Y %H:%i:%p') as updated_date,IF(ar.user_type='STAFF',sa.staffName,st.studentName) AS createdUserName ,hide FROM async_reports ar LEFT JOIN staffaccounts sa ON sa.staffID=ar.created_by LEFT JOIN studentaccount st ON st.studentID=ar.created_by WHERE 1=1"; | |
$query .= $whereQuery . " ORDER BY ar.created_date DESC " . $limitQuery; | |
try { | |
return $this->executeQueryForList($query, $this->mapper[AsyncReportServiceMapper::GET_ASYNC_REPORTS]); | |
} catch (\Exception $e) { | |
throw new ProfessionalException(ProfessionalException::FETCHING_ASYNC_REPORT_FAILED, "Fetching async report failed!"); | |
} | |
} | |
/** | |
* delete async report | |
* @param AsyncReportDeleteRequest $request | |
* @return NULL | |
* @throws ProfessionalException | |
*/ | |
public function deleteAsyncReport(AsyncReportDeleteRequest $request) | |
{ | |
$request = $this->realEscapeObject($request); | |
$deleteIds = []; | |
$getAsyncReportRequest = new GetAsyncReportRequest(); | |
$getAsyncReportRequest->asyncReportId = $request->asyncReportId; | |
$getAsyncReportRequest->context = $request->context; | |
$getAsyncReportRequest->fromDate = $request->fromDate; | |
$getAsyncReportRequest->toDate = $request->toDate; | |
$getAsyncReportRequest->createdByUserType = $request->createdByUserType; | |
$getAsyncReportRequest->onlyUploadedReports = true; | |
try { | |
$asyncReports = $this->getAsyncReport($getAsyncReportRequest); | |
if (!empty($asyncReports)) { | |
$deleteIds = array_column($asyncReports, 'asyncReportId'); | |
$query = "DELETE FROM async_reports WHERE id IN (" . implode(',', $deleteIds) . ")"; | |
$this->executeQuery($query); | |
} | |
foreach ($asyncReports as $ar) { | |
$deleteResourceRequest = new RemoveResourceRequest(); | |
$deleteResourceRequest->resourceId = $ar->linResourceId; | |
$deleteResourceRequest->accessKey = getenv("AWS_ACCESS_KEY") ? getenv("AWS_ACCESS_KEY") : $request->awsAccessKey; | |
$deleteResourceRequest->secretKey = getenv("AWS_CLIENT_SECRET_KEY") ? getenv("AWS_CLIENT_SECRET_KEY") : $request->awsClientSecretKey; | |
ResourceService::getInstance()->removeResource($deleteResourceRequest); | |
} | |
} catch (\Exception $e) { | |
throw new ProfessionalException(ProfessionalException::ERROR_DELETING_ASYNC_REPORT, "Error deleting async report"); | |
} | |
} | |
/** | |
* delete async report from scheduler | |
*/ | |
public function deleteAsyncReportFromScheduler($request) | |
{ | |
$request = $this->realEscapeObject($request); | |
if (empty($request->context)) | |
throw new ProfessionalException(ProfessionalException::CONTEXT_CANNOT_BE_EMPTY, "Context cannot be empty"); | |
if (empty($request->noOfDays)) | |
throw new ProfessionalException(ProfessionalException::INVALID_DATE, "No of days for deleting the async report cannot be empty"); | |
if (empty($request->awsAccessKey)) | |
throw new ProfessionalException(ProfessionalException::INVALID_REQUEST, "Aws Access Key is empty! Please give aws access key"); | |
if (empty($request->awsClientSecretKey)) | |
throw new ProfessionalException(ProfessionalException::INVALID_REQUEST, "Aws Client Secret Key is empty! Please give aws client secret key"); | |
try { | |
$deleteRequest = new AsyncReportDeleteRequest (); | |
$deleteRequest->context = $request->context; | |
$deleteRequest->awsAccessKey = $request->awsAccessKey; | |
$deleteRequest->awsClientSecretKey = $request->awsClientSecretKey; | |
$deleteRequest->toDate = date('Y-m-d', strtotime('-' . $request->noOfDays . " days")); | |
$this->deleteAsyncReport($deleteRequest); | |
} catch (\Exception $e) { | |
throw new ProfessionalException($e->getCode(), $e->getMessage()); | |
} | |
} | |
/** | |
* update existing report entry hide flag | |
* @param AsyncReport $asyncReport | |
* @return NULL | |
* @throws ProfessionalException | |
*/ | |
public function updateAsyncReportHideFlag(AsyncReport $asyncReport) | |
{ | |
$query = "UPDATE async_reports SET hide = '$asyncReport->hide',updated_by='$asyncReport->updatedBy',updated_date=UTC_TIMESTAMP() WHERE id='$asyncReport->asyncReportId'"; | |
try { | |
$this->executeQuery($query); | |
} catch (\Exception $e) { | |
throw new ProfessionalException($e->getCode(), $e->getMessage()); | |
} | |
} | |
} |