Logger   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 12
dl 0
loc 45
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A logRequestsOnly() 0 3 1
A log() 0 3 1
A logErrorsOnly() 0 3 1
A debugMode() 0 3 1
A __construct() 0 8 2
A getLogLevel() 0 3 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the LetsEncrypt ACME client.
7
 *
8
 * @author    Ivanov Aleksandr <[email protected]>
9
 * @copyright 2019-2020
10
 * @license   https://github.com/misantron/letsencrypt-client/blob/master/LICENSE MIT License
11
 */
12
13
namespace LetsEncrypt\Logger;
14
15
use Psr\Http\Message\ResponseInterface;
16
use Psr\Log\LoggerInterface;
17
use Psr\Log\LogLevel;
18
19
final class Logger
20
{
21
    /**
22
     * @var LoggerInterface
23
     */
24
    private $origin;
25
26
    /**
27
     * @var LogStrategy
28
     */
29
    private $strategy;
30
31
    public function __construct(LoggerInterface $origin, LogStrategy $strategy = null)
32
    {
33
        if ($strategy === null) {
34
            $strategy = LogStrategy::errorsOnly();
35
        }
36
37
        $this->origin = $origin;
38
        $this->strategy = $strategy;
39
    }
40
41
    public function log(string $level, string $message, array $context): void
42
    {
43
        $this->origin->log($level, $message, $context);
44
    }
45
46
    public function logRequestsOnly(): bool
47
    {
48
        return $this->strategy->isEqual('requestsOnly');
49
    }
50
51
    public function logErrorsOnly(): bool
52
    {
53
        return $this->strategy->isEqual('errorsOnly');
54
    }
55
56
    public function debugMode(): bool
57
    {
58
        return $this->strategy->isEqual('debugMode');
59
    }
60
61
    public function getLogLevel(ResponseInterface $response): string
62
    {
63
        return $response->getStatusCode() < 400 ? LogLevel::INFO : LogLevel::ERROR;
64
    }
65
}
66