Passed
Push — 2.x ( 2dab0a...b10d5f )
by Terry
02:01
created

SessionLogger::log()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 18
rs 9.9666
cc 2
nc 2
nop 1
1
<?php
2
/**
3
 * This file is part of the Shieldon package.
4
 *
5
 * (c) Terry L. <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 * 
10
 * php version 7.1.0
11
 * 
12
 * @category  Web-security
13
 * @package   Shieldon
14
 * @author    Terry Lin <[email protected]>
15
 * @copyright 2019 terrylinooo
16
 * @license   https://github.com/terrylinooo/shieldon/blob/2.x/LICENSE MIT
17
 * @link      https://github.com/terrylinooo/shieldon
18
 * @see       https://shieldon.io
19
 */
20
21
declare(strict_types=1);
22
23
namespace Shieldon\Firewall\Log;
24
25
use function date;
26
use function file_put_contents;
27
use function is_dir;
28
use function mkdir;
29
use function umask;
30
use function debug_backtrace;
31
use const PHP_EOL;
32
use const FILE_APPEND;
33
34
/**
35
 * Only use this ckass for debugging after running the unit tests.
36
 */
37
final class SessionLogger
38
{
39
    /**
40
     * Log the message for debugging.
41
     * 
42
     * @param string $text The message.
43
     * 
44
     * @return void
45
     */
46
    public static function log(string $text = ''): void
47
    {
48
        $dir = BOOTSTRAP_DIR . '/../tmp/shieldon/session_logs';
49
        $file = $dir . '/' . date('Y-m-d') . '.json';
50
    
51
        $originalUmask = umask(0);
52
53
        if (!is_dir($dir)) {
54
            mkdir($dir, 0777, true);
55
        }
56
    
57
        umask($originalUmask);
58
59
        $method = debug_backtrace()[1]['function'];
60
    
61
        $content = date('Y-m-d H:i:s') . ' - [' . $method . '] ' . $text;
62
63
        file_put_contents($file, $content . PHP_EOL, FILE_APPEND);
64
    }
65
}
66