Completed
Push — master ( dab408...07ce84 )
by Hu
21:02 queued 17:34
created

Pipe::write()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 17
Code Lines 10

Duplication

Lines 17
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 17
loc 17
ccs 0
cts 12
cp 0
rs 8.8571
cc 5
eloc 10
nc 5
nop 1
crap 30
1
<?php
2
/**
3
 * @author Jenner <[email protected]>
4
 * @blog http://www.huyanping.cn
5
 * @license https://opensource.org/licenses/MIT MIT
6
 * @datetime: 2015/11/24 16:29
7
 */
8
9
namespace Jenner\SimpleFork;
1 ignored issue
show
Coding Style introduced by
There must be one blank line after the namespace declaration
Loading history...
10
11
12
class Pipe
13
{
14
    /**
15
     * @var resource
16
     */
17
    protected $read;
18
19
    /**
20
     * @var resource
21
     */
22
    protected $write;
23
24
    /**
25
     * @var string
26
     */
27
    protected $filename;
28
29
    /**
30
     * @var bool
31
     */
32
    protected $block;
33
34
    /**
35
     * @param string $filename fifo filename
36
     * @param int $mode
37
     * @param bool $block if blocking
38
     */
39 3
    public function __construct($filename = '/tmp/simple-fork.pipe', $mode = 0666, $block = false)
40
    {
41 3
        if (!file_exists($filename) && !posix_mkfifo($filename, $mode)) {
42
            throw new \RuntimeException("create pipe failed");
43
        }
44 3
        if (filetype($filename) != "fifo") {
45
            throw new \RuntimeException("file exists and it is not a fifo file");
46
        }
47
48 3
        $this->filename = $filename;
49 3
        $this->block = $block;
50 3
    }
51
52 3
    public function setBlock($block = true)
53
    {
54 3
        if (is_resource($this->read)) {
55
            $set = stream_set_blocking($this->read, $block);
56
            if (!$set) {
57
                throw new \RuntimeException("stream_set_blocking failed");
58
            }
59
        }
60
61 3
        if (is_resource($this->write)) {
62
            $set = stream_set_blocking($this->write, $block);
63
            if (!$set) {
64
                throw new \RuntimeException("stream_set_blocking failed");
65
            }
66
        }
67
68 3
        $this->block = $block;
69 3
    }
70
71
    /**
72
     * @param int $size
73
     * @return string
74
     */
75 3 View Code Duplication
    public function read($size = 1024)
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...
76
    {
77 3
        if (!is_resource($this->read)) {
78 3
            $this->read = fopen($this->filename, 'r+');
79 3
            if (!is_resource($this->read)) {
80
                throw new \RuntimeException("open file failed");
81
            }
82 3
            if (!$this->block) {
83
                $set = stream_set_blocking($this->read, false);
84
                if (!$set) {
85
                    throw new \RuntimeException("stream_set_blocking failed");
86
                }
87
            }
88 3
        }
89
90 3
        return fread($this->read, $size);
91
    }
92
93
    /**
94
     * @param $message
95
     * @return int
96
     */
97 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...
98
    {
99
        if (!is_resource($this->write)) {
100
            $this->write = fopen($this->filename, 'w+');
101
            if (!is_resource($this->write)) {
102
                throw new \RuntimeException("open file failed");
103
            }
104
            if (!$this->block) {
105
                $set = stream_set_blocking($this->write, false);
106
                if (!$set) {
107
                    throw new \RuntimeException("stream_set_blocking failed");
108
                }
109
            }
110
        }
111
112
        return fwrite($this->write, $message);
113
    }
114
115
    /**
116
     *
117
     */
118 3
    public function close()
119
    {
120 3
        if (is_resource($this->read)) {
121 3
            fclose($this->read);
122 3
        }
123 3
        if (is_resource($this->write)) {
124
            fclose($this->write);
125
        }
126 3
    }
127
128
    /**
129
     *
130
     */
131 3
    public function __destruct()
132
    {
133 3
        $this->close();
134 3
    }
135
136
    public function remove()
137
    {
138
        return unlink($this->filename);
139
    }
140
}
141