Pipe::write()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 17
Code Lines 10

Duplication

Lines 17
Ratio 100 %

Code Coverage

Tests 10
CRAP Score 5.1158

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 17
loc 17
ccs 10
cts 12
cp 0.8333
rs 8.8571
cc 5
eloc 10
nc 5
nop 1
crap 5.1158
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\Queue;
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 12
    public function __construct($filename = '/tmp/simple-fork.pipe', $mode = 0666, $block = false)
40
    {
41 12
        if (!file_exists($filename) && !posix_mkfifo($filename, $mode)) {
42
            throw new \RuntimeException('create pipe failed');
43
        }
44 12
        if (filetype($filename) != 'fifo') {
45
            throw new \RuntimeException('file exists and it is not a fifo file');
46
        }
47
48 12
        $this->filename = $filename;
49 12
        $this->block = $block;
50 12
    }
51
52 6
    public function setBlock($block = true)
53
    {
54 6
        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 6
        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 6
        $this->block = $block;
69 6
    }
70
71
    /**
72
     * if the stream is blocking, you would better set the value of size,
73
     * it will not return until the data size is equal to the value of param size
74
     *
75
     * @param int $size
76
     * @return string
77
     */
78 9 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...
79
    {
80 9
        if (!is_resource($this->read)) {
81 9
            $this->read = fopen($this->filename, 'r+');
82 9
            if (!is_resource($this->read)) {
83
                throw new \RuntimeException('open file failed');
84
            }
85 9
            if (!$this->block) {
86 6
                $set = stream_set_blocking($this->read, false);
87 6
                if (!$set) {
88
                    throw new \RuntimeException('stream_set_blocking failed');
89
                }
90 6
            }
91 9
        }
92
93 9
        return fread($this->read, $size);
94
    }
95
96
    /**
97
     * @param $message
98
     * @return int
99
     */
100 6 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...
101
    {
102 6
        if (!is_resource($this->write)) {
103 6
            $this->write = fopen($this->filename, 'w+');
104 6
            if (!is_resource($this->write)) {
105
                throw new \RuntimeException('open file failed');
106
            }
107 6
            if (!$this->block) {
108 6
                $set = stream_set_blocking($this->write, false);
109 6
                if (!$set) {
110
                    throw new \RuntimeException('stream_set_blocking failed');
111
                }
112 6
            }
113 6
        }
114
115 6
        return fwrite($this->write, $message);
116
    }
117
118
    /**
119
     *
120
     */
121 12
    public function __destruct()
122
    {
123 12
        $this->close();
124 12
    }
125
126
    /**
127
     *
128
     */
129 12
    public function close()
130
    {
131 12
        if (is_resource($this->read)) {
132 9
            fclose($this->read);
133 9
        }
134 12
        if (is_resource($this->write)) {
135 6
            fclose($this->write);
136 6
        }
137 12
    }
138
139
    public function remove()
140
    {
141
        return unlink($this->filename);
142
    }
143
}
144