Passed
Branch master (0da0ff)
by Ryuichi
02:26
created

FileOutputter.php$0 ➔ __call()   A

Complexity

Conditions 1

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 1
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
crap 2
1
<?php
2
namespace WebStream\Log\Outputter;
3
4
use WebStream\Cache\Driver\ICache;
0 ignored issues
show
Bug introduced by
The type WebStream\Cache\Driver\ICache was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
5
use WebStream\Cache\Driver\CacheDriverFactory;
0 ignored issues
show
Bug introduced by
The type WebStream\Cache\Driver\CacheDriverFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use WebStream\Container\Container;
0 ignored issues
show
Bug introduced by
The type WebStream\Container\Container was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use WebStream\IO\Writer\SimpleFileWriter;
0 ignored issues
show
Bug introduced by
The type WebStream\IO\Writer\SimpleFileWriter was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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) {} });
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
    public function __destruct()
67
    {
68
        try {
69
            if ($this->isLazyWrite) {
70
                $this->writeLog(implode("", $this->cache->get()));
71
            }
72
        } catch (\Exception $ignore) {
73
            // デストラクタで例外が発生すると致命的なエラーとなる
74
        }
75
    }
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
    public function enableDirectWrite()
92
    {
93 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...
94
            $this->writeLog(implode("", $this->cache->get()));
95
        }
96
97
        $this->isLazyWrite = false;
98
        $this->cache = null;
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 95 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...
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 exist on WebStream\Log\Outputter\FileOutputter. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

109
                $this->/** @scrutinizer ignore-call */ 
110
                       clear();

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 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...
123
            $this->writeLog(implode("", $this->cache->get()));
124
        }
125
    }
126
127
    /**
128
     * ログファイルに書き出す
129
     * @param string $message ログメッセージ
130
     */
131
    private function writeLog($message)
132
    {
133
        $this->writer->write($message);
134
    }
135
}
136