Completed
Push — feature/0.7.0 ( 385c73...df772f )
by Ryuichi
03:38
created

FileOutputter.php$0 ➔ __call()   A

Complexity

Conditions 1

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
dl 0
loc 1
rs 10
1
<?php
2
namespace WebStream\Log\Outputter;
3
4
use WebStream\IO\Writer\SimpleFileWriter;
5
use WebStream\Cache\LoggerCache;
6
use WebStream\Cache\Driver\ICache;
7
use WebStream\Module\Utility\LoggerUtils;
8
use WebStream\Module\Utility\CacheUtils;
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
    use LoggerUtils, CacheUtils;
19
20
    /**
21
     * @var string ログファイルパス
22
     */
23
    private $logPath;
24
25
    /**
26
     * @var ICache キャッシュドライバ
27
     */
28
    private $driver;
29
30
    /**
31
     * @var int バッファリングサイズ
32
     */
33
    private $bufferSize;
34
35
    /**
36
     * @var bool 遅延書き出しフラグ
37
     */
38
    private $isLazyWrite;
39
40
    /**
41
     * @var SimpleFileWriter Writerオブジェクト
42
     */
43
    private $writer;
44
45
    /**
46
     * @var LoggerCache ログキャッシュ
47
     */
48
    private $cache;
49
50
    /**
51
     * constructor
52
     * @param string $logPath ログファイルパス
53
     * @param int $bufferSize バッファリングサイズ
54
     */
55
    public function __construct(string $logPath, int $bufferSize = 1000)
56
    {
57
        $this->logPath = $logPath;
58
        $driver = $this->getCacheDriver("apcu");
59
        // LoggerCacheの中ではログは取らない
60
        $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...
61
        $this->driver = $driver;
62
        $this->bufferSize = $bufferSize;
63
        $this->enableLazyWrite();
64
        $this->writer = new SimpleFileWriter($logPath);
65
    }
66
67
    /**
68
     * destructor
69
     */
70
    public function __destruct()
71
    {
72
        try {
73
            if ($this->isLazyWrite) {
74
                $this->writeLog(implode("", $this->cache->get()));
75
            }
76
        } catch (\Exception $ignore) {
77
            // デストラクタで例外が発生すると致命的なエラーとなる
78
        }
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function enableLazyWrite()
85
    {
86
        if ($this->driver !== null) {
87
            $this->isLazyWrite = true;
88
            $this->cache = new LoggerCache($this->driver);
89
        }
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function enableDirectWrite()
96
    {
97 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...
98
            $this->writeLog(implode("", $this->cache->get()));
99
        }
100
101
        $this->isLazyWrite = false;
102
        $this->cache = null;
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108 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...
109
    {
110
        if ($this->isLazyWrite) {
111
            if ($this->cache->length() >= $this->bufferSize) {
112
                $this->flush();
113
                $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...
114
            }
115
            $this->cache->add($message);
116
        } else {
117
            $this->writeLog($message);
118
        }
119
    }
120
121
    /**
122
     * バッファをログ出力する
123
     */
124
    private function flush()
125
    {
126 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...
127
            $this->writeLog(implode("", $this->cache->get()));
128
        }
129
    }
130
131
    /**
132
     * ログファイルに書き出す
133
     * @param string $message ログメッセージ
134
     */
135
    private function writeLog($message)
136
    {
137
        $this->writer->write($message);
138
    }
139
}
140