Completed
Push — master ( 9b3fe3...b82616 )
by Hong
06:10 queued 03:55
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 HandlerInterface, 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
     * @access public
41
     */
42
    public function __destruct()
43
    {
44
        $this->close();
45
    }
46
47
    /**
48
     * {@inheritDoc}
49
     */
50
    public function handle(LogEntryInterface $entry): LogEntryInterface
51
    {
52
        if ($this->isHandling()) {
53
            $this->write($entry);
54
        }
55
        return $entry;
56
    }
57
58
    /**
59
     * Is this handler handling this log ?
60
     *
61
     * @return bool
62
     */
63
    protected function isHandling(): bool
64
    {
65
        return true;
66
    }
67
68
    /**
69
     * Close the handler
70
     */
71
    protected function close()
72
    {
73
    }
74
75
    /**
76
     * Write to handler's device
77
     *
78
     * @param  LogEntryInterface $entry
79
     */
80
    abstract protected function write(LogEntryInterface $entry);
81
}
82