Completed
Push — feature/0.7.0 ( 2d465b...a2171b )
by Ryuichi
03:17
created

FileOutputter::clear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace WebStream\Log\Outputter;
3
4
use WebStream\IO\Writer\SimpleFileWriter;
5
use WebStream\Log\LoggerCache;
6
use WebStream\Module\Utility\LoggerUtils;
7
8
/**
9
 * FileOutputter
10
 * @author Ryuichi Tanaka
11
 * @since 2016/01/26
12
 * @version 0.7
13
 */
14
class FileOutputter implements IOutputter, ILazyWriter
15
{
16
    use LoggerUtils;
17
18
    /**
19
     * @var string ログファイルパス
20
     */
21
    private $logPath;
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
    public function __construct($logPath, $bufferSize = 1000)
49
    {
50
        $this->logPath = $logPath;
51
        $this->bufferSize = $bufferSize;
52
        $this->enableLazyWrite();
53
        $this->writer = new SimpleFileWriter($logPath);
54
    }
55
56
    /**
57
     * destructor
58
     */
59
    public function __destruct()
60
    {
61
        try {
62
            if ($this->isLazyWrite) {
63
                $this->writeLog(implode("", $this->cache->get()));
64
            }
65
        } catch (\Exception $ignore) {
66
            // デストラクタで例外が発生すると致命的なエラーとなる
67
        }
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function enableLazyWrite()
74
    {
75
        if ($this->enableApcu()) {
76
            $this->isLazyWrite = true;
77
            $this->cache = new LoggerCache();
78
        }
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function enableDirectWrite()
85
    {
86 View Code Duplication
        if ($this->isLazyWrite && $this->cache->length() > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
87
            $this->writeLog(implode("", $this->cache->get()));
88
        }
89
90
        $this->isLazyWrite = false;
91
        $this->cache = null;
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97 View Code Duplication
    public function write($message)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
98
    {
99
        if ($this->isLazyWrite) {
100
            if ($this->cache->length() >= $this->bufferSize) {
101
                $this->flush();
102
                $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...
103
            }
104
            $this->cache->add($message);
105
        } else {
106
            $this->writeLog($message);
107
        }
108
    }
109
110
    /**
111
     * バッファをログ出力する
112
     */
113
    private function flush()
114
    {
115 View Code Duplication
        if ($this->isLazyWrite && $this->cache->length() > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
116
            $this->writeLog(implode("", $this->cache->get()));
117
        }
118
    }
119
120
    /**
121
     * ログファイルに書き出す
122
     * @param string $message ログメッセージ
123
     */
124
    private function writeLog($message)
125
    {
126
        $this->writer->write($message);
127
    }
128
}
129