/decorators/ProductAPILogger.php
PHP | 425 lines | 269 code | 24 blank | 132 comment | 20 complexity | 168063a798da6a13921847005de0718b MD5 | raw file
- <?php
- /**
- * @author Samigullin Kamil <feedback@kamilsk.com>
- * @link http://www.kamilsk.com/
- */
- namespace Ecwid\decorators;
- use Ecwid\interfaces\iProductAPI,
- \DateTime,
- \Exception;
- /**
- * @package Ecwid.decorators
- * @since 1.0
- */
- class ProductAPILogger extends ProductAPIDecorator
- {
- /**
- * @var array
- */
- protected static $_defaultConfig = array(
- 'log_path' => __DIR__,
- 'levels' => array('error'),
- 'enabled' => true,
- );
- /**
- * Constructor.
- *
- * @param iProductAPI $product_api
- * @param array $config
- * <code>
- * array(
- * 'log_path' => %s:path,
- * 'levels' => %a:list('error','debug'),
- * 'enabled' => %bool,
- * )
- * </code>
- * @throws Exception
- */
- public function __construct(iProductAPI $product_api, array $config = array())
- {
- if ($this->_initConfig(array_merge(self::$_defaultConfig, $config))) {
- parent::__construct($product_api);
- } else {
- throw new Exception('_init');
- }
- }
- /**
- * @return string
- */
- public function getLogPath()
- {
- return $this->_config['log_path'];
- }
- /**
- * @param string $path
- * @return self
- * @throws Exception
- */
- public function setLogPath($path)
- {
- if ( ! is_string($path)) {
- throw new Exception('_invalid_type');
- }
- if (false === ($path = realpath($path)) or ! is_writable($path)) {
- throw new Exception('_invalid_path');
- }
- $this->_config['log_path'] = $path;
- return $this;
- }
- /**
- * @return array
- */
- public function getLevels()
- {
- return $this->_config['levels'];
- }
- /**
- * @param array|string $levels
- * @return self
- * @throws Exception
- */
- public function setLevels($levels)
- {
- if (is_string($levels)) {
- $levels = preg_split('/(\s+)?[,:\|](\s+)?|\s+/', $levels);
- }
- if ( ! is_array($levels) or empty($levels)) {
- throw new Exception('_invalid_type');
- }
- $this->_config['levels'] = array();
- $allowed = array('error', 'debug');
- foreach ($levels as $level) {
- if ( ! in_array($level, $allowed)) {
- throw new Exception('_invalid_level');
- }
- if ( ! in_array($level, $this->_config['levels'])) {
- array_push($this->_config['levels'], $level);
- }
- }
- return $this;
- }
- /**
- * @return bool
- */
- public function isEnabled()
- {
- return $this->_config['enabled'];
- }
- /**
- * @param bool $condition
- * @return void
- * @throws Exception
- */
- protected function _setCondition($condition)
- {
- if ( ! is_bool($condition)) {
- throw new Exception('_invalid_type');
- }
- $this->_config['enabled'] = $condition;
- }
- /**
- * @param array $config
- * @return bool
- * @throws Exception
- */
- protected function _initConfig(array $config)
- {
- $map = array(
- 'log_path' => 'setLogPath',
- 'levels' => 'setLevels',
- 'enabled' => '_setCondition',
- );
- $this->_silent = true;
- foreach ($config as $key => $value) {
- if (array_key_exists($key, $map)) {
- $set = $map[$key];
- $this->$set($value);
- }
- }
- $this->_silent = false;
- return true;
- }
- /* iProductAPI */
- /**
- * @param int|null $parent
- * @return mixed
- * @throws Exception
- */
- public function getCategories($parent = null)
- {
- try {
- $this->_log(__FUNCTION__, func_get_args());
- return $this->_product_api->getCategories($parent);
- } catch (Exception $e) {
- $this->_log(__FUNCTION__, func_get_args(), $e);
- throw $e;
- }
- }
- /**
- * @param int $id
- * @return mixed
- * @throws Exception
- */
- public function getCategory($id)
- {
- try {
- $this->_log(__FUNCTION__, func_get_args());
- return $this->_product_api->getCategory($id);
- } catch (Exception $e) {
- $this->_log(__FUNCTION__, func_get_args(), $e);
- throw $e;
- }
- }
- /**
- * @param int|null $category
- * @return mixed
- * @throws Exception
- */
- public function getProducts($category = null)
- {
- try {
- $this->_log(__FUNCTION__, func_get_args());
- return $this->_product_api->getProducts($category);
- } catch (Exception $e) {
- $this->_log(__FUNCTION__, func_get_args(), $e);
- throw $e;
- }
- }
- /**
- * @param int $id
- * @return mixed
- * @throws Exception
- */
- public function getProduct($id)
- {
- try {
- $this->_log(__FUNCTION__, func_get_args());
- return $this->_product_api->getProduct($id);
- } catch (Exception $e) {
- $this->_log(__FUNCTION__, func_get_args(), $e);
- throw $e;
- }
- }
- /**
- * @param mixed $data
- * @return mixed
- * @throws Exception
- */
- public function putProducts($data)
- {
- try {
- $this->_log(__FUNCTION__, func_get_args());
- return $this->_product_api->putProducts($data);
- } catch (Exception $e) {
- $this->_log(__FUNCTION__, func_get_args(), $e);
- throw $e;
- }
- }
- /**
- * @param int $id
- * @param mixed $data
- * @return mixed
- * @throws Exception
- */
- public function putProduct($id, $data)
- {
- try {
- $this->_log(__FUNCTION__, func_get_args());
- return $this->_product_api->putProduct($id, $data);
- } catch (Exception $e) {
- $this->_log(__FUNCTION__, func_get_args(), $e);
- throw $e;
- }
- }
- /**
- * @param int $count
- * @return mixed
- * @throws Exception
- */
- public function getRandomProducts($count)
- {
- try {
- $this->_log(__FUNCTION__, func_get_args());
- return $this->_product_api->getRandomProducts($count);
- } catch (Exception $e) {
- $this->_log(__FUNCTION__, func_get_args(), $e);
- throw $e;
- }
- }
- /**
- * @return mixed
- * @throws Exception
- */
- public function getClasses()
- {
- try {
- $this->_log(__FUNCTION__, func_get_args());
- return $this->_product_api->getClasses();
- } catch (Exception $e) {
- $this->_log(__FUNCTION__, func_get_args(), $e);
- throw $e;
- }
- }
- /**
- * @param int $id
- * @return mixed
- * @throws Exception
- */
- public function getClass($id)
- {
- try {
- $this->_log(__FUNCTION__, func_get_args());
- return $this->_product_api->getClass($id);
- } catch (Exception $e) {
- $this->_log(__FUNCTION__, func_get_args(), $e);
- throw $e;
- }
- }
- /**
- * @param mixed $data
- * @return mixed
- * @throws Exception
- */
- public function postClass($data)
- {
- try {
- $this->_log(__FUNCTION__, func_get_args());
- return $this->_product_api->postClass($data);
- } catch (Exception $e) {
- $this->_log(__FUNCTION__, func_get_args(), $e);
- throw $e;
- }
- }
- /**
- * @param int $id
- * @param mixed $data
- * @return mixed
- * @throws Exception
- */
- public function putClass($id, $data)
- {
- try {
- $this->_log(__FUNCTION__, func_get_args());
- return $this->_product_api->putClass($id, $data);
- } catch (Exception $e) {
- $this->_log(__FUNCTION__, func_get_args(), $e);
- throw $e;
- }
- }
- /**
- * @param int $id
- * @return mixed
- * @throws Exception
- */
- public function deleteClass($id)
- {
- try {
- $this->_log(__FUNCTION__, func_get_args());
- return $this->_product_api->deleteClass($id);
- } catch (Exception $e) {
- $this->_log(__FUNCTION__, func_get_args(), $e);
- throw $e;
- }
- }
- /**
- * @return mixed
- * @throws Exception
- */
- public function getProfile()
- {
- try {
- $this->_log(__FUNCTION__, func_get_args());
- return $this->_product_api->getProfile();
- } catch (Exception $e) {
- $this->_log(__FUNCTION__, func_get_args(), $e);
- throw $e;
- }
- }
- /**
- * @param array $data
- * @return mixed
- * @throws Exception
- */
- public function runBatch(array $data)
- {
- try {
- $this->_log(__FUNCTION__, func_get_args());
- return $this->_product_api->runBatch($data);
- } catch (Exception $e) {
- $this->_log(__FUNCTION__, func_get_args(), $e);
- throw $e;
- }
- }
- /**
- * @param string $method
- * @param array $params
- * @param Exception $exception
- * @return void
- * @throws Exception
- */
- protected function _log($method, array $params, Exception $exception = null)
- {
- if ($this->isEnabled()) {
- $date = new DateTime();
- $file = "{$this->getLogPath()}/{$date->format('Y-m-d')}.log";
- if (false === ($handle = @fopen($file, 'ab'))) {
- throw new Exception('_file_open');
- }
- $args = serialize($params);
- if (is_null($exception)) {
- if (in_array('debug', $this->getLevels())) {
- $message = array(
- 'Debug',
- $date->format('H:i:s'),
- "call {$method} with {$args}",
- );
- }
- } else {
- if (in_array('error', $this->getLevels())) {
- $message = array(
- 'Error',
- $date->format('H:i:s'),
- "call {$method} with {$args}",
- "Exception: {$exception->getMessage()} ({$exception->getCode()})",
- "Trace:\n{$exception->getTraceAsString()}\n\n",
- );
- }
- }
- if (isset($message)) {
- if (false === fwrite($handle, implode("\t", $message))) {
- throw new Exception('_file_write');
- }
- if (false === fwrite($handle, "\n")) {
- throw new Exception('_file_write');
- }
- if (false === fclose($handle)) {
- throw new Exception('_file_close');
- }
- }
- }
- }
- }