ErrorManager.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. namespace ModulesGarden\Servers\ProxmoxCloudVps\Core\HandlerError;
  3. use ModulesGarden\Servers\ProxmoxCloudVps\Core\ServiceLocator;
  4. /**
  5. * Description of ErrorManager
  6. *
  7. * @author Rafał Ossowski <rafal.os@modulesgarden.com>
  8. */
  9. class ErrorManager
  10. {
  11. const TYPE_ERROR = "Error";
  12. const TYPE_WARNING = "Warning";
  13. protected static $lastToken = null;
  14. /**
  15. * @var \ModulesGarden\Servers\ProxmoxCloudVps\Core\HandlerError\Model\Error[]
  16. */
  17. protected static $errors = [];
  18. /**
  19. * @var \ModulesGarden\Servers\ProxmoxCloudVps\Core\HandlerError\Model\Warning[]
  20. */
  21. protected static $warnings = [];
  22. /**
  23. * @var \ModulesGarden\Servers\ProxmoxCloudVps\Core\HandlerError\WhmcsLogsHandler
  24. */
  25. protected static $whmcsLogger;
  26. /**
  27. * @var \ModulesGarden\Servers\ProxmoxCloudVps\Core\Interfaces\LoggerInterface
  28. */
  29. protected $logger;
  30. /**
  31. * @var bool
  32. */
  33. protected $withWarning = false;
  34. /**
  35. * @param Logger $logger
  36. */
  37. public function __construct(Logger $logger)
  38. {
  39. $this->logger = $logger;
  40. }
  41. /**
  42. * @param string $classname
  43. * @param string $message
  44. * @param array $trace
  45. */
  46. public function addError($classname = null, $message = '', $trace = [])
  47. {
  48. $error = $this->createNewModel(self::TYPE_ERROR, $classname, $message, $trace);
  49. $this->_addError($error)
  50. ->logger->addError($error->getFullMessage(), $error->getTrace());
  51. $this->getWhmcsLogger()->addModuleLog(
  52. [
  53. "type" => "ERROR",
  54. "class" => $error->getClass(),
  55. "message" => $error->getMessage(),
  56. "data" => $error->getTrace()
  57. ]
  58. );
  59. return $this;
  60. }
  61. public static function getLastErrorToken()
  62. {
  63. return self::$lastToken;
  64. }
  65. public static function setLastErrorToken($token)
  66. {
  67. self::$lastToken = $token;
  68. return self;
  69. }
  70. /**
  71. * @return \ModulesGarden\Servers\ProxmoxCloudVps\Core\HandlerError\Model\Error
  72. */
  73. public static function getFirstError()
  74. {
  75. return (count(self::$errors)>0)?self::$errors[0]:null;
  76. }
  77. /**
  78. * @param string $classname
  79. * @param string $message
  80. * @param array $trace
  81. */
  82. public function addWarning($classname = null, $message = '', $trace = [])
  83. {
  84. $warning = $this->createNewModel(self::TYPE_WARNING, $classname, $message, $trace);
  85. $this->_addWarning($warning)
  86. ->logger->addWarning($warning->getFullMessage(), $warning->getTrace());
  87. $this->getWhmcsLogger()->addModuleLog(
  88. [
  89. "type" => "WARNING",
  90. "class" => $warning->getClass(),
  91. "message" => $warning->getMessage(),
  92. "data" => $warning->getTrace()
  93. ]
  94. );
  95. return $this;
  96. }
  97. /**
  98. * @return \ModulesGarden\Servers\ProxmoxCloudVps\Core\HandlerError\Model\Error[]
  99. */
  100. public static function getErrors()
  101. {
  102. return self::$errors;
  103. }
  104. /**
  105. * @return \ModulesGarden\Servers\ProxmoxCloudVps\Core\HandlerError\Model\Warning[]
  106. */
  107. public static function getWarnings()
  108. {
  109. return self::$warnings;
  110. }
  111. public static function reset()
  112. {
  113. self::$warnings = [];
  114. self::$errors = [];
  115. return self;
  116. }
  117. /**
  118. * @return \ModulesGarden\Servers\ProxmoxCloudVps\Core\HandlerError\WhmcsLogsHandler
  119. */
  120. public function getWhmcsLogger()
  121. {
  122. if (isset(self::$whmcsLogger) === false)
  123. {
  124. self::$whmcsLogger = ServiceLocator::call("whmcsLogger");
  125. }
  126. return self::$whmcsLogger;
  127. }
  128. /**
  129. * @return bool
  130. */
  131. public function hesError()
  132. {
  133. return (bool) (count(self::$errors) != 0);
  134. }
  135. /**
  136. * @return bool
  137. */
  138. public function hesWarning()
  139. {
  140. return (bool) (count(self::$warnings) != 0);
  141. }
  142. public function withWarnings()
  143. {
  144. $this->withWarning = true;
  145. return $this;
  146. }
  147. public function withoutWarnings()
  148. {
  149. $this->withWarning = false;
  150. return $this;
  151. }
  152. /**
  153. * @return string
  154. */
  155. public function __toString()
  156. {
  157. $string = '';
  158. if (count(self::getErrors()) != 0 || (count(self::getWarnings()) != 0 ) && $this->withWarning)
  159. {
  160. $string .= '<div class="list-group">';
  161. foreach (self::getErrors() as $error)
  162. {
  163. $string .= '<div class="alert alert-danger">';
  164. $string .= '<h4>Class: <strong>';
  165. $string .= $error->getClass();
  166. $string .= '<span class="pull-right">' . $error->getDate() . " " . $error->getTime() . '</span>';
  167. $string .= '</strong></h4>';
  168. $string .= '<p>' . $error->getMessage() . '</p>';
  169. $string .= '</div>';
  170. }
  171. if ($this->withWarning)
  172. {
  173. foreach (self::getWarnings() as $warning)
  174. {
  175. $string .= '<div class="alert alert-warning">';
  176. $string .= '<h4>Class: <strong>';
  177. $string .= $warning->getClass();
  178. $string .= '<span class="pull-right">' . $warning->getDate() . " " . $warning->getTime() . '</span>';
  179. $string .= '</strong></h4>';
  180. $string .= '<p>' . $warning->getMessage() . '</p>';
  181. $string .= '</div>';
  182. }
  183. }
  184. $string .= '</div>';
  185. self::reset();
  186. }
  187. return $string;
  188. }
  189. private function createNewModel($type = self::TYPE_ERROR, $class = "", $massage = "", $trace = [])
  190. {
  191. return $this->{"createNewModel".$type}($class, $massage, $trace);
  192. }
  193. /**
  194. * @param string $class
  195. * @param string $massage
  196. * @param array $trace
  197. * @return \ModulesGarden\Servers\ProxmoxCloudVps\Core\HandlerError\Model\Error
  198. */
  199. private function createNewModelError($class = "", $massage = "", $trace = [])
  200. {
  201. return new \ModulesGarden\Servers\ProxmoxCloudVps\Core\HandlerError\Model\Error($class, $massage, $trace);
  202. }
  203. /**
  204. * @param string $class
  205. * @param string $massage
  206. * @param array $trace
  207. * @return \ModulesGarden\Servers\ProxmoxCloudVps\Core\HandlerError\Model\Warning
  208. */
  209. private function createNewModelWarning($class = "", $massage = "", $trace = [])
  210. {
  211. return new \ModulesGarden\Servers\ProxmoxCloudVps\Core\HandlerError\Model\Error($class, $massage, $trace);
  212. }
  213. protected function _addError(\ModulesGarden\Servers\ProxmoxCloudVps\Core\HandlerError\Model\Error $error)
  214. {
  215. array_push(self::$errors, $error);
  216. return $this;
  217. }
  218. protected function _addWarning(\ModulesGarden\Servers\ProxmoxCloudVps\Core\HandlerError\Model\Warning $warning)
  219. {
  220. array_push(self::$warnings, $warning);
  221. return $this;
  222. }
  223. }