PageRenderTime 42ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/decorators/ProductAPILogger.php

https://github.com/octolab/Ecwid
PHP | 425 lines | 269 code | 24 blank | 132 comment | 20 complexity | 168063a798da6a13921847005de0718b MD5 | raw file
  1. <?php
  2. /**
  3. * @author Samigullin Kamil <feedback@kamilsk.com>
  4. * @link http://www.kamilsk.com/
  5. */
  6. namespace Ecwid\decorators;
  7. use Ecwid\interfaces\iProductAPI,
  8. \DateTime,
  9. \Exception;
  10. /**
  11. * @package Ecwid.decorators
  12. * @since 1.0
  13. */
  14. class ProductAPILogger extends ProductAPIDecorator
  15. {
  16. /**
  17. * @var array
  18. */
  19. protected static $_defaultConfig = array(
  20. 'log_path' => __DIR__,
  21. 'levels' => array('error'),
  22. 'enabled' => true,
  23. );
  24. /**
  25. * Constructor.
  26. *
  27. * @param iProductAPI $product_api
  28. * @param array $config
  29. * <code>
  30. * array(
  31. * 'log_path' => %s:path,
  32. * 'levels' => %a:list('error','debug'),
  33. * 'enabled' => %bool,
  34. * )
  35. * </code>
  36. * @throws Exception
  37. */
  38. public function __construct(iProductAPI $product_api, array $config = array())
  39. {
  40. if ($this->_initConfig(array_merge(self::$_defaultConfig, $config))) {
  41. parent::__construct($product_api);
  42. } else {
  43. throw new Exception('_init');
  44. }
  45. }
  46. /**
  47. * @return string
  48. */
  49. public function getLogPath()
  50. {
  51. return $this->_config['log_path'];
  52. }
  53. /**
  54. * @param string $path
  55. * @return self
  56. * @throws Exception
  57. */
  58. public function setLogPath($path)
  59. {
  60. if ( ! is_string($path)) {
  61. throw new Exception('_invalid_type');
  62. }
  63. if (false === ($path = realpath($path)) or ! is_writable($path)) {
  64. throw new Exception('_invalid_path');
  65. }
  66. $this->_config['log_path'] = $path;
  67. return $this;
  68. }
  69. /**
  70. * @return array
  71. */
  72. public function getLevels()
  73. {
  74. return $this->_config['levels'];
  75. }
  76. /**
  77. * @param array|string $levels
  78. * @return self
  79. * @throws Exception
  80. */
  81. public function setLevels($levels)
  82. {
  83. if (is_string($levels)) {
  84. $levels = preg_split('/(\s+)?[,:\|](\s+)?|\s+/', $levels);
  85. }
  86. if ( ! is_array($levels) or empty($levels)) {
  87. throw new Exception('_invalid_type');
  88. }
  89. $this->_config['levels'] = array();
  90. $allowed = array('error', 'debug');
  91. foreach ($levels as $level) {
  92. if ( ! in_array($level, $allowed)) {
  93. throw new Exception('_invalid_level');
  94. }
  95. if ( ! in_array($level, $this->_config['levels'])) {
  96. array_push($this->_config['levels'], $level);
  97. }
  98. }
  99. return $this;
  100. }
  101. /**
  102. * @return bool
  103. */
  104. public function isEnabled()
  105. {
  106. return $this->_config['enabled'];
  107. }
  108. /**
  109. * @param bool $condition
  110. * @return void
  111. * @throws Exception
  112. */
  113. protected function _setCondition($condition)
  114. {
  115. if ( ! is_bool($condition)) {
  116. throw new Exception('_invalid_type');
  117. }
  118. $this->_config['enabled'] = $condition;
  119. }
  120. /**
  121. * @param array $config
  122. * @return bool
  123. * @throws Exception
  124. */
  125. protected function _initConfig(array $config)
  126. {
  127. $map = array(
  128. 'log_path' => 'setLogPath',
  129. 'levels' => 'setLevels',
  130. 'enabled' => '_setCondition',
  131. );
  132. $this->_silent = true;
  133. foreach ($config as $key => $value) {
  134. if (array_key_exists($key, $map)) {
  135. $set = $map[$key];
  136. $this->$set($value);
  137. }
  138. }
  139. $this->_silent = false;
  140. return true;
  141. }
  142. /* iProductAPI */
  143. /**
  144. * @param int|null $parent
  145. * @return mixed
  146. * @throws Exception
  147. */
  148. public function getCategories($parent = null)
  149. {
  150. try {
  151. $this->_log(__FUNCTION__, func_get_args());
  152. return $this->_product_api->getCategories($parent);
  153. } catch (Exception $e) {
  154. $this->_log(__FUNCTION__, func_get_args(), $e);
  155. throw $e;
  156. }
  157. }
  158. /**
  159. * @param int $id
  160. * @return mixed
  161. * @throws Exception
  162. */
  163. public function getCategory($id)
  164. {
  165. try {
  166. $this->_log(__FUNCTION__, func_get_args());
  167. return $this->_product_api->getCategory($id);
  168. } catch (Exception $e) {
  169. $this->_log(__FUNCTION__, func_get_args(), $e);
  170. throw $e;
  171. }
  172. }
  173. /**
  174. * @param int|null $category
  175. * @return mixed
  176. * @throws Exception
  177. */
  178. public function getProducts($category = null)
  179. {
  180. try {
  181. $this->_log(__FUNCTION__, func_get_args());
  182. return $this->_product_api->getProducts($category);
  183. } catch (Exception $e) {
  184. $this->_log(__FUNCTION__, func_get_args(), $e);
  185. throw $e;
  186. }
  187. }
  188. /**
  189. * @param int $id
  190. * @return mixed
  191. * @throws Exception
  192. */
  193. public function getProduct($id)
  194. {
  195. try {
  196. $this->_log(__FUNCTION__, func_get_args());
  197. return $this->_product_api->getProduct($id);
  198. } catch (Exception $e) {
  199. $this->_log(__FUNCTION__, func_get_args(), $e);
  200. throw $e;
  201. }
  202. }
  203. /**
  204. * @param mixed $data
  205. * @return mixed
  206. * @throws Exception
  207. */
  208. public function putProducts($data)
  209. {
  210. try {
  211. $this->_log(__FUNCTION__, func_get_args());
  212. return $this->_product_api->putProducts($data);
  213. } catch (Exception $e) {
  214. $this->_log(__FUNCTION__, func_get_args(), $e);
  215. throw $e;
  216. }
  217. }
  218. /**
  219. * @param int $id
  220. * @param mixed $data
  221. * @return mixed
  222. * @throws Exception
  223. */
  224. public function putProduct($id, $data)
  225. {
  226. try {
  227. $this->_log(__FUNCTION__, func_get_args());
  228. return $this->_product_api->putProduct($id, $data);
  229. } catch (Exception $e) {
  230. $this->_log(__FUNCTION__, func_get_args(), $e);
  231. throw $e;
  232. }
  233. }
  234. /**
  235. * @param int $count
  236. * @return mixed
  237. * @throws Exception
  238. */
  239. public function getRandomProducts($count)
  240. {
  241. try {
  242. $this->_log(__FUNCTION__, func_get_args());
  243. return $this->_product_api->getRandomProducts($count);
  244. } catch (Exception $e) {
  245. $this->_log(__FUNCTION__, func_get_args(), $e);
  246. throw $e;
  247. }
  248. }
  249. /**
  250. * @return mixed
  251. * @throws Exception
  252. */
  253. public function getClasses()
  254. {
  255. try {
  256. $this->_log(__FUNCTION__, func_get_args());
  257. return $this->_product_api->getClasses();
  258. } catch (Exception $e) {
  259. $this->_log(__FUNCTION__, func_get_args(), $e);
  260. throw $e;
  261. }
  262. }
  263. /**
  264. * @param int $id
  265. * @return mixed
  266. * @throws Exception
  267. */
  268. public function getClass($id)
  269. {
  270. try {
  271. $this->_log(__FUNCTION__, func_get_args());
  272. return $this->_product_api->getClass($id);
  273. } catch (Exception $e) {
  274. $this->_log(__FUNCTION__, func_get_args(), $e);
  275. throw $e;
  276. }
  277. }
  278. /**
  279. * @param mixed $data
  280. * @return mixed
  281. * @throws Exception
  282. */
  283. public function postClass($data)
  284. {
  285. try {
  286. $this->_log(__FUNCTION__, func_get_args());
  287. return $this->_product_api->postClass($data);
  288. } catch (Exception $e) {
  289. $this->_log(__FUNCTION__, func_get_args(), $e);
  290. throw $e;
  291. }
  292. }
  293. /**
  294. * @param int $id
  295. * @param mixed $data
  296. * @return mixed
  297. * @throws Exception
  298. */
  299. public function putClass($id, $data)
  300. {
  301. try {
  302. $this->_log(__FUNCTION__, func_get_args());
  303. return $this->_product_api->putClass($id, $data);
  304. } catch (Exception $e) {
  305. $this->_log(__FUNCTION__, func_get_args(), $e);
  306. throw $e;
  307. }
  308. }
  309. /**
  310. * @param int $id
  311. * @return mixed
  312. * @throws Exception
  313. */
  314. public function deleteClass($id)
  315. {
  316. try {
  317. $this->_log(__FUNCTION__, func_get_args());
  318. return $this->_product_api->deleteClass($id);
  319. } catch (Exception $e) {
  320. $this->_log(__FUNCTION__, func_get_args(), $e);
  321. throw $e;
  322. }
  323. }
  324. /**
  325. * @return mixed
  326. * @throws Exception
  327. */
  328. public function getProfile()
  329. {
  330. try {
  331. $this->_log(__FUNCTION__, func_get_args());
  332. return $this->_product_api->getProfile();
  333. } catch (Exception $e) {
  334. $this->_log(__FUNCTION__, func_get_args(), $e);
  335. throw $e;
  336. }
  337. }
  338. /**
  339. * @param array $data
  340. * @return mixed
  341. * @throws Exception
  342. */
  343. public function runBatch(array $data)
  344. {
  345. try {
  346. $this->_log(__FUNCTION__, func_get_args());
  347. return $this->_product_api->runBatch($data);
  348. } catch (Exception $e) {
  349. $this->_log(__FUNCTION__, func_get_args(), $e);
  350. throw $e;
  351. }
  352. }
  353. /**
  354. * @param string $method
  355. * @param array $params
  356. * @param Exception $exception
  357. * @return void
  358. * @throws Exception
  359. */
  360. protected function _log($method, array $params, Exception $exception = null)
  361. {
  362. if ($this->isEnabled()) {
  363. $date = new DateTime();
  364. $file = "{$this->getLogPath()}/{$date->format('Y-m-d')}.log";
  365. if (false === ($handle = @fopen($file, 'ab'))) {
  366. throw new Exception('_file_open');
  367. }
  368. $args = serialize($params);
  369. if (is_null($exception)) {
  370. if (in_array('debug', $this->getLevels())) {
  371. $message = array(
  372. 'Debug',
  373. $date->format('H:i:s'),
  374. "call {$method} with {$args}",
  375. );
  376. }
  377. } else {
  378. if (in_array('error', $this->getLevels())) {
  379. $message = array(
  380. 'Error',
  381. $date->format('H:i:s'),
  382. "call {$method} with {$args}",
  383. "Exception: {$exception->getMessage()} ({$exception->getCode()})",
  384. "Trace:\n{$exception->getTraceAsString()}\n\n",
  385. );
  386. }
  387. }
  388. if (isset($message)) {
  389. if (false === fwrite($handle, implode("\t", $message))) {
  390. throw new Exception('_file_write');
  391. }
  392. if (false === fwrite($handle, "\n")) {
  393. throw new Exception('_file_write');
  394. }
  395. if (false === fclose($handle)) {
  396. throw new Exception('_file_close');
  397. }
  398. }
  399. }
  400. }
  401. }