HandlerAbstract::close()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Logger
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Logger\Handler;
16
17
use Phossa2\Shared\Base\ObjectAbstract;
18
use Phossa2\Logger\Entry\LogEntryInterface;
19
use Phossa2\Logger\Formatter\FormatterInterface;
20
use Phossa2\Logger\Formatter\FormatterAwareTrait;
21
22
/**
23
 * HandlerAbstract
24
 *
25
 * @package Phossa2\Logger
26
 * @author  Hong Zhang <[email protected]>
27
 * @see     ObjectAbstract
28
 * @see     HandlerInterface
29
 * @version 2.0.0
30
 * @since   2.0.0 added
31
 * @since   2.0.1 removed level param from constructor
32
 */
33
abstract class HandlerAbstract extends ObjectAbstract implements HandlerInterface
34
{
35
    use FormatterAwareTrait;
36
37
    /**
38
     * Stop log propagation after this handler ?
39
     *
40
     * @var    bool
41
     * @access protected
42
     */
43
    protected $stop;
44
45
    /**
46
     * Created with level handling
47
     *
48
     * @param  FormatterInterface $formatter if any
49
     * @param  bool $stopPropagation if u want to
50
     * @access public
51
     * @since  2.0.1 removed level param
52
     */
53
    public function __construct(
54
        FormatterInterface $formatter = null,
55
        /*# bool */ $stopPropagation = false
56
    ) {
57
        $this->stop  = (bool) $stopPropagation;
58
        $this->setFormatter($formatter);
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function __invoke(LogEntryInterface $logEntry)
65
    {
66
        if ($this->isHandling($logEntry)) {
67
            // format message with formatter
68
            call_user_func($this->getFormatter(), $logEntry);
69
70
            // write method of this handler
71
            $this->write($logEntry);
72
73
            // stop propagation if u want to
74
            if ($this->stop) {
75
                $logEntry->stopPropagation(true);
76
            }
77
        }
78
    }
79
80
    /**
81
     * Destructor
82
     *
83
     * @access public
84
     */
85
    public function __destruct()
86
    {
87
        $this->close();
88
    }
89
90
    /**
91
     * Write to handler's device
92
     *
93
     * @param  LogEntryInterface $logEntry
94
     * @access protected
95
     */
96
    abstract protected function write(LogEntryInterface $logEntry);
97
98
    /**
99
     * Is this handler handling this log ?
100
     *
101
     * To be overriden by child classes
102
     *
103
     * @param  LogEntryInterface $logEntry
104
     * @return bool
105
     * @access protected
106
     */
107
    protected function isHandling(LogEntryInterface $logEntry)/*# : bool */
0 ignored issues
show
Unused Code introduced by
The parameter $logEntry is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
108
    {
109
        return true;
110
    }
111
112
    /**
113
     * Close the handler, to be overriden by child classes
114
     *
115
     * @access protected
116
     */
117
    protected function close()
118
    {
119
    }
120
121
    /**
122
     * Get EOL char base on the platform WIN or UNIX
123
     *
124
     * @return string
125
     * @access protected
126
     */
127
    protected function getEol()/*# : string */
128
    {
129
        if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
130
            return "\r\n";
131
        } else {
132
            return "\n";
133
        }
134
    }
135
136
    /**
137
     * Test to see if in CLI mode
138
     *
139
     * @return bool
140
     * @access protected
141
     */
142
    protected function isCliMode()/*# : bool */
143
    {
144
        return 'cli' === php_sapi_name();
145
    }
146
}
147