ToConsole   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 70
c 1
b 0
f 0
dl 0
loc 151
ccs 0
cts 62
cp 0
rs 10
wmc 13

5 Methods

Rating   Name   Duplication   Size   Complexity  
A set() 0 9 2
B getTypeColor() 0 23 6
A __construct() 0 4 1
A addToLog() 0 15 2
A singleton() 0 6 2
1
<?php
2
/**
3
 * Class to Log messages to Console.
4
 *
5
 * @author    Vitex <[email protected]>
6
 * @copyright 2016 [email protected] (G)
7
 */
8
9
namespace Ease\Logger;
10
11
/**
12
 * Description of ToConsole
13
 *
14
 * @author vitex
15
 */
16
class ToConsole extends ToMemory implements Loggingable
17
{
18
    /**
19
     * Saves obejct instace (singleton...).
20
     */
21
    private static $instance = null;
22
23
    /**
24
     * Standard Output handle
25
     *
26
     * @var resource|false
27
     */
28
    public $stdout = false;
29
30
    /**
31
     * Standard error handle
32
     *
33
     * @var resource
34
     */
35
    public $stderr = false;
36
37
    /**
38
     * Ansi Codes
39
     *
40
     * @var array
41
     */
42
    protected static $ANSI_CODES = array(
43
        "off" => 0,
44
        "bold" => 1,
45
        "italic" => 3,
46
        "underline" => 4,
47
        "blink" => 5,
48
        "inverse" => 7,
49
        "hidden" => 8,
50
        "black" => 30,
51
        "red" => 31,
52
        "green" => 32,
53
        "yellow" => 33,
54
        "blue" => 34,
55
        "magenta" => 35,
56
        "cyan" => 36,
57
        "white" => 37,
58
        "black_bg" => 40,
59
        "red_bg" => 41,
60
        "green_bg" => 42,
61
        "yellow_bg" => 43,
62
        "blue_bg" => 44,
63
        "magenta_bg" => 45,
64
        "cyan_bg" => 46,
65
        "white_bg" => 47
66
    );
67
68
    /**
69
     * Log Status messages to console
70
     */
71
    public function __construct()
72
    {
73
        $this->stdout = fopen('php://stdout', 'w');
74
        $this->stderr = fopen('php://stderr', 'w');
0 ignored issues
show
Documentation Bug introduced by
It seems like fopen('php://stderr', 'w') can also be of type false. However, the property $stderr is declared as type resource. Maybe add an additional type 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 mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
75
    }
76
77
    /**
78
     * Set Ansi Color
79
     * 
80
     * @param string $str
81
     * @param string $color
82
     * 
83
     * @return string
84
     */
85
    public static function set($str, $color)
86
    {
87
        $color_attrs = explode("+", $color);
88
        $ansi_str    = "";
89
        foreach ($color_attrs as $attr) {
90
            $ansi_str .= "\033[".self::$ANSI_CODES[$attr]."m";
91
        }
92
        $ansi_str .= $str."\033[".self::$ANSI_CODES["off"]."m";
93
        return $ansi_str;
94
    }
95
96
    /**
97
     * Zapise zapravu do logu.
98
     *
99
     * @param string $caller  název volajícího objektu
100
     * @param string $message zpráva
101
     * @param string $type    typ zprávy (success|info|error|warning|*)
102
     *
103
     * @return boolean|null byl report zapsán ?
104
     */
105
    public function addToLog($caller, $message, $type = 'message')
106
    {
107
        $message = $this->set(
108
            ' '.Message::getTypeUnicodeSymbol($type).' '.strip_tags($message),
109
            self::getTypeColor($type)
110
        );
111
        $logLine = strftime("%D %T").' `'.$caller.'` '.$message;
112
113
        switch ($type) {
114
        case 'error':
115
            fputs($this->stderr, $logLine."\n");
116
            break;
117
        default:
118
            fputs($this->stdout, $logLine."\n");
0 ignored issues
show
Bug introduced by
It seems like $this->stdout can also be of type boolean; however, parameter $handle of fputs() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

118
            fputs(/** @scrutinizer ignore-type */ $this->stdout, $logLine."\n");
Loading history...
119
            break;
120
        }
121
    }
122
123
    /**
124
     * Get color code for given message 
125
     * 
126
     * @param string $type
127
     */
128
    public static function getTypeColor($type)
129
    {
130
        switch ($type) {
131
        case 'mail':                       // Envelope
132
            $color = 'blue';
133
            break;
134
        case 'warning':                    // Vykřičník v trojůhelníku
135
            $color = 'yellow';
136
            break;
137
        case 'error':                      // Lebka
138
            $color = 'red';
139
            break;
140
        case 'debug':                      // Kytička
141
            $color = 'magenta';
142
            break;
143
        case 'success':                    // Kytička
144
            $color = 'green';
145
            break;
146
        default:                           // i v kroužku
147
            $color = 'white';
148
            break;
149
        }
150
        return $color;
151
    }
152
153
    /**
154
     * Pri vytvareni objektu pomoci funkce singleton (ma stejne parametry, jako
155
     * konstruktor) se bude v ramci behu programu pouzivat pouze jedna jeho
156
     * instance (ta prvni).
157
     *
158
     * @link http://docs.php.net/en/language.oop5.patterns.html Dokumentace a
159
     * priklad
160
     */
161
    public static function singleton()
162
    {
163
        if (!isset(self::$instance)) {
164
            self::$instance = new self();
165
        }
166
        return self::$instance;
167
    }
168
}
169