1<?php
2/**
3 * Copyright (C) 2017-2019 thirty bees
4 * Copyright (C) 2007-2016 PrestaShop SA
5 *
6 * thirty bees is an extension to the PrestaShop software by PrestaShop SA.
7 *
8 * NOTICE OF LICENSE
9 *
10 * This source file is subject to the Academic Free License (AFL 3.0)
11 * that is bundled with this package in the file LICENSE.md.
12 * It is also available through the world-wide-web at this URL:
13 * https://opensource.org/licenses/afl-3.0.php
14 * If you did not receive a copy of the license and are unable to
15 * obtain it through the world-wide-web, please send an email
16 * to license@thirtybees.com so we can send you a copy immediately.
17 *
18 * @author    thirty bees <modules@thirtybees.com>
19 * @author    PrestaShop SA <contact@prestashop.com>
20 * @copyright 2017-2019 thirty bees
21 * @copyright 2007-2016 PrestaShop SA
22 * @license   Academic Free License (AFL 3.0)
23 * PrestaShop is an internationally registered trademark of PrestaShop SA.
24 */
25
26namespace TbUpdaterModule;
27
28/**
29 * Class AbstractLoggerCore
30 *
31 * @since 1.0.0
32 */
33abstract class AbstractLogger
34{
35    const DEBUG = 0;
36    const INFO = 1;
37    const WARNING = 2;
38    const ERROR = 3;
39    public $level;
40    protected $level_value = [
41        0 => 'DEBUG',
42        1 => 'INFO',
43        2 => 'WARNING',
44        3 => 'ERROR',
45    ];
46
47    /**
48     * AbstractLoggerCore constructor.
49     *
50     * @param int $level
51     *
52     * @since 1.0.0
53     */
54    public function __construct($level = self::INFO)
55    {
56        if (array_key_exists((int) $level, $this->level_value)) {
57            $this->level = $level;
58        } else {
59            $this->level = static::INFO;
60        }
61    }
62
63    /**
64     * Log a debug message
65     *
66     * @param string $message
67     */
68    public function logDebug($message)
69    {
70        $this->log($message, static::DEBUG);
71    }
72
73    /**
74     * Check the level and log the message if needed
75     *
76     * @param string $message
77     * @param int    $level
78     */
79    public function log($message, $level = self::DEBUG)
80    {
81        if ($level >= $this->level) {
82            $this->logMessage($message, $level);
83        }
84    }
85
86    /**
87     * Log an info message
88     *
89     * @param string $message
90     */
91    public function logInfo($message)
92    {
93        $this->log($message, static::INFO);
94    }
95
96    /**
97     * Log a warning message
98     *
99     * @param string $message
100     */
101    public function logWarning($message)
102    {
103        $this->log($message, static::WARNING);
104    }
105
106    /**
107     * Log an error message
108     *
109     * @param string $message
110     */
111    public function logError($message)
112    {
113        $this->log($message, static::ERROR);
114    }
115
116    /**
117     * Log the message
118     *
119     * @param string $message
120     * @param level
121     */
122    abstract protected function logMessage($message, $level);
123}
124