Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 40 |
LogEntityEnricherService | |
0.00% |
0 / 1 |
|
0.00% |
0 / 3 |
110.00 | |
0.00% |
0 / 40 |
enrichEntity | |
0.00% |
0 / 1 |
30.00 | |
0.00% |
0 / 25 |
|||
identifyAndEnrichEntities | |
0.00% |
0 / 1 |
20.00 | |
0.00% |
0 / 13 |
|||
deepCloneInefficientDoNotUseInLiveCode | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 2 |
<?php | |
namespace com\linways\core\ams\professional\service; | |
use com\linways\base\util\MakeSingletonTrait; | |
class LogEntityEnricherService extends BaseService | |
{ | |
use MakeSingletonTrait; | |
/** | |
* @param $entity | |
* @return mixed | |
* @throws \Exception | |
*/ | |
public function enrichEntity($entity){ | |
if (!class_exists($entity->className)) { | |
error_log("Logger:enrichEntity: The class $entity->className does not exist."); | |
return $entity; | |
} | |
$entityInstance = new $entity->className(); | |
if (!method_exists($entityInstance, 'getQuery')) { | |
error_log("Logger:enrichEntity: The class $entity->className does not have getQuery method."); | |
return $entity; | |
} | |
$entityInstance->import($entity); | |
try{ | |
$query = $entityInstance->getQuery(); | |
}catch(\Throwable $e){ | |
error_log("Could not create the query for logging entity"); | |
throw $e; | |
} | |
if(!empty($query)){ | |
$result = $this->executeQueryForObject($query); | |
$entityInstance->import($result); | |
} | |
unset($entityInstance->isDefinedEntity); | |
unset($entityInstance->className); | |
$entityInstance->entity = basename(str_replace('\\', '/', get_class($entityInstance))); | |
// get_object_vars is used to prevent adding className to the entity object automatically. | |
return get_object_vars($entityInstance); | |
} | |
/** | |
* Identifies predefined entities in the log data and enriches (add more info about that entity) it | |
* @param $rawLogData | |
* @return mixed | |
* @throws \Exception | |
*/ | |
public function identifyAndEnrichEntities($rawLogData){ | |
$logData = $this->deepCloneInefficientDoNotUseInLiveCode($rawLogData); | |
foreach ($logData->context as $key => &$value){ | |
// TODO: Identify nested entities and populate them with extra details | |
if(isset($value->isDefinedEntity)){ | |
try{ | |
$value = $this->enrichEntity($value); | |
}catch (\Exception $e){ | |
error_log('log enrich failed' . $e->getMessage()); | |
throw $e; | |
} | |
} | |
} | |
return $logData; | |
} | |
private function deepCloneInefficientDoNotUseInLiveCode($obj){ | |
return unserialize(serialize($obj)); | |
} | |
} |