HandlerAbstract   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 2
dl 0
loc 55
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __destruct() 0 4 1
write() 0 1 ?
A __invoke() 0 6 2
A close() 0 3 1
A isHandling() 0 4 2
1
<?php
2
3
/**
4
 * Phoole (PHP7.2+)
5
 *
6
 * @category  Library
7
 * @package   Phoole\Logger
8
 * @copyright Copyright (c) 2019 Hong Zhang
9
 */
10
declare(strict_types=1);
11
12
namespace Phoole\Logger\Handler;
13
14
use Phoole\Logger\Entry\LogEntryInterface;
15
use Phoole\Logger\Formatter\DefaultFormatter;
16
use Phoole\Logger\Formatter\FormatterInterface;
17
use Phoole\Logger\Formatter\FormatterAwareTrait;
18
use Phoole\Logger\Formatter\FormatterAwareInterface;
19
20
/**
21
 * HandlerAbstract
22
 *
23
 * @package Phoole\Logger
24
 */
25
abstract class HandlerAbstract implements FormatterAwareInterface
26
{
27
    use FormatterAwareTrait;
28
29
    /**
30
     * @param  FormatterInterface $formatter
31
     */
32
    public function __construct(?FormatterInterface $formatter = NULL)
33
    {
34
        $this->setFormatter($formatter ?? new DefaultFormatter());
35
    }
36
37
    /**
38
     * Destructor
39
     */
40
    public function __destruct()
41
    {
42
        $this->close();
43
    }
44
45
    /**
46
     * @param  LogEntryInterface $entry
47
     */
48
    public function __invoke(LogEntryInterface $entry)
49
    {
50
        if ($this->isHandling($entry)) {
51
            $this->write($entry);
52
        }
53
    }
54
55
    /**
56
     * Close the handler if wanted
57
     */
58
    protected function close()
59
    {
60
    }
61
62
    /**
63
     * Is this handler handling this log ?
64
     *
65
     * @param  LogEntryInterface $entry
66
     * @return bool
67
     */
68
    protected function isHandling(LogEntryInterface $entry): bool
69
    {
70
        return $entry ? TRUE : TRUE;
71
    }
72
73
    /**
74
     * Write to handler's device
75
     *
76
     * @param  LogEntryInterface $entry
77
     */
78
    abstract protected function write(LogEntryInterface $entry);
79
}