Entity.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. <?php
  2. namespace ModulesGarden\Servers\ProxmoxVps\Core\Logger;
  3. use ModulesGarden\Servers\ProxmoxVps\Core\Models\Logger\Model as LoggerModel;
  4. use \Carbon\Carbon;
  5. /**
  6. * Description of AbstractLogger
  7. *
  8. * @author Rafał Ossowski <rafal.os@modulesgarden.com>
  9. */
  10. class Entity
  11. {
  12. // Log Type
  13. const TYPE_DEBUG = "debug";
  14. const TYPE_ERROR = "error";
  15. const TYPE_INFO = "info";
  16. const TYPE_SUCCESS = "success";
  17. const TYPE_CRITICAL = "critical";
  18. // Log Level
  19. const LEVEL_LOW = "low";
  20. const LEVEL_MEDIUM = "medium";
  21. const LEVEL_HIGHT = "hight";
  22. /**
  23. * @var LoggerModel
  24. */
  25. protected $model;
  26. //data
  27. protected $id;
  28. protected $idRef;
  29. protected $idType;
  30. protected $type;
  31. protected $level;
  32. protected $date;
  33. protected $request;
  34. protected $response;
  35. protected $beforeVars;
  36. protected $vars;
  37. protected $reference;
  38. protected $message;
  39. // static field
  40. protected static $fields = [
  41. 'id' => 'id',
  42. 'id_ref' => 'idRef',
  43. 'id_type' => 'idType',
  44. 'type' => 'type',
  45. 'level' => 'level',
  46. 'date' => 'date',
  47. 'request' => 'request',
  48. 'response' => 'response',
  49. 'before_vars' => 'beforeVars',
  50. 'vars' => 'vars'
  51. ];
  52. protected static $types = [
  53. 'debug' => "Debug",
  54. 'error' => "Error",
  55. 'info' => "Info",
  56. 'success' => "Success",
  57. 'critical' => "Critical"
  58. ];
  59. protected static $levels = [
  60. 'low' => "Low",
  61. 'medium' => "Medium",
  62. 'hight' => "Hight"
  63. ];
  64. public function __construct(LoggerModel $model)
  65. {
  66. $this->model = $model;
  67. }
  68. public function find($id)
  69. {
  70. $data = $this->getModel()->find($id);
  71. $this->loadData($data->toArray());
  72. $this->loadMessage();
  73. $this->model = $data;
  74. return $this;
  75. }
  76. public function getModel()
  77. {
  78. if ($this->model === null)
  79. {
  80. $this->model = new LoggerModel();
  81. }
  82. return $this->model;
  83. }
  84. /**
  85. * @param string $type
  86. * @return Entity[]
  87. */
  88. public function all($type = null)
  89. {
  90. $model = clone ($this->getModel());
  91. if ($type !== null)
  92. {
  93. $model = $model->where("type", "LIKE", $type);
  94. }
  95. $collection = [];
  96. foreach ($model->get()->toArray() as $record)
  97. {
  98. $item = clone $this;
  99. $collection[] = $item->loadData($record)->loadMessage();
  100. }
  101. return $collection;
  102. }
  103. // ToDo: To powinno być w innej klasie o nazwie collection albo repozitory
  104. public function getReferenceLogs($isAllType = false)
  105. {
  106. $model = clone ($this->getModel());
  107. if ($this->type !== null && $isAllType === true)
  108. {
  109. $model = $model->where("type", "LIKE", $this->type);
  110. }
  111. $model = $model->where("id_ref", "LIKE", $this->idRef)
  112. ->where("id_type", "LIKE", $this->idType);
  113. return $model->get();
  114. }
  115. public function getTypeLabel()
  116. {
  117. return self::$types[$this->type];
  118. }
  119. public function getType()
  120. {
  121. return $this->type;
  122. }
  123. public function getLevelLabel()
  124. {
  125. return self::$levels[$this->level];
  126. }
  127. public function getReference()
  128. {
  129. if ($this->reference === null && $this->idType)
  130. {
  131. if ($reference = $this->getModel()->find($this->id)->reference())
  132. {
  133. $this->reference = $reference->toArray();
  134. }
  135. }
  136. return $this->reference;
  137. }
  138. public function getRequest()
  139. {
  140. return $this->request;
  141. }
  142. public function getResponse()
  143. {
  144. return $this->response;
  145. }
  146. public function getDate()
  147. {
  148. return $this->date;
  149. }
  150. public function getBeforeVars()
  151. {
  152. return $this->beforeVars;
  153. }
  154. public function getVars()
  155. {
  156. return $this->vars;
  157. }
  158. public function getReferenceId()
  159. {
  160. return $this->idRef;
  161. }
  162. public function getReferenceNamespace()
  163. {
  164. return $this->idType;
  165. }
  166. public function getMessage()
  167. {
  168. return $this->message;
  169. }
  170. public function setReference($id, $className)
  171. {
  172. if ($className !== null)
  173. {
  174. $this->idRef = $id;
  175. $this->idType = $className;
  176. }
  177. return $this;
  178. }
  179. public function setLevel($level)
  180. {
  181. $this->level = $level;
  182. return $this;
  183. }
  184. public function setType($type)
  185. {
  186. $this->type = $type;
  187. return $this;
  188. }
  189. public function setRequest($request)
  190. {
  191. $this->request = $request;
  192. return $this;
  193. }
  194. public function setResponse($response)
  195. {
  196. $this->response = $response;
  197. return $this;
  198. }
  199. public function setMessage($message = "")
  200. {
  201. $this->message = $message;
  202. return $this;
  203. }
  204. public function setBeforeVars($vars)
  205. {
  206. $this->beforeVars = $vars;
  207. return $this;
  208. }
  209. public function setVars($vars)
  210. {
  211. $this->vars = $vars;
  212. return $this;
  213. }
  214. protected function loadMessage()
  215. {
  216. if (is_array($this->response))
  217. {
  218. $this->message = $this->response['message_base'];
  219. unset($this->response['message_base']);
  220. }
  221. elseif (is_string($this->response))
  222. {
  223. $this->message = $this->response;
  224. $this->response = null;
  225. }
  226. elseif (is_object($this->response))
  227. {
  228. $this->message = $this->response->message;
  229. unset($this->response->message);
  230. }
  231. return $this;
  232. }
  233. protected function loadData(array $data = [])
  234. {
  235. foreach ($data as $name => $row)
  236. {
  237. $name = $this->getPropertyName($name);
  238. if (in_array($name, ['vars', 'beforeVars', 'response', 'request'],true))
  239. {
  240. $newData = json_decode($row, true);
  241. if ($newData === false)
  242. {
  243. $newData = unserialize($newData);
  244. }
  245. if ($newData !== false)
  246. {
  247. $this->$name = $newData;
  248. }
  249. else
  250. {
  251. $this->$name = $row;
  252. }
  253. }
  254. else
  255. {
  256. $this->$name = $row;
  257. }
  258. }
  259. return $this;
  260. }
  261. protected function getDataToSave()
  262. {
  263. $data = [];
  264. foreach (self::$fields as $key => $property)
  265. {
  266. if ($property === "response")
  267. {
  268. if (is_array($this->$property))
  269. {
  270. $this->{$property}['message_base'] = $this->message;
  271. }
  272. elseif (is_string($this->$property) || empty($this->$property))
  273. {
  274. $this->$property = $this->message;
  275. }
  276. elseif (is_object($this->$property))
  277. {
  278. $this->{$property}->message = $this->message;
  279. }
  280. }
  281. $item = $this->convertData($this->$property);
  282. if ($item)
  283. {
  284. $data[$key] = $item;
  285. }
  286. }
  287. return $data;
  288. }
  289. private function getPropertyName($columnName)
  290. {
  291. return self::$fields[$columnName];
  292. }
  293. protected function convertData($value)
  294. {
  295. if (is_array($value))
  296. {
  297. $value = json_encode($value);
  298. }
  299. elseif (is_object($value))
  300. {
  301. $value = serialize($value);
  302. }
  303. return $value;
  304. }
  305. public function save()
  306. {
  307. $model = $this->getModel();
  308. if ($this->id)
  309. {
  310. $model->find($this->id)->update($this->getDataToSave());
  311. }
  312. else
  313. {
  314. $data = $this->getDataToSave();
  315. $data['date'] = Carbon::now();
  316. $model->create($data);
  317. }
  318. return $this;
  319. }
  320. public function toArray()
  321. {
  322. return [
  323. 'id' => $this->id,
  324. //'ref' => print_r($this->getReference(), true),
  325. 'ref_id' => $this->getReferenceId(),
  326. 'ref_type' => $this->getReferenceNamespace(),
  327. 'message' => $this->getMessage(),
  328. 'type' => $this->getType(),
  329. 'typeLabel' => $this->getTypeLabel(),
  330. 'level' => $this->getLevelLabel(),
  331. 'request' => $this->getRequest(),
  332. 'response' => $this->getResponse(),
  333. 'before_vars' => $this->getBeforeVars(),
  334. 'vars' => $this->getVars(),
  335. 'date' => $this->getDate()
  336. ];
  337. }
  338. public function toStdClass()
  339. {
  340. $data = new \stdClass();
  341. $data->id = $this->id;
  342. $data->ref_id = $this->getReferenceId();
  343. $data->ref_type = $this->getReferenceNamespace();
  344. $data->message = $this->getMessage();
  345. $data->type = $this->getType();
  346. $data->typeLabel = $this->getTypeLabel();
  347. $data->level = $this->getLevelLabel();
  348. $data->request = $this->getRequest();
  349. $data->response = $this->getResponse();
  350. $data->before_vars = $this->getBeforeVars();
  351. $data->vars = $this->getVars();
  352. $data->date = $this->getDate();
  353. return $data;
  354. }
  355. public static function convertToId($data)
  356. {
  357. if (is_array($data) && isset($data['id']))
  358. {
  359. return $data['id'];
  360. }
  361. elseif (is_object($data) && method_exists($data, 'getKeyName'))
  362. {
  363. return $data->{$data->getKeyName()};
  364. }
  365. elseif (is_object($data) && $data->id !== null)
  366. {
  367. return $data->id;
  368. }
  369. }
  370. public static function convertToClassName($data)
  371. {
  372. if (is_array($data) && isset($data['className']))
  373. {
  374. return $data['className'];
  375. }
  376. elseif (is_object($data))
  377. {
  378. $class = get_class($data);
  379. return $class;
  380. }
  381. }
  382. }