Completed
Push — master ( b1d5d0...554127 )
by Hu
03:28
created

Pipe::__construct()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4.25

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 12
ccs 6
cts 8
cp 0.75
rs 9.2
cc 4
eloc 7
nc 3
nop 3
crap 4.25
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;
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 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
     * @param int $size
73
     * @return string
74
     */
75 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...
76
    {
77 9
        if (!is_resource($this->read)) {
78 9
            $this->read = fopen($this->filename, 'r+');
79 9
            if (!is_resource($this->read)) {
80
                throw new \RuntimeException("open file failed");
81
            }
82 9
            if (!$this->block) {
83 6
                $set = stream_set_blocking($this->read, false);
84 6
                if (!$set) {
85
                    throw new \RuntimeException("stream_set_blocking failed");
86
                }
87 6
            }
88 9
        }
89
90 9
        return fread($this->read, $size);
91
    }
92
93
    /**
94
     * @param $message
95
     * @return int
96
     */
97 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...
98
    {
99 6
        if (!is_resource($this->write)) {
100 6
            $this->write = fopen($this->filename, 'w+');
101 6
            if (!is_resource($this->write)) {
102
                throw new \RuntimeException("open file failed");
103
            }
104 6
            if (!$this->block) {
105 6
                $set = stream_set_blocking($this->write, false);
106 6
                if (!$set) {
107
                    throw new \RuntimeException("stream_set_blocking failed");
108
                }
109 6
            }
110 6
        }
111
112 6
        return fwrite($this->write, $message);
113
    }
114
115
    /**
116
     *
117
     */
118 12
    public function close()
119
    {
120 12
        if (is_resource($this->read)) {
121 9
            fclose($this->read);
122 9
        }
123 12
        if (is_resource($this->write)) {
124 6
            fclose($this->write);
125 6
        }
126 12
    }
127
128
    /**
129
     *
130
     */
131 12
    public function __destruct()
132
    {
133 12
        $this->close();
134 12
    }
135
136
    public function remove()
137
    {
138
        return unlink($this->filename);
139
    }
140
}
141