Completed
Push — master ( d2992a...8fe16f )
by Michał
06:47
created

Log   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 3
dl 0
loc 141
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A makeLog() 0 6 1
A log() 0 12 2
A reloadStorage() 0 10 2
A reloadMessage() 0 10 2
A setOption() 0 5 1
A getOption() 0 8 2
A getLastMessage() 0 4 1
1
<?php
2
3
namespace SimpleLog;
4
5
use Psr\Log\LoggerInterface;
6
use Psr\Log\LoggerTrait;
7
use Psr\Log\LogLevel;
8
use Psr\Log\InvalidArgumentException;
9
use SimpleLog\Storage\StorageInterface;
10
use SimpleLog\Message\MessageInterface;
11
12
class Log implements LogInterface, LoggerInterface
13
{
14
    use LoggerTrait;
15
16
    /**
17
     * @var array
18
     */
19
    protected $defaultParams = [
20
        'log_path' => './log',
21
        'level' => 'notice',
22
        'storage' => \SimpleLog\Storage\File::class,
23
        'message' => \SimpleLog\Message\DefaultMessage::class,
24
    ];
25
26
    /**
27
     * @var \SimpleLog\Storage\StorageInterface
28
     */
29
    protected $storage;
30
31
    /**
32
     * @var array
33
     */
34
    protected $levels = [];
35
36
    /**
37
     * @var \SimpleLog\Message\MessageInterface
38
     */
39
    protected $message;
40
41
    /**
42
     * @param array $params
43
     * @throws \ReflectionException
44
     */
45
    public function __construct(array $params = [])
46
    {
47
        $this->defaultParams = array_merge($this->defaultParams, $params);
48
49
        $levels = new \ReflectionClass(new LogLevel);
50
        $this->levels = $levels->getConstants();
51
52
        $this->reloadStorage();
53
        $this->reloadMessage();
54
    }
55
56
    /**
57
     * log event information into file
58
     *
59
     * @param array|string|object $message
60
     * @param array $context
61
     * @return $this
62
     */
63
    public function makeLog($message, array $context = [])
64
    {
65
        $this->log($this->defaultParams['level'], $message, $context);
66
67
        return $this;
68
    }
69
70
    /**
71
     * @param string $level
72
     * @param string|array|object $message
73
     * @param array $context
74
     * @throws \Psr\Log\InvalidArgumentException
75
     */
76
    public function log($level, $message, array $context = [])
77
    {
78
        if (!in_array($level, $this->levels, true)) {
79
            throw new InvalidArgumentException('Level not defined: ' . $level);
80
        }
81
82
        $newMessage = $this->message
83
            ->createMessage($message, $context)
84
            ->getMessage();
85
86
        $this->storage->store($newMessage, $level);
87
    }
88
89
    /**
90
     * @return $this
91
     */
92
    protected function reloadStorage()
93
    {
94
        if ($this->defaultParams['storage'] instanceof StorageInterface) {
0 ignored issues
show
Bug introduced by
The class SimpleLog\Storage\StorageInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
95
            $this->storage = $this->defaultParams['storage'];
96
            return $this;
97
        }
98
99
        $this->storage = new $this->defaultParams['storage']($this->defaultParams);
100
        return $this;
101
    }
102
103
    /**
104
     * @return $this
105
     */
106
    protected function reloadMessage()
107
    {
108
        if ($this->defaultParams['message'] instanceof MessageInterface) {
0 ignored issues
show
Bug introduced by
The class SimpleLog\Message\MessageInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
109
            $this->message = $this->defaultParams['message'];
110
            return $this;
111
        }
112
113
        $this->message = new $this->defaultParams['message']($this->defaultParams);
114
        return $this;
115
    }
116
117
    /**
118
     * set log option for all future executions of makeLog
119
     *
120
     * @param string $key
121
     * @param mixed $val
122
     * @return $this
123
     */
124
    public function setOption($key, $val)
125
    {
126
        $this->defaultParams[$key] = $val;
127
        return $this->reloadStorage();
128
    }
129
130
    /**
131
     * return all configuration or only given key value
132
     *
133
     * @param null|string $key
134
     * @return array|mixed
135
     */
136
    public function getOption($key = null)
137
    {
138
        if (is_null($key)) {
139
            return $this->defaultParams;
140
        }
141
142
        return $this->defaultParams[$key];
143
    }
144
145
    /**
146
     * @return string
147
     */
148
    public function getLastMessage()
149
    {
150
        return $this->message->getMessage();
151
    }
152
}
153