CLogger   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 87.88%

Importance

Changes 2
Bugs 2 Features 0
Metric Value
wmc 6
c 2
b 2
f 0
lcom 1
cbo 0
dl 0
loc 92
ccs 29
cts 33
cp 0.8788
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setOutputDir() 0 4 1
A getOutput() 0 4 1
A writeErrors() 0 8 1
A init() 0 9 2
B getContents() 0 25 1
1
<?php
2
namespace krysvac\Logger;
3
4
/**
5
 * CLogger
6
 * 
7
 * @package krysvac\CLogger
8
 * @author Jonathan Sundqvist
9
 * @version 1.0
10
 */
11
class CLogger
12
{    
0 ignored issues
show
Coding Style introduced by
The opening class brace should be on a newline by itself.
Loading history...
13
    /**
14
     * output directory
15
     */
16
    private static $outputDir = __DIR__ . DIRECTORY_SEPARATOR;
17
    
18
    /**
19
     * Initializes class
20
     */
21 1
    public static function init()
22
    {
23 1
        if (!file_exists(self::getOutput()))
24
        {
25
            mkdir(self::getOutput());
26
        }
27
28 1
        set_error_handler([self::class, "writeErrors"]);
29 1
    }
30
31
    /**
32
     * Sets output directory
33
     *
34
     */
35
    public static function setOutputDir($dir)
36
    {
37
        self::$outputDir = $dir;
38
    }
39
40
    /**
41
     * Returns output directory
42
     */
43 1
    public static function getOutput()
44
    {
45 1
        return self::$outputDir . "log" . DIRECTORY_SEPARATOR;
46
    }
47
48
    /**
49
     * Writes error to file.
50
     *
51
     * @param $errno int - error level
52
     * @param $errstr string - error message
53
     * @param $errfile string - filename of error causing file
54
     * @param $errline int - line number of error
55
     *
56
     * @return bool
57
     */
58 1
    public static function writeErrors($errno, $errstr, $errfile, $errline)
59
    {
60 1
        $filename = date("Y-m-d") . ".log";
61 1
        $content  = self::getContents($errno, $errstr, $errfile, $errline);
62 1
        file_put_contents(self::getOutput() . $filename, implode("\n", $content), FILE_APPEND);
63
64 1
        return true;
65
    }
66
67
    /**
68
     * Converts errordata to array
69
     *
70
     * @param $errno int - error level
71
     * @param $errstr string - error message
72
     * @param $errfile string - filename of error causing file
73
     * @param $errline int - line number of error
74
     *
75
     * @return array
76
     */
77 1
    private static function getContents($errno, $errstr, $errfile, $errline)
78
    {
79
        $errors = [
80 1
            E_WARNING           => "E_WARNING",
81 1
            E_NOTICE            => "E_NOTICE",
82 1
            E_USER_ERROR        => "E_USER_ERROR",
83 1
            E_USER_WARNING      => "E_USER_WARNING",
84 1
            E_USER_NOTICE       => "E_USER_NOTICE",
85 1
            E_STRICT            => "E_STRICT",
86 1
            E_RECOVERABLE_ERROR => "E_RECOVERABLE_ERROR",
87 1
            E_DEPRECATED        => "E_DEPRECATED",
88 1
            E_USER_DEPRECATED   => "E_USER_DEPRECATED"
89
        ];
90
91 1
        $errmsg = $errors[$errno];
92 1
        $time   = time();
93 1
        $date   = date("Y-m-d H:i:s", $time);
94
        return [
95 1
            "Error: $errno ($errmsg)",
96 1
            "Unix Time: $time ($date)",
97 1
            "File: $errfile",
98 1
            "Line: $errline",
99 1
            "Message: $errstr"
100
        ];
101
    }
102
}
103