AbstractAdapter::getLevel()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Balloon
7
 *
8
 * @author      Raffael Sahli <[email protected]>
9
 * @copyright   Copryright (c) 2012-2017 gyselroth GmbH (https://gyselroth.com)
10
 * @license     GPL-3.0 https://opensource.org/licenses/GPL-3.0
11
 */
12
13
namespace Micro\Log\Adapter;
14
15
use Micro\Log\Exception;
16
17
abstract class AbstractAdapter implements AdapterInterface
18
{
19
    /**
20
     * Log format.
21
     *
22
     * @var string
23
     */
24
    protected $format = '{date} {message}';
25
26
    /**
27
     * Date format.
28
     *
29
     * @var string
30
     */
31
    protected $date_format = 'Y-d-m H:i:s';
32
33
    /**
34
     * Level.
35
     *
36
     * @var int
37
     */
38
    protected $level = 7;
39
40
    /**
41
     * Create adapter.
42
     */
43
    public function __construct(? Iterable $config = null)
44
    {
45
        $this->setOptions($config);
46
    }
47
48
    /**
49
     * Get format.
50
     *
51
     * @return string
52
     */
53
    public function getFormat(): string
54
    {
55
        return $this->format;
56
    }
57
58
    /**
59
     * Get date format.
60
     */
61
    public function getDateFormat(): string
62
    {
63
        return $this->date_format;
64
    }
65
66
    /**
67
     * Get level.
68
     *
69
     * @return int
70
     */
71
    public function getLevel(): int
72
    {
73
        return $this->level;
74
    }
75
76
    /**
77
     * Set options.
78
     *
79
     * @return AdapterInterface
80
     */
81
    public function setOptions(? Iterable $config = null): AdapterInterface
82
    {
83
        if (null === $config) {
84
            return $this;
85
        }
86
87
        foreach ($config as $option => $value) {
88
            switch ($option) {
89
                case 'date_format':
90
                case 'format':
91
                    $this->{$option} = (string) $value;
92
93
                break;
94
                case 'level':
95
                    if (!is_numeric($value)) {
96
                        throw new Exception('log level must be a number');
97
                    }
98
99
                    $this->level = (int) $value;
100
101
                break;
102
                default:
103
                    throw new Exception('invalid option '.$option.' given');
104
            }
105
        }
106
107
        return $this;
108
    }
109
}
110