Passed
Push — master ( 35a69c...54bf96 )
by Ryuichi
01:46
created

SimpleFileWriter::close()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 2
ccs 0
cts 0
cp 0
crap 2
rs 10
1
<?php
2
3
namespace WebStream\IO\Writer;
4
5
use WebStream\IO\File;
6
use WebStream\Exception\Extend\IOException;
0 ignored issues
show
Bug introduced by
The type WebStream\Exception\Extend\IOException 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
8
/**
9
 * SimpleFileWriter
10
 * OutputStreamWriterを継承しているが、write以外の機能を無効にする
11
 * @author Ryuichi TANAKA.
12
 * @since 2016/02/25
13
 * @version 0.7
14
 */
15
class SimpleFileWriter extends OutputStreamWriter
16
{
17
    /**
18
     * @var string ファイルパス
19
     */
20
    private $filepath;
21
22
    /**
23
     * @var int バッファリングサイズ
24
     */
25
    private $bufferSize;
26
27
    /**
28
     * @var string 書き込みモード
29
     */
30
    private $mode;
31
32
    /**
33
     * constructor
34
     * @param string $filepath ファイルパス
35
     * @param int $bufferSize バッファリングサイズ
36
     */
37 2
    public function __construct($filepath, $bufferSize = null)
38
    {
39 2
        $dirname = dirname($filepath);
40 2
        $dir = new File($dirname);
41 2
        if (!$dir->isWritable()) {
42
            throw new IOException("Cannot writable: " . $filepath);
43
        }
44
45 2
        $this->filepath = $filepath;
46 2
        $this->bufferSize = $bufferSize;
47 2
        $this->mode = file_exists($this->filepath) ? 'ab' : 'wb';
48
    }
49
50
    /**
51
     * ファイルに書き込む
52
     * ファイルが存在する場合、常に追記モード
53
     * @param mixed $data 書き込みデータ
54
     */
55 2
    public function write($data)
56
    {
57 2
        $stream = fopen($this->filepath, $this->mode);
58
59 2
        if ($this->bufferSize !== null && stream_set_write_buffer($stream, $this->bufferSize) !== 0) {
0 ignored issues
show
Bug introduced by
It seems like $stream can also be of type false; however, parameter $stream of stream_set_write_buffer() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

59
        if ($this->bufferSize !== null && stream_set_write_buffer(/** @scrutinizer ignore-type */ $stream, $this->bufferSize) !== 0) {
Loading history...
60
            throw new IOException("Failed to change the buffer size.");
61
        }
62
63 2
        if (!is_resource($stream) || $stream === false) {
64
            throw new IOException("Unable open " . $this->filepath);
65
        }
66
67 2
        if (!flock($stream, LOCK_EX | LOCK_NB)) {
68
            throw new IOException("Cannot lock file: " . $this->filepath);
69
        }
70
71 2
        if (fwrite($stream, $data) === false) {
72
            flock($stream, LOCK_UN);
73
            fclose($stream);
74
            throw new IOException("Failed to write stream.");
75
        }
76
77 2
        fflush($stream);
78 2
        flock($stream, LOCK_UN);
79 2
        fclose($stream);
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function close()
86
    {
87
        // Nothing to do
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function flush()
94
    {
95
        // Nothing to do
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function newLine()
102
    {
103
        $this->write(PHP_EOL);
104
    }
105
}
106