Passed
Push — main ( 50df58...63f4e5 )
by Nobufumi
02:13
created

Log::__callStatic()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 2
dl 0
loc 15
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Log class (Static Wrapper for LogPsr3)
5
 *
6
 * Provides a static interface for the PSR-3 compliant LogPsr3 class.
7
 *
8
 * This class allows developers to use logging functionality via static
9
 * methods while maintaining the flexibility of the LogPsr3 instance-based design.
10
 *
11
 * @category Utility
12
 * @package  jidaikobo/log
13
 * @author   jidaikobo-shibata <[email protected]>
14
 * @license  Unlicense <https://unlicense.org/>
15
 * @link     https://github.com/jidaikobo-shibata/log
16
 */
17
18
namespace Jidaikobo;
19
20
use Jidaikobo\LogPsr3;
21
22
class Log
23
{
24
    private static ?LogPsr3 $instance = null;
25
26
    /**
27
     * Initialize the Logger instance.
28
     *
29
     * @param string $logFile     path of log file
30
     * @param int    $maxFileSize log rotation size
31
     *
32
     * @return void
33
     */
34
    public static function init(string $logFile = null, int $maxFileSize = 10 * 1024 * 1024): void
35
    {
36
        if ($logFile === null) {
37
            $logFile = dirname(__DIR__, 4) . '/logs/php.log'; // default project root
38
        }
39
40
        $logDir = dirname($logFile);
41
        if (!is_dir($logDir)) {
42
            mkdir($logDir, 0777, true);
43
        }
44
45
        self::$instance = new LogPsr3($logFile, $maxFileSize);
46
    }
47
48
    /**
49
     * Get the Logger instance.
50
     *
51
     * @return LogPsr3
52
     * @throws \RuntimeException if the instance is not initialized.
53
     */
54
    public static function getInstance(): LogPsr3
55
    {
56
        if (!self::$instance) {
57
            self::init();
58
59
            if (!self::$instance) {
60
                throw new \RuntimeException('Logger instance is not initialized.');
61
            }
62
        }
63
64
        return self::$instance;
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::instance could return the type null which is incompatible with the type-hinted return Jidaikobo\LogPsr3. Consider adding an additional type-check to rule them out.
Loading history...
65
    }
66
67
    /**
68
     * Forward static calls to the Logger instance.
69
     *
70
     * @param string            $method called method name
71
     * @param array<int, mixed> $args   arguments
72
     *
73
     * @return mixed
74
     */
75
    public static function __callStatic(string $method, array $args)
76
    {
77
        $instance = self::getInstance();
78
79
        if (!method_exists($instance, $method)) {
80
            $availableMethods = implode(', ', get_class_methods($instance));
81
            $message = sprintf(
82
                "Method '%s' does not exist on LogPsr3. Available methods: %s",
83
                $method,
84
                $availableMethods
85
            );
86
            throw new \BadMethodCallException($message);
87
        }
88
89
        return $instance->$method(...$args);
90
    }
91
}
92