AbstractFifo::isBlocking()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Process Library
4
 * @author Tao <[email protected]>
5
 */
6
namespace Slince\Process\Pipe;
7
8
use Slince\Process\Exception\InvalidArgumentException;
9
use Slince\Process\Exception\RuntimeException;
10
11
abstract class AbstractFifo implements PipeInterface
12
{
13
    protected $pathname;
14
15
    protected $mode;
16
17
    protected $permission;
18
19
    protected $stream;
20
21
    protected $blocking;
22
23
    public function __construct($pathname, $blocking, $mode, $permission = 0666)
24
    {
25
        if (($isExisted = file_exists($pathname)) && filetype($pathname) !== 'fifo') {
26
            throw new InvalidArgumentException("The file already exists, but is not a valid fifo file");
27
        }
28
        if (!$isExisted && !posix_mkfifo($pathname, $permission)) {
29
            throw new RuntimeException("Cannot create the fifo file");
30
        }
31
        $this->pathname = $pathname;
32
        $this->blocking = (boolean)$blocking;
33
        $this->mode = $mode;
34
        $this->permission = $permission;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function getStream()
41
    {
42
        if (!is_null($this->stream)) {
43
            return $this->stream;
44
        }
45
        return $this->stream = fopen($this->pathname, $this->mode);
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function close()
52
    {
53
        is_resource($this->stream) && fclose($this->stream);
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function setBlocking($blocking)
60
    {
61
        $this->blocking = $blocking;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function isBlocking()
68
    {
69
        return $this->blocking;
70
    }
71
}
72