Completed
Push — feature/0.7.0 ( 79c386...7f3169 )
by Ryuichi
03:02
created

FileOutputter::__destruct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
namespace WebStream\Log\Outputter;
3
4
/**
5
 * FileOutputter
6
 * @author Ryuichi Tanaka
7
 * @since 2016/01/26
8
 * @version 0.7
9
 */
10
class FileOutputter implements IOutputter, ILazyWriter
11
{
12
    /**
13
     * @var string ログファイルパス
14
     */
15
    private $logPath;
16
17
    /**
18
     * @var array<string> ログメッセージリスト
19
     */
20
    private $logMessages;
21
22
    /**
23
     * @var int バッファリングサイズ
24
     */
25
    private $bufferSize;
26
27
    /**
28
     * @var bool 遅延書き出しフラグ
29
     */
30
    private $isLazyWrite;
31
32
    /**
33
     * constructor
34
     * @param string $logPath ログファイルパス
35
     * @param int $bufferSize バッファリングサイズ
36
     */
37
    public function __construct($logPath, $bufferSize = 1000)
38
    {
39
        $this->logPath = $logPath;
40
        $this->logMessages = [];
41
        $this->bufferSize = $bufferSize;
42
        $this->isLazyWrite = true;
43
    }
44
45
    public function __destruct()
46
    {
47
        try {
48
            $this->writeLog(implode("", $this->logMessages));
49
        } catch (\Exception $ignore) {
50
            // デストラクタで例外が発生すると致命的なエラーとなる
51
        }
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function enableLazyWrite()
58
    {
59
        $this->isLazyWrite = true;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function enableDirectWrite()
66
    {
67 View Code Duplication
        if (count($this->logMessages) > 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...
68
            $this->writeLog(implode("", $this->logMessages));
69
            $this->logMessages = [];
70
        }
71
72
        $this->isLazyWrite = false;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function write($message)
79
    {
80
        if ($this->isLazyWrite) {
81 View Code Duplication
            if (count($this->logMessages) >= $this->bufferSize) {
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...
82
                $this->writeLog(implode("", $this->logMessages));
83
                $this->logMessages = [];
84
            }
85
            $this->logMessages[] = $message;
86
        } else {
87
            $this->writeLog($message);
88
        }
89
    }
90
91
    /**
92
     * ログファイルに書き出す
93
     * @param string $message ログメッセージ
94
     */
95
    private function writeLog($message)
96
    {
97
        error_log($message, 3, $this->logPath);
98
    }
99
}
100