Completed
Branch master (a3fffb)
by Hong
33:51 queued 28:35
created

HandlerAbstract::close()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
}