Regent::__construct()   C
last analyzed

Complexity

Conditions 13
Paths 27

Size

Total Lines 39
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 182

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 32
c 1
b 0
f 0
nc 27
nop 1
dl 0
loc 39
ccs 0
cts 27
cp 0
crap 182
rs 6.6166

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Class to Rule message loggers.
4
 *
5
 * @author    Vitex <[email protected]>
6
 * @copyright 2016-2019 [email protected] (G)
7
 */
8
9
namespace Ease\Logger;
10
11
/**
12
 * Description of Regent
13
 *
14
 * @author vitex
15
 */
16
class Regent extends \Ease\Atom implements Loggingable
17
{
18
    /**
19
     * Saves obejct instace (singleton...).
20
     */
21
    private static $instance = null;
22
23
    /**
24
     * Here to reach logger objects
25
     *
26
     * @var array 
27
     */
28
    public $loggers = [];
29
30
    /**
31
     * Hodnoty pro obarvování logu.
32
     *
33
     * @var array
34
     */
35
    public $logStyles = [
36
        'notice' => 'color: black;',
37
        'success' => 'color: #2C5F23;',
38
        'message' => 'color: #2C5F23;',
39
        'warning' => 'color: #AB250E;',
40
        'error' => 'color: red;',
41
        'debug' => 'font-style: italic;',
42
        'report' => 'font-wight: bold;',
43
        'info' => 'color: blue;',
44
    ];
45
46
    /**
47
     * Allow to write logs to multiplete logging destinations
48
     * 
49
     * @param string $logger class name
50
     */
51
    public function __construct($logger = null)
52
    {
53
        if (is_null($logger) && defined('EASE_LOGGER')) {
54
            $loggers = explode('|', constant('EASE_LOGGER'));
55
        } else {
56
            $loggers = empty($logger) ? ['syslog'] : [$logger];
57
        }
58
59
        foreach ($loggers as $logger) {
60
            switch ($logger) {
61
            case 'console':
62
                $this->loggers[$logger] = ToConsole::singleton();
63
                break;
64
            case 'syslog':
65
                $this->loggers[$logger] = ToSyslog::singleton();
66
                break;
67
            case 'memory':
68
                $this->loggers[$logger] = ToMemory::singleton();
69
                break;
70
            case 'email':
71
                $this->loggers[$logger] = ToEmail::singleton();
72
                break;
73
            case 'std':
74
                $this->loggers[$logger] = ToStd::singleton();
75
                break;
76
            case 'eventlog':
77
                $this->loggers[$logger] = ToEventlog::singleton();
78
                break;
79
            default:
80
                if (\class_exists($logger) && \method_exists(
81
                    $logger,
82
                    'singleton'
83
                )
84
                ) {
85
                    $this->loggers[$logger] = $logger::singleton();
86
                } else {
87
                    $this->loggers[$logger] = ToFile::singleton();
88
                }
89
                break;
90
            }
91
        }
92
    }
93
94
    public function takeMessage()
95
    {
96
        
97
    }
98
99
    /**
100
     * Add Status Message to all registered loggers
101
     *
102
     * @param string $caller  message provider
103
     * @param string $message message to log
104
     * @param string $type    info|succes|warning|error|email|...
105
     * 
106
     * @return boolean At least one logger takes message
107
     */
108
    public function addToLog($caller, $message, $type = 'info')
109
    {
110
        $logged = false;
111
        foreach ($this->loggers as $logger) {
112
            $logged = $logger->addToLog($caller, $message, $type);
113
        }
114
        return $logged;
115
    }
116
117
    /**
118
     * Add Status Object to stack
119
     * 
120
     * @param \Ease\Logger\Message $message
121
     * 
122
     * @return int number of stored message
123
     */
124
    public function addStatusObject(Message $message)
125
    {
126
        $this->addToLog($message->caller, $message->body, $message->type);
127
        \Ease\Shared::instanced()->addStatusMessage(
128
            $message->body,
129
            $message->type
130
        );
131
        return parent::addStatusMessage($message->body, $message->type);
0 ignored issues
show
Bug Best Practice introduced by
The expression return parent::addStatus...->body, $message->type) returns the type true which is incompatible with the documented return type integer.
Loading history...
132
    }
133
134
    /**
135
     * Get The Regent
136
     */
137
   
138
    public static function singleton($logger = null)
139
    {
140
        if (!isset(self::$instance)) {
141
            self::$instance = new self($logger);
142
        }
143
        return self::$instance;
144
    }
145
}
146