Passed
Push — master ( 927712...e06ccd )
by Vítězslav
05:12
created

ToStd::finalizeMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
/**
3
 * Log to stdout/stderr
4
 *
5
 * @author    Vitex <[email protected]>
6
 * @copyright 2009-2016 [email protected] (G)
7
 */
8
9
namespace Ease\Logger;
10
11
/**
12
 * Log to syslog.
13
 *
14
 * @author    Vitex <[email protected]>
15
 * @copyright 2009-2012 [email protected] (G)
16
 */
17
class ToStd extends ToMemory
18
{
19
    /**
20
     * Předvolená metoda logování.
21
     *
22
     * @var string
23
     */
24
    public $logType = 'stdout';
25
26
    /**
27
     * úroveň logování.
28
     *
29
     * @var string - silent,debug
30
     */
31
    public $logLevel = 'debug';
32
33
    /**
34
     * Odkaz na vlastnící objekt.
35
     *
36
     * @var \Ease\Sand
37
     */
38
    public $parentObject = null;
39
40
    /**
41
     * Pole uložených zpráv.
42
     *
43
     * @var array
44
     */
45
    public $statusMessages = [];
46
47
    /**
48
     * ID naposledy ulozene zpravy.
49
     *
50
     * @var int unsigned
51
     */
52
    private $messageID = 0;
53
54
    /**
55
     * Obecné konfigurace frameworku.
56
     *
57
     * @var Shared
0 ignored issues
show
Bug introduced by
The type Ease\Logger\Shared was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
58
     */
59
    public $easeShared = null;
60
61
    /**
62
     * List of allready flushed messages.
63
     *
64
     * @var array
65
     */
66
    public $flushed = [];
67
68
    /**
69
     * Saves obejct instace (singleton...).
70
     */
71
    private static $_instance = null;
72
73
    /**
74
     * Log Name
75
     *
76
     * @var string
77
     */
78
    public $logName = null;
79
80
    /**
81
     * Logovací třída.
82
     *
83
     * @param string $logName symbolic name for log
84
     */
85
    public function __construct($logName = null)
86
    {
87
        $this->logName = $logName;
88
    }
89
90
    /**
91
     * Pri vytvareni objektu pomoci funkce singleton (ma stejne parametry, jako
92
     * konstruktor) se bude v ramci behu programu pouzivat pouze jedna jeho
93
     * instance (ta prvni).
94
     *
95
     * @link http://docs.php.net/en/language.oop5.patterns.html Dokumentace a
96
     * priklad
97
     */
98
    public static function singleton()
99
    {
100
        if (!isset(self::$_instance)) {
101
            $class = __CLASS__;
102
            if (defined('EASE_APPNAME')) {
103
                self::$_instance = new $class(constant('EASE_APPNAME'));
104
            } else {
105
                self::$_instance = new $class('EaseFramework');
106
            }
107
        }
108
109
        return self::$_instance;
110
    }
111
112
    
113
    /**
114
     * Zapise zapravu do logu.
115
     *
116
     * @param string $caller  název volajícího objektu
117
     * @param string $message zpráva
118
     * @param string $type    typ zprávy (success|info|error|warning|*)
119
     *
120
     * @return null|boolean byl report zapsán ?
121
     */
122
    public function addToLog($caller, $message, $type = 'message')
123
    {
124
        ++$this->messageID;
125
        if (($this->logLevel == 'silent') && ($type != 'error')) {
126
            return;
127
        }
128
        if (($this->logLevel != 'debug') && ($type == 'debug')) {
129
            return;
130
        }
131
132
        $this->statusMessages[$type][$this->messageID] = $message;
133
134
        $message = htmlspecialchars_decode(strip_tags(stripslashes($message)));
135
136
        $user = \Ease\Shared::user();
137
        if( get_class($user) !== 'Ease\\Anonym' ) {
138
            if( method_exists($user, 'getUserName')){
139
                $person =  $user->getUserName();
140
            } else {
141
                $person = $user->getObjectName();
142
            }
143
            $caller = $person.' '.$caller;
144
        } 
145
146
        $logLine = ' `'.$caller.'` '.str_replace(['notice', 'message', 'debug', 'report',
147
                'error', 'warning', 'success', 'info', 'mail',],
148
                ['**', '##', '@@', '::'], $type).' '.$message."\n";
149
        if (!isset($this->logStyles[$type])) {
150
            $type = 'notice';
151
        }
152
153
        $this->output($type, $logLine);
154
155
        return true;
156
    }
157
158
    /**
159
     * Output logline to stderr/stdout by its type
160
     *
161
     * @param string $type    message type 'error' or anything else
162
     * @param string $logLine message to output
163
     */
164
    public function output($type, $logLine)
165
    {
166
        switch ($type) {
167
            case 'error':
168
                $stderr = fopen('php://stderr', 'w');
169
                fwrite($stderr, $this->logName.': '.$logLine);
0 ignored issues
show
Bug introduced by
It seems like $stderr can also be of type false; however, parameter $handle of fwrite() 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

169
                fwrite(/** @scrutinizer ignore-type */ $stderr, $this->logName.': '.$logLine);
Loading history...
170
                fclose($stderr);
0 ignored issues
show
Bug introduced by
It seems like $stderr can also be of type false; however, parameter $handle of fclose() 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

170
                fclose(/** @scrutinizer ignore-type */ $stderr);
Loading history...
171
                break;
172
            default:
173
                $stdout = fopen('php://stdout', 'w');
174
                fwrite($stdout, $this->logName.': '.$logLine);
175
                fclose($stdout);
176
                break;
177
        }
178
    }
179
180
    /**
181
     * Oznamuje chybovou událost.
182
     *
183
     * @param string $caller     název volající funkce, nebo objektu
184
     * @param string $message    zpráva
185
     * @param mixed  $objectData data k zaznamenání
186
     */
187
    public function error($caller, $message, $objectData = null)
188
    {
189
        if (!is_null($objectData)) {
190
            $message .= print_r($objectData, true);
191
        }
192
        $this->addToLog($caller, $message, 'error');
193
    }
194
195
    /**
196
     * Last message check/modify point before output
197
     *
198
     * @param string $messageRaw
199
     *
200
     * @return string ready to use message
201
     */
202
    public function finalizeMessage($messageRaw)
203
    {
204
        return trim($messageRaw).PHP_EOL;
205
    }
206
207
    /**
208
     * Flush Messages.
209
     *
210
     * @param string $caller
211
     *
212
     * @return int how many messages was flushed
213
     */
214
    public function flush($caller = null)
215
    {
216
        $flushed = 0;
217
        if (count($this->statusMessages)) {
218
            foreach ($this->statusMessages as $type => $messages) {
219
                foreach ($messages as $messageID => $message) {
220
                    if (!isset($this->flushed[$type][$messageID])) {
221
                        $this->addToLog($caller, $message, $type);
222
                        $this->flushed[$type][$messageID] = true;
223
                        ++$flushed;
224
                    }
225
                }
226
            }
227
        }
228
229
        return $flushed;
230
    }
231
}
232