Logger   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 28
c 1
b 0
f 1
dl 0
loc 84
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A reconfigure() 0 16 1
A __construct() 0 4 1
A log() 0 5 5
1
<?php
2
/**
3
 * Logger scened
4
 * User: moyo
5
 * Date: 10/10/2017
6
 * Time: 2:43 PM
7
 */
8
9
namespace Carno\Log;
10
11
use Carno\Log\Contracts\Formatter;
12
use Carno\Log\Contracts\Outputter;
13
use Carno\Log\Contracts\Replicated;
14
use Psr\Log\AbstractLogger;
15
use Psr\Log\LogLevel;
16
17
class Logger extends AbstractLogger
18
{
19
    /**
20
     * @var string
21
     */
22
    private $scene = null;
23
24
    /**
25
     * @var array
26
     */
27
    private $levels = [
28
        LogLevel::EMERGENCY,
29
        LogLevel::ALERT,
30
        LogLevel::CRITICAL,
31
        LogLevel::ERROR,
32
        LogLevel::WARNING,
33
        LogLevel::NOTICE,
34
        LogLevel::INFO,
35
        LogLevel::DEBUG,
36
    ];
37
38
    /**
39
     * @var array
40
     */
41
    private $allows = [];
42
43
    /**
44
     * @var Formatter
45
     */
46
    private $formatter = null;
47
48
    /**
49
     * @var Outputter
50
     */
51
    private $outputter = null;
52
53
    /**
54
     * @var Replicated
55
     */
56
    private $replicator = null;
57
58
    /**
59
     * Logger constructor.
60
     * @param string $scene
61
     * @param Configure $configure
62
     */
63
    public function __construct(string $scene, Configure $configure)
64
    {
65
        $this->scene = $scene;
66
        $this->reconfigure($configure);
67
    }
68
69
    /**
70
     * @param Configure $configure
71
     */
72
    public function reconfigure(Configure $configure) : void
73
    {
74
        $configure->syncLevel($this->scene, function (string $level) {
75
            $this->allows = array_slice($this->levels, 0, array_search($level, $this->levels, true) + 1);
76
        });
77
78
        $configure->syncFormatter($this->scene, function (Formatter $formatter) {
79
            $this->formatter = $formatter;
80
        });
81
82
        $configure->syncOutputter($this->scene, function (Outputter $outputter) {
83
            $this->outputter = $outputter;
84
        });
85
86
        $configure->syncReplicator($this->scene, function (Replicated $replicated = null) {
87
            $this->replicator = $replicated;
88
        });
89
    }
90
91
    /**
92
     * @param string $level
93
     * @param string $message
94
     * @param array $context
95
     */
96
    public function log($level, $message, array $context = []) : void
97
    {
98
        if (in_array($level, $this->allows) && $this->outputter && $this->formatter) {
99
            $this->outputter->write($this->formatter->render($this->scene, $level, $message, $context));
100
            $this->replicator && $this->replicator->send($this->scene, $level, $message, $context);
101
        }
102
    }
103
}
104