Completed
Push — main ( 8f9aaa...dcb65b )
by huang
05:08 queued 12s
created

Util::writeInfoLog()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 4
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Xmly;
4
5
final class Util
6
{
7 4
    public static function jsonDecode($json, $assoc = false, $depth = 512)
8
    {
9 4
        static $jsonErrors = array(
10
            JSON_ERROR_DEPTH => 'JSON_ERROR_DEPTH - Maximum stack depth exceeded',
11
            JSON_ERROR_STATE_MISMATCH => 'JSON_ERROR_STATE_MISMATCH - Underflow or the modes mismatch',
12
            JSON_ERROR_CTRL_CHAR => 'JSON_ERROR_CTRL_CHAR - Unexpected control character found',
13
            JSON_ERROR_SYNTAX => 'JSON_ERROR_SYNTAX - Syntax error, malformed JSON',
14
            JSON_ERROR_UTF8 => 'JSON_ERROR_UTF8 - Malformed UTF-8 characters, possibly incorrectly encoded'
15
        );
16
17 4
        if (empty($json)) {
18
            return null;
19
        }
20 4
        $data = \json_decode($json, $assoc, $depth);
21
22 4
        if (JSON_ERROR_NONE !== json_last_error()) {
23
            $last = json_last_error();
24
            throw new \InvalidArgumentException(
25
                'Unable to parse JSON data: '
26
                . (isset($jsonErrors[$last])
27
                    ? $jsonErrors[$last]
28
                    : 'Unknown error')
29
            );
30
        }
31
32 4
        return $data;
33
    }
34
35 4
    public static function randomString($length = 11)
36
    {
37 4
        $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
38 4
        $randStr = str_shuffle(str_repeat($str, $length));//打乱字符串
39 4
        return substr($randStr, 0, $length);
40
    }
41
42 4
    public static function msecTime()
43
    {
44 4
        return floor(microtime(true) * 1000);
45
    }
46
47 4
    public static function mkdir()
48
    {
49 4
        if (!is_dir(Config::LOG_PATH)) {
50 1
            mkdir(Config::LOG_PATH, 0777, true);
51
        }
52 4
    }
53
54 4
    public static function writeLog($logLevel, $url, $body, $statusCode = null, $duration = null, $error = null)
55
    {
56 4
        self::mkdir();
57 4
        date_default_timezone_set("Asia/Shanghai");
58 4
        $logFileName = Config::LOG_PATH . $logLevel . '-' . date('Y-m-d-H') . '.log';
59 4
        if ($logLevel === 'INFO') {
60 4
            $content = date('Y-m-d H:i:s') . ' [' . $logLevel . '] ';
61 4
            $content .= $statusCode . '  ' . $duration . '  ' . $url . '  ' . $body . "\n";
62
        } else {
63 1
            $content = date('Y-m-d H:i:s') . ' [' . $logLevel . '] ';
64 1
            $content .= $statusCode . '  ' . $duration . '  ' . $url . '  ' . $body . '  ' . $error . "\n";
65
        }
66
67 4
        file_put_contents($logFileName, $content, FILE_APPEND);
68 4
    }
69
70 4
    public static function writeInfoLog($url, $body, $statusCode, $duration)
71
    {
72 4
        self::writeLog('INFO', $url, $body, $statusCode, $duration);
73 4
    }
74
75 1
    public static function writeErrLog($url, $body, $statusCode, $duration, $error)
76
    {
77 1
        self::writeLog('ERROR', $url, $body, $statusCode, $duration, $error);
78 1
    }
79
}
80