SharedLoggerTrait   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 4
eloc 18
c 4
b 0
f 1
dl 0
loc 44
ccs 9
cts 9
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A log() 0 11 4
1
<?php
2
3
namespace Vectorface\Auth\Plugin;
4
5
use Psr\Log\LoggerTrait as PsrLoggerTrait;
6
use Stringable;
7
use Vectorface\Auth\Auth;
8
9
/**
10
 * Plugins wishing to perform their own logging may do so using this trait.
11
 */
12
trait SharedLoggerTrait
13
{
14
    /**
15
     * Use the PSR logger trait, but make the methods protected so they aren't exposed via the Auth object.
16
     */
17
    use PsrLoggerTrait {
18
        emergency as protected;
19
        alert as protected;
20
        critical as protected;
21
        error as protected;
22
        warning as protected;
23
        notice as protected;
24
        info as protected;
25
        debug as protected;
26
        log as protected;
27
    }
28
29
    /**
30
     * Logs with an arbitrary level, or don't if no logger has been set.
31
     *
32
     * @param mixed $level The log level. A LogLevel::* constant (usually)
33
     * @param Stringable|string $message The message to log.
34
     * @param array $context Further information about the context of the log message.
35 2
     */
36
    protected function log($level, Stringable|string $message, array $context = []): void
37 2
    {
38 2
        $logger = null;
39 1
        if (isset($this->logger)) {
40 2
            $logger = $this->logger;
41 2
        } elseif ($auth = $this->getAuth()) {
42
            $logger = $auth->getLogger();
43
        }
44 2
45 1
        if ($logger) {
46
            $logger->log($level, $message, $context);
47 2
        }
48
    }
49
50
    /**
51
     * Get the Auth class instance
52
     *
53
     * @return Auth
54
     */
55
    abstract protected function getAuth();
56
}
57