Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 153 |
| ValuationMethodService | |
0.00% |
0 / 1 |
|
0.00% |
0 / 8 |
1260.00 | |
0.00% |
0 / 153 |
| __construct | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 2 |
|||
| saveValuationMethod | |
0.00% |
0 / 1 |
56.00 | |
0.00% |
0 / 25 |
|||
| validateSaveValuationMethodRequest | |
0.00% |
0 / 1 |
6.00 | |
0.00% |
0 / 4 |
|||
| insertValuationMethod | |
0.00% |
0 / 1 |
20.00 | |
0.00% |
0 / 15 |
|||
| updateValuationMethod | |
0.00% |
0 / 1 |
12.00 | |
0.00% |
0 / 17 |
|||
| deleteValuationMethod | |
0.00% |
0 / 1 |
12.00 | |
0.00% |
0 / 19 |
|||
| restoreValuationMethod | |
0.00% |
0 / 1 |
12.00 | |
0.00% |
0 / 19 |
|||
| searchValuationMethod | |
0.00% |
0 / 1 |
156.00 | |
0.00% |
0 / 52 |
|||
| <?php | |
| namespace com\linways\ec\core\service; | |
| use com\linways\base\util\SecurityUtils; | |
| use com\linways\base\util\MakeSingletonTrait; | |
| use com\linways\ec\core\exception\ECCoreException; | |
| use com\linways\ec\core\request\SearchValuationMethodRequest; | |
| use com\linways\ec\core\mapper\ValuationMethodServiceMapper; | |
| use com\linways\ec\core\constant\FilterModeConstants; | |
| use com\linways\ec\core\constant\StatusConstants; | |
| use com\linways\ec\core\dto\ValuationMethod; | |
| class ValuationMethodService extends BaseService | |
| { | |
| use MakeSingletonTrait; | |
| private function __construct() { | |
| $this->mapper = ValuationMethodServiceMapper::getInstance()->getMapper(); | |
| } | |
| /** | |
| * Save valuationMethod | |
| * @param ValuationMethod $valuationMethod | |
| * @return String $id | |
| */ | |
| public function saveValuationMethod (ValuationMethod $valuationMethod) | |
| { | |
| $valuationMethod = $this->realEscapeObject($valuationMethod); | |
| $valuationMethod->createdBy = $GLOBALS['userId'] ?? $valuationMethod->createdBy; | |
| $valuationMethod->updatedBy = $GLOBALS['userId'] ?? $valuationMethod->updatedBy; | |
| try{ | |
| $this->validateSaveValuationMethodRequest($valuationMethod); | |
| if(!empty($valuationMethod->id)) | |
| { | |
| $valuationMethod->id = $this->updateValuationMethod($valuationMethod); | |
| } | |
| else | |
| { | |
| $valuationMethod->id = $this->insertValuationMethod($valuationMethod); | |
| } | |
| }catch(\Exception $e) { | |
| if($e->getCode() !== ECCoreException::INVALID_PARAMETERS && $e->getCode() !== ECCoreException::EMPTY_PARAMETERS && $e->getCode() !== ECCoreException::DUPLICATE_ENTRY) { | |
| throw new ECCoreException(ECCoreException::ERROR_SAVING_EVALUTION_METHOD,"Failed to save valuation method! Please try again"); | |
| } else if ($e->getCode() === ECCoreException::DUPLICATE_ENTRY) { | |
| throw new ECCoreException (ECCoreException::DUPLICATE_ENTRY,"Cannot create valuation method.".$valuationMethod->name." already exists!"); | |
| } else { | |
| throw new ECCoreException ($e->getCode(),$e->getMessage()); | |
| } | |
| } | |
| return $valuationMethod->id; | |
| } | |
| /** | |
| * Validate ValuationMethod Request Before Saving | |
| * @param ValuationMethod $valuationMethod | |
| * @return NULL | |
| */ | |
| private function validateSaveValuationMethodRequest($valuationMethod) | |
| { | |
| if(empty($valuationMethod->name)) | |
| throw new ECCoreException(ECCoreException::EMPTY_PARAMETERS,"Valuation method name is empty! Please enter a name for valuation method"); | |
| //TODO: Validate type if updating | |
| } | |
| /** | |
| * Insert valuationMethod | |
| * @param ValuationMethod $valuationMethod | |
| * @return String $id | |
| */ | |
| private function insertValuationMethod(ValuationMethod $valuationMethod) | |
| { | |
| $properties = !empty($valuationMethod->properties) ? "'".json_encode($valuationMethod->properties)."'" : "NULL"; | |
| $identifyingContext = !empty($valuationMethod->identifyingContext) ? "'" . json_encode($valuationMethod->identifyingContext) . "'" : "NULL"; | |
| $id = SecurityUtils::getRandomString(); | |
| $query = "INSERT INTO valution_method | |
| (id,identifying_context,name,properties,created_by,updated_by) | |
| VALUES | |
| ('$id',$identifyingContext,'$valuationMethod->name','$valuationMethod->type',$properties,'$valuationMethod->createdBy','$valuationMethod->updatedBy')"; | |
| try { | |
| $this->executeQuery($query); | |
| return $id; | |
| } catch (\Exception $e) { | |
| throw new ECCoreException($e->getCode(),$e->getMessage()); | |
| } | |
| } | |
| /** | |
| * Update ValuationMethod | |
| * @param ValuationMethod $valuationMethod | |
| * @return NULL | |
| */ | |
| private function updateValuationMethod(ValuationMethod $valuationMethod) | |
| { | |
| $properties = !empty($valuationMethod->properties) ? "'".json_encode($valuationMethod->properties)."'" : "NULL"; | |
| $query = "UPDATE | |
| valution_method | |
| SET | |
| name = '$valuationMethod->name', | |
| properties = $properties, | |
| updated_by = '$valuationMethod->updatedBy' | |
| WHERE | |
| id = '$valuationMethod->id'"; | |
| try { | |
| $this->executeQuery($query); | |
| return $valuationMethod->id; | |
| } catch (\Exception $e) { | |
| throw new ECCoreException($e->getCode(), $e->getMessage()); | |
| } | |
| } | |
| /** | |
| * Delete ValuationMethod (Soft Delete) | |
| * @param String $id | |
| * @return NULL | |
| */ | |
| public function deleteValuationMethod($id) | |
| { | |
| $id = $this->realEscapeString($id); | |
| $updatedBy = $GLOBALS['userId']; | |
| if(empty($id)) { | |
| throw new ECCoreException(ECCoreException::EMPTY_PARAMETERS,"No valuation method selected! Please select a valuationMethod to delete"); | |
| } | |
| //TODO: Do validation before deleting | |
| $query = "UPDATE | |
| valution_method | |
| SET | |
| trashed = UTC_TIMESTAMP(), | |
| updated_by = '$updatedBy' | |
| WHERE | |
| id = '$id'"; | |
| try { | |
| $this->executeQuery($query); | |
| } catch (\Exception $e) { | |
| throw new ECCoreException(ECCoreException::ERROR_DELETING_EVALUTION_METHOD,"Error deleting valuation method! Please try again"); | |
| } | |
| } | |
| /** | |
| * Restore ValuationMethod | |
| * @param String $id | |
| * @return NULL | |
| */ | |
| public function restoreValuationMethod($id) | |
| { | |
| $id = $this->realEscapeString($id); | |
| $updatedBy = $GLOBALS['userId']; | |
| if(empty($id)) { | |
| throw new ECCoreException(ECCoreException::EMPTY_PARAMETERS,"No valuation method selected! Please select a valuationMethod to restore"); | |
| } | |
| $query = "UPDATE | |
| valution_method | |
| SET | |
| trashed = NULL, | |
| updated_by = '$updatedBy' | |
| WHERE | |
| id = '$id'"; | |
| try { | |
| $this->executeQuery($query); | |
| } catch (\Exception $e) { | |
| throw new ECCoreException(ECCoreException::ERROR_RESTORING_EVALUTION_METHOD,"Error restoring valuation method! Please try again"); | |
| } | |
| } | |
| /** | |
| * Search ValuationMethod Details | |
| * @param SearchValuationMethodRequest $request | |
| * @return ValuationMethod | |
| */ | |
| public function searchValuationMethod(SearchValuationMethodRequest $request) | |
| { | |
| $request = $this->realEscapeObject($request); | |
| $whereQuery = ""; | |
| $limitQuery = ""; | |
| if($request->trashed === StatusConstants::ACTIVE) { | |
| $whereQuery .= ""; | |
| } | |
| if($request->trashed === StatusConstants::TRASHED) { | |
| $whereQuery .= ""; | |
| } | |
| if(!empty($request->id)) { | |
| $whereQuery .= " AND vm.id='$request->id' "; | |
| } | |
| if(!empty($request->name)) { | |
| $whereQuery .= " AND vm.name LIKE '%$request->name%' "; | |
| } | |
| if(!empty($request->identifyingContext)) { | |
| $identifyingContext = is_object($request->identifyingContext) | |
| ? "'" . json_encode($request->identifyingContext) . "'" | |
| : is_string($request->identifyingContext) | |
| ? "'$request->identifyingContext'" | |
| : "'{}'"; | |
| $whereQuery .= " AND JSON_CONTAINS(vm.identifying_context, $identifyingContext, '$') "; | |
| if ($request->identifyingContext == FilterModeConstants::STRICT) { | |
| $whereQuery .= " AND CAST(vm.identifying_context AS JSON) = CAST($identifyingContext AS JSON) "; | |
| } | |
| } | |
| if($request->startIndex !== "" && $request->endIndex !== "") | |
| { | |
| $limitQuery .= " LIMIT $request->startIndex,$request->endIndex"; | |
| } | |
| $query = "SELECT | |
| vm.id, | |
| vm.identifying_context, | |
| vm.name, | |
| vm.properties, | |
| vm.trashed, | |
| vm.created_by, | |
| vm.created_date, | |
| vm.updated_by, | |
| vm.updated_date | |
| FROM | |
| valution_method vm | |
| WHERE | |
| 1 = 1"; | |
| try { | |
| $valuationMethods = $this->executeQueryForList($query.$whereQuery.$limitQuery,$this->mapper[ValuationMethodServiceMapper::SEARCH_VALUATION_METHOD]); | |
| } catch (\Exception $e) { | |
| throw new ECCoreException(ECCoreException::ERROR_FETCHING_EVALUTION_METHOD,"Cannot fetch valuation method details! Please try again"); | |
| } | |
| return $valuationMethods; | |
| } | |
| } |