Completed
Pull Request — master (#1)
by Ryuichi
02:26
created

FileOutputter::writeLog()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
namespace WebStream\Log\Outputter;
3
4
use WebStream\Cache\Driver\ICache;
5
use WebStream\Cache\Driver\CacheDriverFactory;
6
use WebStream\Container\Container;
7
use WebStream\IO\Writer\SimpleFileWriter;
8
use WebStream\Log\LoggerCache;
9
10
/**
11
 * FileOutputter
12
 * @author Ryuichi Tanaka
13
 * @since 2016/01/26
14
 * @version 0.7
15
 */
16
class FileOutputter implements IOutputter, ILazyWriter
17
{
18
    /**
19
     * @var ICache キャッシュドライバ
20
     */
21
    private $driver;
22
23
    /**
24
     * @var int バッファリングサイズ
25
     */
26
    private $bufferSize;
27
28
    /**
29
     * @var bool 遅延書き出しフラグ
30
     */
31
    private $isLazyWrite;
32
33
    /**
34
     * @var SimpleFileWriter Writerオブジェクト
35
     */
36
    private $writer;
37
38
    /**
39
     * @var LoggerCache ログキャッシュ
40
     */
41
    private $cache;
42
43
    /**
44
     * constructor
45
     * @param string $logPath ログファイルパス
46
     * @param int $bufferSize バッファリングサイズ
47
     */
48 141
    public function __construct(string $logPath, int $bufferSize = 1000)
49
    {
50 141
        $config = new Container(false);
51 141
        $config->classPrefix = "logger_cache";
52 141
        $factory = new CacheDriverFactory();
53 141
        $driver = $factory->create("WebStream\Cache\Driver\Apcu", $config);
54
55
        // LoggerCacheの中ではログは取らない
56
        $driver->inject('logger', new class() { function __call($name, $args) {} });
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
57 141
        $this->driver = $driver;
58 141
        $this->bufferSize = $bufferSize;
59 141
        $this->enableLazyWrite();
60 141
        $this->writer = new SimpleFileWriter($logPath);
61 141
    }
62
63
    /**
64
     * destructor
65
     */
66 1
    public function __destruct()
67
    {
68
        try {
69 1
            if ($this->isLazyWrite) {
70 1
                $this->writeLog(implode("", $this->cache->get()));
71
            }
72
        } catch (\Exception $ignore) {
73
            // デストラクタで例外が発生すると致命的なエラーとなる
74
        }
75 1
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80 141
    public function enableLazyWrite()
81
    {
82 141
        if ($this->driver !== null) {
83 141
            $this->isLazyWrite = true;
84 141
            $this->cache = new LoggerCache($this->driver);
85
        }
86 141
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91 1
    public function enableDirectWrite()
92
    {
93 1
        if ($this->isLazyWrite && $this->cache->length() > 0) {
94 1
            $this->writeLog(implode("", $this->cache->get()));
95
        }
96
97 1
        $this->isLazyWrite = false;
98 1
        $this->cache = null;
99 1
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 95
    public function write($message)
105
    {
106 95
        if ($this->isLazyWrite) {
107 95
            if ($this->cache->length() >= $this->bufferSize) {
108
                $this->flush();
109
                $this->clear();
0 ignored issues
show
Bug introduced by
The method clear() does not seem to exist on object<WebStream\Log\Outputter\FileOutputter>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
110
            }
111 95
            $this->cache->add($message);
112
        } else {
113
            $this->writeLog($message);
114
        }
115 95
    }
116
117
    /**
118
     * バッファをログ出力する
119
     */
120
    private function flush()
121
    {
122
        if ($this->isLazyWrite && $this->cache->length() > 0) {
123
            $this->writeLog(implode("", $this->cache->get()));
124
        }
125
    }
126
127
    /**
128
     * ログファイルに書き出す
129
     * @param string $message ログメッセージ
130
     */
131 1
    private function writeLog($message)
132
    {
133 1
        $this->writer->write($message);
134 1
    }
135
}
136