Completed
Push — feature/0.7.0 ( 5bdbd1...cdc406 )
by Ryuichi
03:10
created

ConsoleOutputter   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 115
Duplicated Lines 16.52 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 14
c 2
b 0
f 0
lcom 1
cbo 0
dl 19
loc 115
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A __destruct() 0 4 1
A enableLazyWrite() 0 4 1
A enableDirectWrite() 0 5 1
A write() 12 12 3
A clear() 0 4 1
A flush() 7 7 3
A writeLog() 0 7 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace WebStream\Log\Outputter;
3
4
/**
5
 * ConsoleOutputter
6
 * @author Ryuichi Tanaka
7
 * @since 2016/01/26
8
 * @version 0.7
9
 */
10
class ConsoleOutputter implements IOutputter, ILazyWriter
11
{
12
    /**
13
     * https://github.com/php/php-src/tree/master/sapi
14
     * PHP7以前のものは対応しない
15
     * @var SAPIリスト
16
     */
17
    private $sapis = [
18
        'apache2handler' => 'http',
19
        'cgi'            => 'http',
20
        'cli'            => 'console',
21
        'fpm'            => 'http',
22
        'embed'          => 'unsupported',
23
        'litespeed'      => 'unsupported',
24
        'phpdbg'         => 'unsupported',
25
        'tests'          => 'unsupported'
26
    ];
27
28
    /**
29
     * @var array<string> ログメッセージリスト
30
     */
31
    private $logMessages;
32
33
    /**
34
     * @var int バッファリングサイズ
35
     */
36
    private $bufferSize;
37
38
    /**
39
     * @var bool 遅延書き出しフラグ
40
     */
41
    private $isLazyWrite;
42
43
    /**
44
     * constructor
45
     */
46
    public function __construct($bufferSize = 1000)
47
    {
48
        $this->logMessages = [];
49
        $this->bufferSize = $bufferSize;
50
        $this->isLazyWrite = false;
51
    }
52
53
    /**
54
     * destructor
55
     */
56
    public function __destruct()
57
    {
58
        $this->writeLog(implode("", $this->logMessages));
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function enableLazyWrite()
65
    {
66
        $this->isLazyWrite = true;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function enableDirectWrite()
73
    {
74
        $this->flush();
75
        $this->isLazyWrite = false;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 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...
82
    {
83
        if ($this->isLazyWrite) {
84
            if (count($this->logMessages) >= $this->bufferSize) {
85
                $this->flush();
86
                $this->clear();
87
            }
88
            $this->logMessages[] = $message;
89
        } else {
90
            $this->writeLog($message);
91
        }
92
    }
93
94
    /**
95
     * バッファをクリアする
96
     */
97
    private function clear()
98
    {
99
        $this->logMessages = [];
100
    }
101
102
    /**
103
     * バッファをログ出力する
104
     */
105 View Code Duplication
    private function flush()
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...
106
    {
107
        if ($this->isLazyWrite && count($this->logMessages) > 0) {
108
            $this->writeLog(implode("", $this->logMessages));
109
            $this->clear();
110
        }
111
    }
112
113
    /**
114
     * ログファイルに書き出す
115
     * @param string $message ログメッセージ
116
     */
117
    private function writeLog($message)
118
    {
119
        $sapi = php_sapi_name();
120
        if (array_key_exists($sapi, $this->sapis) && $this->sapis[$sapi] === 'console') {
121
            echo $message;
122
        }
123
    }
124
}
125