Logger::log()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 10
cc 3
nc 3
nop 3
crap 3
1
<?php
2
namespace Fyuze\Log;
3
4
use RuntimeException;
5
use Psr\Log\AbstractLogger;
6
use Psr\Log\LoggerInterface;
7
use Fyuze\Log\Handlers\Handler;
8
9
class Logger extends AbstractLogger implements LoggerInterface
10
{
11
    /**
12
     * Logging handlers
13
     *
14
     * @var array
15
     */
16
    protected $handlers = [];
17
18
    /**
19
     * @var array
20
     */
21
    protected $logs = [];
22
23
    /**
24
     * @var string
25
     */
26
    protected $name;
27
28
    /**
29
     * Logger constructor.
30
     *
31
     * @param $name
32
     */
33 4
    public function __construct($name)
34
    {
35 4
        $this->name = $name;
36
    }
37
38
    /**
39
     * Register a logging handler
40
     *
41
     * @param Handler $handler
42
     */
43 1
    public function register(Handler $handler)
44
    {
45 1
        $this->handlers[] = $handler;
46
    }
47
48
    /**
49
     * Logs with an arbitrary level.
50
     *
51
     * @param mixed $level
52
     * @param string $message
53
     * @param array $context
54
     *
55
     * @return $this
56
     * @throws RuntimeException
57
     */
58 2
    public function log($level, $message, array $context = [])
59
    {
60 2
        if (count($this->handlers) === 0) {
61 1
            throw new RuntimeException('You must define at least one logging handler, none provided.');
62
        }
63
64 1
        foreach ($this->handlers as $handler) {
65
66 1
            $this->logs[] = sprintf('[%s] %s', $level, $message);
67
68 1
            $handler->write($level, $message, $context);
69
        }
70
71 1
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Fyuze\Log\Logger which is incompatible with the return type mandated by Psr\Log\LoggerInterface::log() of void.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
72
    }
73
74
    /**
75
     * Gets all logs as an array
76
     *
77
     * @return array
78
     */
79 1
    public function getLogs()
80
    {
81 1
        return $this->logs;
82
    }
83
84
    /**
85
     * Turns logs into a string
86
     *
87
     * @return string
88
     */
89 1
    public function __toString()
90
    {
91 1
        $logs = '';
92
93
        /** @var \Fyuze\Log\Handlers\Log $handler */
94 1
        foreach ($this->handlers as $handler) {
95 1
            $logs .= implode("\n", $this->getLogs());
96
        }
97
98 1
        return $logs;
99
    }
100
}
101