StreamHandler::write()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
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\Logger\Message\Message;
18
use Phossa2\Logger\Entry\LogEntryInterface;
19
use Phossa2\Logger\Exception\LogicException;
20
use Phossa2\Logger\Formatter\FormatterInterface;
21
22
/**
23
 * StreamHandler
24
 *
25
 * Log to a stream (file, terminal, url etc.)
26
 *
27
 * @package Phossa2\Logger
28
 * @author  Hong Zhang <[email protected]>
29
 * @see     HandlerAbstract
30
 * @version 2.0.0
31
 * @since   2.0.0 added
32
 * @since   2.0.1 updated constructor
33
 */
34
class StreamHandler extends HandlerAbstract
35
{
36
    /**
37
     * stream
38
     *
39
     * @var    resource
40
     * @access protected
41
     */
42
    protected $stream;
43
44
    /**
45
     * Constructor
46
     *
47
     * @param  string|resource $stream the stream
48
     * @param  FormatterInterface $formatter
49
     * @param  bool $stopPropagation
50
     * @access public
51
     * @since  2.0.1 removed level param
52
     */
53
    public function __construct(
54
        $stream,
55
        FormatterInterface $formatter = null,
56
        /*# bool */ $stopPropagation = false
57
    ) {
58
        // open stream
59
        $strm = $this->openStream($stream);
60
61
        if (!is_resource($strm)) {
62
            throw new LogicException(
63
                Message::get(Message::LOG_STREAM_FAIL, $stream),
64
                Message::LOG_STREAM_FAIL
65
            );
66
        }
67
        $this->stream = $strm;
68
69
        parent::__construct($formatter, $stopPropagation);
70
    }
71
72
    /**
73
     * {@inheritDoc}
74
     */
75
    protected function write(LogEntryInterface $logEntry)
76
    {
77
        if ($this->stream) {
78
            flock($this->stream, LOCK_EX);
79
            fwrite($this->stream, $logEntry->getFormatted() . $this->getEol());
80
            flock($this->stream, LOCK_UN);
81
        }
82
    }
83
84
    /**
85
     * Open stream for writing
86
     *
87
     * @param  string|resource $path
88
     * @return resource|false
89
     * @access protected
90
     */
91
    protected function openStream(/*# string */ $path)
92
    {
93
        if (is_string($path)) {
94
            if (false !== strpos($path, '://')) {
95
                $path = 'file://' . $path;
96
            }
97
            return fopen($path, 'a');
98
        }
99
        return $path;
100
    }
101
102
    /**
103
     * {@inheritDoc}
104
     */
105
    public function close()
106
    {
107
        if ($this->stream) {
108
            fclose($this->stream);
109
        }
110
    }
111
}
112