Passed
Push — master ( 0d72e8...44be60 )
by Gabor
04:58
created

ServiceAdapter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 61
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A log() 0 10 4
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 7.1
6
 *
7
 * @copyright 2012 - 2017 Gixx-web (http://www.gixx-web.com)
8
 * @license   https://opensource.org/licenses/MIT The MIT License (MIT)
9
 *
10
 * @link      http://www.gixx-web.com
11
 */
12
declare(strict_types = 1);
13
14
namespace WebHemi\Logger\ServiceAdapter\Klogger;
15
16
use Katzgrau\KLogger\Logger;
17
use Psr\Log\LogLevel;
18
use WebHemi\Configuration\ServiceInterface as ConfigurationInterface;
19
use WebHemi\Logger\ServiceInterface;
20
21
/**
22
 * Class ServiceAdapter.
23
 */
24
class ServiceAdapter extends LogLevel implements ServiceInterface
25
{
26
    /** @var string */
27
    protected $section;
28
    /** @var Logger */
29
    private $adapter;
30
    /** @var array */
31
    private $configuration;
32
    /** @var array */
33
    private $logLevel = [
34
        0 => self::DEBUG,
35
        1 => self::INFO,
36
        2 => self::NOTICE,
37
        3 => self::WARNING,
38
        4 => self::ERROR,
39
        5 => self::CRITICAL,
40
        6 => self::ALERT,
41
        7 => self::EMERGENCY,
42
    ];
43
    private $defaultLevel = self::WARNING;
44
45
    /**
46
     * ServiceAdapter constructor.
47
     *
48
     * @param ConfigurationInterface $configuration
49
     * @param string                 $section
50
     */
51 2
    public function __construct(ConfigurationInterface $configuration, string $section)
52
    {
53 2
        $this->configuration = $configuration->getData('logger/'.$section);
54
55 2
        $logPath = $this->configuration['path'];
56 2
        $logLevel = $this->logLevel[$this->configuration['log_level']];
57
        $options = [
58 2
            'prefix' => $this->configuration['file_name'],
59 2
            'extension' => $this->configuration['file_extension'],
60 2
            'dateFormat' => $this->configuration['date_format']
61
        ];
62
63 2
        $this->adapter = new Logger($logPath, $logLevel, $options);
64 2
    }
65
66
    /**
67
     * Logs with an arbitrary level.
68
     *
69
     * @param mixed  $level
70
     * @param string $message
71
     * @param array  $context
72
     * @return void
73
     */
74 1
    public function log($level, string $message, array $context = []) : void
75
    {
76 1
        if (is_numeric($level)) {
77 1
            $level = isset($this->logLevel[$level]) ? $this->logLevel[$level] : $this->defaultLevel;
78 1
        } elseif (!in_array($level, $this->logLevel)) {
79 1
            $level = $this->defaultLevel;
80
        }
81
82 1
        $this->adapter->log($level, $message, $context);
83 1
    }
84
}
85