Passed
Push — develop ( 5fc28f...93d639 )
by Michał
16:38
created

Log::buildContext()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 6
nc 3
nop 1
dl 0
loc 13
rs 8.8571
c 0
b 0
f 0
ccs 9
cts 9
cp 1
crap 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A Log::setOption() 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\DefaultMessage
38
     */
39
    protected $message;
40
41
    /**
42
     * @param array $params
43
     * @throws \ReflectionException
44
     */
45 9
    public function __construct(array $params = [])
46
    {
47 9
        $this->defaultParams = array_merge($this->defaultParams, $params);
48
49 9
        $levels = new \ReflectionClass(new LogLevel);
50 9
        $this->levels = $levels->getConstants();
51
52 9
        $this->reloadStorage();
53 9
        $this->reloadMessage();
54 9
    }
55
56
    /**
57
     * log event information into file
58
     *
59
     * @param array|string|object $message
60
     * @param array $context
61
     * @return $this
62
     */
63 8
    public function makeLog($message, array $context = [])
64
    {
65 8
        return $this->log($this->defaultParams['level'], $message, $context);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->log($this->defaul...'], $message, $context) targeting SimpleLog\Log::log() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
66
    }
67
68
    /**
69
     * @param string $level
70
     * @param string|array|object $message
71
     * @param array $context
72
     * @throws \Psr\Log\InvalidArgumentException
73
     */
74 10
    public function log($level, $message, array $context = [])
75
    {
76 10
        if (!in_array($level, $this->levels, true)) {
77 1
            throw new InvalidArgumentException('Level not defined: ' . $level);
78
        }
79
80 9
        $newMessage = $this->message
81 9
            ->createMessage($message, $context)
82 8
            ->getMessage();
83
84 8
        $this->storage->store($newMessage, $level);
85 8
    }
86
87
    /**
88
     * @return $this
89
     */
90 10
    protected function reloadStorage()
91
    {
92 10
        if ($this->defaultParams['storage'] instanceof StorageInterface) {
93
            $this->storage = $this->defaultParams['storage'];
94
            return $this;
95
        }
96
97 10
        $this->storage = new $this->defaultParams['storage']($this->defaultParams);
98 10
        return $this;
99
    }
100
101
    /**
102
     * @return $this
103
     */
104 9
    protected function reloadMessage()
105
    {
106 9
        if ($this->defaultParams['message'] instanceof MessageInterface) {
107
            $this->message = $this->defaultParams['message'];
0 ignored issues
show
Documentation Bug introduced by
$this->defaultParams['message'] is of type SimpleLog\Message\MessageInterface, but the property $message was declared to be of type SimpleLog\Message\DefaultMessage. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

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